krillzip
(Krillzip)
February 12, 2009, 10:40pm
1
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.
krillzip
(Krillzip)
February 13, 2009, 6:55pm
3
Wrong answer!
return parent::beforeDelete();
is the right one.
jonah
(Poppitypop)
February 13, 2009, 7:25pm
4
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. >.>
crazywizdom
(Tzewang Dorje)
December 21, 2010, 12:26pm
8
jonah:
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.
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