My database uses MyISAM engine under the hood, so I can't invoke the cascade delete action, because MyISAM doesn't support it. At the same time, I would like to delete related records in my yii2 application. How can I do that?
There are several options to address this issue.
1) Create a trigger on DB side:
CREATE TRIGGER trigger_name
BEFORE DELETE ON table_name FOR EACH ROW
BEGIN
-- variable declarations
-- trigger code
END;
2) Delete related records inside yii2 beforeDelete function:
public function beforeDelete() {
if (!parent::beforeDelete()) {
return false;
}
// ... custom code here ...
return true;
}
Triggers are faster, and good at enforcing referential integrity, that's why I chose them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With