Hi, everybody! I have a strange problem, that already took a day of my life, and trying to get more. So, I need help.
I trying to add "Trash-bin" functionality to my models (not every, just where it needs).
I using extension “trash-bin-behavior” (since I’m unable to post links here, you can find this extension in the “unofficial yii extensions repository”, hosted on github), it does very simple job: when you try to find some records, it adds some attributes to the search criteria, so you won’t see “deleted” records.
The important part of code: (there are much more, but it doesn’t have relation to my question)
/* ... */
class ETrashBinBehavior extends CActiveRecordBehavior
{
/* .... */
/* Function, that adding filtering option to a criteria
* $this->trashFlagField - is a field name, that used for "removed" flag storage
* $this->removedFlag - is a value, that means "removed state" of the record (usually removedFlag=1, and 0 means "not deleted")
*/
public function filterRemoved()
{
$owner=$this->getOwner();
$criteria=$owner->getDbCriteria();
$criteria->addCondition($owner->getTableAlias().'.'.$this->trashFlagField.'!='.CDbCriteria::PARAM_PREFIX.CDbCriteria::$paramCount);
$criteria->params[CDbCriteria::PARAM_PREFIX.CDbCriteria::$paramCount++]=$this->removedFlag;
return $owner;
}
//In the beforeFind event handler we watching, is the var "_withRemoved" set, and run function filterRemoved() if it isn't
public function beforeFind(CEvent $event)
{
if($this->getEnabled() && !$this->findRemoved && !$this->_withRemoved)
$this->filterRemoved();
$this->_withRemoved=false;
parent::beforeFind($event);
}
//This function should set _withRemoved to deactivate searching filter.
public function withRemoved()
{
$this->_withRemoved=true;
return $this->getOwner();
}
/* .... */
}
// Usage: add behavior to a model, and after that:
// 1) Get all "non-deleted" records:
$modelInstance->findAll();
// 2) Get all records, no matter if they are "deleted" or not:
$modelInstance->withRemoved()->findAll();
Executing findAll() on the model gives me expected results. But executing count() - gives me amount of all the records, not only “undeleted”. I’ve searched all the docs, all the forum, and find out, that named scopes can solve my problem. But, if I create named scopes “onlyNotDeleted()”, “onlyDeleted()” and “everyThing”, than, by default scope “everyThing” will be applied, and it’s not what I want. I want to use defaultScope to this thing, but I don’t know, how to apply it from the behavior.
Also, I found this topic: http://www.yiiframework.com/forum/index.php/topic/16732-question-cactiverecordbehavior/, and where are answer "Overwrite CActiveRecordBehavior::beforeFind() method in your behavior class" to the question "is there any way to put defaultScope method into CActiveRecordBehavior class?".
So, I’m confused. And there a no event “beforeCount” that could help me. Please, give me some advices on how to solve the problem. Maybe I have to rewrite count() function in the behavior ?
P.S. I don’t want to change defaultScope() of the model itself, all logic have to be in the behavior.
Thank you, and forgive me my poor english.