CakePHP saveAll() 不会删除或更新连接表

发布于 2024-10-11 14:46:59 字数 2411 浏览 6 评论 0原文

我希望连接表自动更新/删除。我在 saveAll() 之前执行了 deleteAll() 作为解决方法。

当我提交表单时,布局和组件模型会正确更新,但布局组件模型(即联接表)会插入新数据(这就是我想要的),但不会删除引用的数据。

布局模型:

class Layout extends AppModel {
    var $name = 'Layout';

    var $hasMany = array(
        'LayoutComponentOrder' => array(
            'className' => 'LayoutComponentOrder',
            'foreignKey' => 'layout_id',
            'dependent' => false,
            'order' => 'LayoutComponentOrder.sort_id ASC',
        ),
        'ComponentVideo' => array(
            'className' => 'ComponentVideo',
            'foreignKey' => 'layout_id',
            'dependent' => false,
        ),
);}

组件模型:

class ComponentVideo extends AppModel {
    var $name = 'ComponentVideo';
    //The Associations below have been created with all possible keys, those that are not needed can be removed

    var $hasMany = array(
        'LayoutComponentOrder' => array(
            'className' => 'LayoutComponentOrder',
            'foreignKey' => 'layout_component_id',
            'dependent' => false,
        ),
    );

    var $belongsTo = array(
        'Layout' => array(
            'className'    => 'Layout',
            'foreignKey'    => 'layout_id'
        ),
    );
};

布局组件模型(连接表):

class LayoutComponentOrder extends AppModel {
    var $name = 'LayoutComponentOrder';
    var $uses = 'layout_component_orders'; 

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    var $belongsTo = array(
            'Layout' => array(
                'className'    => 'Layout',
                'foreignKey'    => 'layout_id'
            ),
            'ComponentVideo' => array(
                'className'    => 'ComponentVideo',
                'foreignKey'    => 'layout_component_id'
            )
        );
}

布局控制器:

// deleting the data manually
$this->LayoutComponentOrder->deleteAll(array('LayoutComponentOrder.layout_id' => $layout_id));
// this one inserts into the tables including the join table        
$this->Layout->id = $layout_id;
if ($this->Layout->saveAll($this->data)) {
   $this->Session->setFlash(__('The layout has been saved', true));
}

如何自动删除连接?这可以用 CakePHP 实现吗?

I want the join table to be updated/deleted automatically. I'm doing a deleteAll() prior to saveAll() as a workaround to do this.

When I submit the form, the Layout and Component models updates correctly but the Layout Component model (which is the join table) inserts new data (which is what I want) but does not delete the referenced data.

Layout Model:

class Layout extends AppModel {
    var $name = 'Layout';

    var $hasMany = array(
        'LayoutComponentOrder' => array(
            'className' => 'LayoutComponentOrder',
            'foreignKey' => 'layout_id',
            'dependent' => false,
            'order' => 'LayoutComponentOrder.sort_id ASC',
        ),
        'ComponentVideo' => array(
            'className' => 'ComponentVideo',
            'foreignKey' => 'layout_id',
            'dependent' => false,
        ),
);}

Component Model:

class ComponentVideo extends AppModel {
    var $name = 'ComponentVideo';
    //The Associations below have been created with all possible keys, those that are not needed can be removed

    var $hasMany = array(
        'LayoutComponentOrder' => array(
            'className' => 'LayoutComponentOrder',
            'foreignKey' => 'layout_component_id',
            'dependent' => false,
        ),
    );

    var $belongsTo = array(
        'Layout' => array(
            'className'    => 'Layout',
            'foreignKey'    => 'layout_id'
        ),
    );
};

Layout Component Model (join table):

class LayoutComponentOrder extends AppModel {
    var $name = 'LayoutComponentOrder';
    var $uses = 'layout_component_orders'; 

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    var $belongsTo = array(
            'Layout' => array(
                'className'    => 'Layout',
                'foreignKey'    => 'layout_id'
            ),
            'ComponentVideo' => array(
                'className'    => 'ComponentVideo',
                'foreignKey'    => 'layout_component_id'
            )
        );
}

Layout Controller:

// deleting the data manually
$this->LayoutComponentOrder->deleteAll(array('LayoutComponentOrder.layout_id' => $layout_id));
// this one inserts into the tables including the join table        
$this->Layout->id = $layout_id;
if ($this->Layout->saveAll($this->data)) {
   $this->Session->setFlash(__('The layout has been saved', true));
}

How can I have the join be deleted automatically? Is this possible with CakePHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别想她 2024-10-18 14:46:59

不幸的是,这并没有内置到 CakePHP 的 hasMany 关系的 saveAll 中。我希望如此,因为我发现此页面正在寻找同一问题的解决方案。

不过,它似乎确实内置于 HABTM 关系中。

请参阅 有没有办法让 saveAll() 删除无关的对象?saveAll 不应该也删除关联记录吗?)

您必须实现自己的解决方案(如果表单验证,我的快速解决方案在 saveAll 之前运行 deleteAll。但这并不理想,因为如果出现与表单验证无关的错误,您将丢失现有的关联项目)。

Unfortunately, this is not built into CakePHP's saveAll for hasMany relationships. I wish it was, as I found this page searching for a solution to the same thing.

It does seem to be built in with HABTM relationships, though.

See Is there a way to make saveAll() remove extraneous objects? and Shouldn't saveAll remove associated records as well?).

You'll have to implement your own solution (My quick solution running a deleteAll before the saveAll if the form validates. This is not ideal though, because if there is an error not-related to form validation you lose the existing associated items).

焚却相思 2024-10-18 14:46:59

也许我不明白这个问题,但是我们是在谈论从 Cake 中删除父记录时删除连接吗?如果使用“dependent”参数在模型关系中进行配置,它确实会这样做:

dependent:当 dependent 键设置为 true 时,模型的
调用delete()方法并将cascade参数设置为true,
关联的模型记录也会被删除。在本例中,我们设置
true,因此删除用户也会删除其关联的个人资料。

http://book.cakephp.org/2.0/en /models/associations-linking-models-together.html

这是您要找的吗?

Maybe I don't understand the question, but are we talking about removing joins when a parent record is removed from Cake? It does do that, if configured in the model relationships using the 'dependent' parameter:

dependent: When the dependent key is set to true, and the model’s
delete() method is called with the cascade parameter set to true,
associated model records are also deleted. In this case, we set it
true so that deleting a User will also delete her associated Profile.

http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

Is that what you're looking for?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文