Can Yii Do Cascade Deletes?

Can Yii do cascade deletes automatically based on the model relations?

Not that I’m aware of.

There’s this extension: http://www.yiiframework.com/extension/advancedrelationsbehavior/

I know that if you set ON DELETE, CASCADE in mysql, Yii will do it.

What do you mean is you set ON DELETE, CASCADE.

Basically you mean you write all the code to do the deletes in DB, and then call delete on the model and the DB will remove the rest etc. Basically you still have to do all the work in the DB.

Hi james as jim pointed out you can use constraints in mysql if you running mysql as your back-end alternatively you can make use of AR hooks

here is article on Stack Overflow that discuss constraints in details

http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html

In fact, it’s not Yii who does it but mysql. I am using innodb and in the relation view, I set ‘CASCADE’ for the ‘ondelete’ action (for the master record).

When I delete a master record in Yii’s application, all the related child records are deleted.

Sorry for late reply

Up ;)

Hi everyones, lucky people who already know Yii

So, basically, to answer the initial question of this thread, no?

or in the meantime (Yii 1.14 or 1.15 now), is it yes now?

The answer was "yes" all the time.

You can use beforeDelete/afterDelete hooks to do whatever you want with related models.

And in some cases it’s way more convenient than using DB triggers.

But notice that these events will not fire in case of massive db queries, like deleteAll and updateAll. This is by design.