CActiveRecord->beforeDelete() abort deletion


In my controller: ObjectController, I'm trying to delete one post like:


In my ar I have the following:

class Objects extends CActiveRecord


    protected function beforeDelete()


        Comments::model()->deleteAll('ObjectsID = '.$this->ObjectsID);

        Categories_has_Objects::model()->deleteAll('ObjectsID = '.$this->ObjectsID);

        Flags_has_Objects::model()->deleteAll('ObjectsID = '.$this->ObjectsID);

        Resources::model()->deleteAll('ObjectsID = '.$this->ObjectsID);



All the related records gets deleted, while the Objects records deletion is aborted. But this still works if I use afterDelete() instead.

Use transaction.

Wrong answer!

return parent::beforeDelete();

is the right one.

You are supposed to have beforeDelete() to return whether the record should be deleted. Defaults to true.

So "return true;" would work just as well.

The parent::beforeDelete() simply returns true, it has no other logic in it.  It is just a placeholder.


Any chance this could be noted in the api or docs somewhere? It'd be handy for idiots like myself who spent two weeks trying to figure out what was going wrong. >.>


Any chance this could be noted in the api or docs somewhere?

It is documented:


{return}  boolean  whether the saving should be executed. Defaults to true.

I guess you could argue that parent::beforeDelete() is the more correct call because it future proofs your code a little better. If additional logic should get added into the parent beforeDelete in a future version of Yii, then your code will still call it.

dan :)