CakePHP saveAll() 不会删除或更新连接表
我希望连接表自动更新/删除。我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,这并没有内置到 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).
也许我不明白这个问题,但是我们是在谈论从 Cake 中删除父记录时删除连接吗?如果使用“dependent”参数在模型关系中进行配置,它确实会这样做:
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:
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html
Is that what you're looking for?