CakePHP - delete cascade not working

前端 未结 3 984
小鲜肉
小鲜肉 2021-01-06 13:50

In CakePHP I have a model Type and SpecificType.

SpecificType belongTo a Type. (type_id field)

When I delete an entry of SpecificType, how can I also delet

3条回答
  •  没有蜡笔的小新
    2021-01-06 14:25

    You want to delete the Type, not the SpecificType. You will also need to make sure you have your model set correctly for Type:

    var $hasMany = array(
        'SpecificType' => array(
        'className' => 'SpecificType',
        'foreignKey' => 'type_id',
        'dependent'=> true,
        )
    );
    

    Then delete the type and it will work.

    If you are deleting the child (SpecificType) and you want to delete it's parent, you must call the delete on the parent model. But keep in mind, if you have the Cascade set up correctly (dependent = true on the model) all of the SpecificType children will be deleted anyway.

    Note: If you want to delete the parent of the child, you may want to reconsider your relationships and confirm they are correct. If that is really how you want them, then don't do the delete on the child. Simply make sure your cascade relationships are set correctly, pull the child's parent information, and delete the parent. Then all of the children will be removed as well.

提交回复
热议问题