mauzilla
mauzilla

Reputation: 3592

Delete associative table rows when deleting a main model entry

I have 2 models, usergroups and usergrouppermissions. my usergrouppermissions has a field called user_group_id which links back to the main usergroup. In a case where I want to delete the user group, I automatically want it to delete the entry in the database for usergrouppermissions (as the user group wont exist) anymore. I have tried delete, however, it just deletes the usergroup.

  // Here is my model: (usergroup):
  public $hasMany = array(
       'UserGroupPermission'
  );

  // Here is the usergrouppermissions model:
  public $belongsTo = array(
        "UserGroup"
    );

Upvotes: 1

Views: 2793

Answers (1)

L. Sanna
L. Sanna

Reputation: 6552

Try:

$this->Model->delete($item_to_delete_id,true);

delete() docs.

Upvotes: 1

Related Questions