I might be wrong, but I think, that’s a bug in the docs. From the AR source code i can’t see where scopes are applied for delete*() commands.
BTW i’ve used exactly the same solution like you did in a similar situation for a find command. Only problem i see: It makes the code harder to understand. So now i’m using this approach instead:
Remember, a scope consists of the property values for a CDbCriteria. So you can use the array values of scopes() and throw them at pretty much any AR method you want.
An easy way around this is to add the following function to your base active record class:
/**
* Deletes rows with the specified condition, after applying existing scopes
* See {@link find()} for detailed explanation about $condition and $params.
* @param mixed $condition query condition or criteria.
* @param array $params parameters to be bound to an SQL statement.
* @return integer the number of rows deleted
*/
public function deleteAllWithScopes($condition='',$params=array())
{
Yii::trace(get_class($this).'.deleteAllWithScopes()',
'system.db.ar.CustomCActiveRecord');
$builder=$this->getCommandBuilder();
$criteria=$builder->createCriteria($condition,$params);
$this->applyScopes($criteria);
$command=$builder->createDeleteCommand($this->getTableSchema(),$criteria);
return $command->execute();
}
I really feel deleteAll() should use scopes. Would there be any problem if I just overload the deleteAll() instead of a new function called deleteWithScopes()?