CActiveRecord->beforeDelete() abort deletion

Hi!

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

$this->loadObjects($_POST['id'])->delete();

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);


        parent::beforeDelete();


    }

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.

true.

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. >.>

Quote

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

It is documented:

Quote

{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 :)