CakePHP - 删除级联不起作用 [英] CakePHP - delete cascade not working

查看:28
本文介绍了CakePHP - 删除级联不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 CakePHP 中,我有一个模型类型和特定类型.

In CakePHP I have a model Type and SpecificType.

SpecificType 属于一个类型.(type_id 字段)

SpecificType belongTo a Type. (type_id field)

当我删除一个特定类型的条目时,我如何同时删除类型?

When I delete an entry of SpecificType, how can I also delete Type?

我有它

$this->SpecificType->del($id, true)

但是,类型下的条目不会被删除.

However, the entry under Type isn't getting deleted.

谢谢,
三通

推荐答案

您要删除 Type,而不是 SpecificType.您还需要确保为 Type 正确设置了模型:

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.

如果您要删除子模型 (SpecificType) 并且想要删除它的父模型,则必须在父模型上调用 delete.但请记住,如果您正确设置了 Cascade(模型上的 dependent = true),则无论如何都会删除所有 SpecificType 子项.

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.

这篇关于CakePHP - 删除级联不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆