Overwriting beforeFind in behavior

I would like to add a condition to all finds, in models implementing this behavior.

I’ve tried this, but doesn’t seems to be invoked:




class SiteBehavior extends CActiveRecordBehavior {

    public function beforeFind() {

		if ($this->getEnabled()) {

			$this->Owner

			->getDbCriteria()

			->addCondition('`' . $this->tableName() . '`.`siteId` = ' . Yii::app()->params->siteId . '');

		}

	}

}



Thanks in advance :)

I assume you overrode behaviors() in the model to have the SiteBehavior automatically attached.

Did you by any chance declare a local beforeFind() method in the model and forgot to call the parent?

/Tommy

Sorry, i was editing on another server, and couldn’t see the changes.

If anyone should ever get by this post, here is the final code I’ve made up:




<?php

class SiteBehavior extends CActiveRecordBehavior {


    public function beforeFind() {

		if ($this->getEnabled()) {

			$this->Owner

			->getDbCriteria()

			->addCondition('`t`.`siteId` IN(0,' . Yii::app()->params->siteId . ')');

		}

	}

	

	public function beforeSave() {

		if (isset($this->Owner->siteId) && is_numeric($this->Owner->siteId)) return;

		$this->Owner->siteId = Yii::app()->params->siteId;

	}

	

}

?>



Set params -> siteId to something, add the column ‘siteId’ to the tables you want, and enable this behavior to the corresponding models. Then you’ll quickly be able to run a multi site system.

siteId = 0 means the row is global.