Ondelete. How To Check Relations?

Hello everyone!

I’m deleting a record which has a MANY_MANY relation. I need to check the middle table (tblATotblB). If there are some dependencies i had better call $this->addError();

I was trying to make a validator. But first trouble was the scenario name. When i delete a record - model says scenario is ‘update’… (wtf?)

I have to deny the deletion if there are any relative records and i need to say about them.

There is no relatives - just delete.

What is the best way to solve my problem? Is there native yii way to make it?

You should use beforeDelete(). The pattern would normally look something like this:




public function beforeDelete()

{

    if (!parent::beforeDelete())

        return false;


    // Now check your relations. Return true if delete should be allowed and false to abort.

}



If you’ve set your foreign key constraints correctly in the database, you can make sure that the record can’t be deleted if any related records exist.