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




$post=Post::model()->find($condition,$params);

$post=Post::model()->findByPk($postID,$condition,$params);



Is it different from the following?




$post=Post::model();

$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__)

	{

		if(isset(self::$_models[$className]))

			return self::$_models[$className];

		else

		{

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

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

			$model->attachBehaviors($model->behaviors());

			return $model;

		}

	}



Cheers

L

Excellent. Thank you!