What I'm wondering is: is it possible in Yii to add some kind of property in a Model, so only items with the property isdeleted
set as 0
are shown?
So I'm looking for a way, Yii would just ignore these instances of the items... Something like:
public function rules()
{
return array(
...
array('isdeleted', 'shouldEqualTo=>0'),
...
);
}
I thought messing around with rules()
would be a way, but it doesn't work or I am doing it wrong...
You should use scopes() for that.
public function scopes()
{
return array('active' => array('condition' => 'isdeleted = 0'));
}
Then
$active = MyModel::model()->active()->findAll();
EDIT:
If you want to make the filter default, implement defaultScope() function:
public function defaultScope()
{
return array('condition' => 'isdeleted = 0');
}
Thanks to W.B.'s answer I knew to look into scopes
, you can use scopes
like W.B. did:
public function scopes()
{
return array('active' => array('condition' => 'isdeleted = 0'));
}
and then use
$active = MyModel::model()->active()->findAll();
If you do not want to change your code in your project (like me) you can use:
public function defaultScope()
{
return array(
'condition' => 'isdeleted = 0',
);
}
and then use
$active = MyModel::model()->findAll();