Question on the right ActiveRecord pattern

I have one curious question, hoping that someone can recommend the right approach with ActiveRecord pattern, under Yii.

Often times in tutorial you have codes like so



Is it different from the following?


$result = $post->find($condition,$params);

$result = $post->findByPk($postID,$condition,$params);

I am most likely wrong, but I have this sense that you would use less memory by creating a single Post::model() object, instead of creating one every time you do a query. However, I also fear that would defeat the purpose of atomic transaction under ActiveRecord pattern. Which approach in the code snips is recommended? and why?

Thank you for reading!

Yii implements singleton / register pattern for AR models. Hence, there is no difference between first and second usage of your code from point of view resource consumption.

CActive record keeps static array of all already instantiated models - check out CActiveRecord::model():

	public static function model($className=__CLASS__)



			return self::$_models[$className];



			$model=self::$_models[$className]=new $className(null);

			$model->_md=new CActiveRecordMetaData($model);


			return $model;





Excellent. Thank you!