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()?
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);
}