Default Dependency for Model

Hi,

I’m wondering if there is an option to have a default dependency for a particular model. Currently I do this for a ActiveRecord request for a User model (simplified):




$dependency = new CFileCacheDependency($filename);

$user = User::model()->cache(1000, $dependency)->findByPk(Yii::app()->user->getId());	



Is there a way to do this automaticly for each ActiveRecord User request (with findByPk()). Something like setting a dependency in DefaultScope()?

Regards,

Tim

I have not tried this but I am sure you can override the model() method on ActiveRecord to achieve this

Still not getting it to work. Overriding the model()-function seems to be correct, but I’ll need more help ;)

Thank you alirz23!

Got it working now: since models extend from ActiveRecord, you can simply override findAll() and findByPk(). See below for an example.




//dependency override for caching

	public function findAll($condition='',$params=array())

	{

		Yii::trace(get_class($this).'.findAll()','system.db.ar.CActiveRecord');

		$criteria=$this->getCommandBuilder()->createCriteria($condition,$params);

		//return $this->query($criteria,true); --> this is edited out

		

		$dependency = new CDbCacheDependency('

			SELECT UPDATE_TIME

			FROM   information_schema.tables

			WHERE  TABLE_SCHEMA = "'.Yii::app()->params['db_name'].'"

			AND TABLE_NAME = "yii_course"');

		return $this->cache(1000, $dependency)->query($criteria,true);

	}