Is there a way to check referential integrity for MyIsam tables using YII native relations? -
while used innodb tables caught fk constraint exceptions innodb engine.
i know yii can fetch related tables according relations(). besides need check referential integrity during other crud operations - such insert, update , delete.
is there yii native mechanism perform this? or should write additional code checking?
in order avoid errors database need write additional code handle this. manage using beforedelete() method of models. write if condition check referential integrity violations uising model's relations. can either delete offending records or abort deletion not returning 'parent::beforedelete().'
in many many example user model can wrap 'return parent::beforedelete()' call in if check relations() of model in following way:
protected function beforedelete() { if (empty($this->tasks)) return parent::beforedelete(); }
the deletion aborted if user found have assigned tasks.
for reference relation in model , requires third table:
'tasks' => array(self::many_many, 'task', 'task_assignment(user_id,task_id)'),
this example basic , may want implement more complex way of handling these scenarios include deletion of related records or updating of them remove foreign key references. have decide works best , makes sense each of models , relations.
Comments
Post a Comment