CakePHP saveAll()不删除或更新联接表
我想自动更新/删除联接表。我在saveAll()之前先执行deleteAll()操作,以此作为解决方法 提交表单时,布局和组件模型会正确更新,但布局组件模型(即联接表)会插入新数据(这是我想要的),但不会删除引用的数据 布局模型:CakePHP saveAll()不删除或更新联接表,cakephp,model-associations,Cakephp,Model Associations,我想自动更新/删除联接表。我在saveAll()之前先执行deleteAll()操作,以此作为解决方法 提交表单时,布局和组件模型会正确更新,但布局组件模型(即联接表)会插入新数据(这是我想要的),但不会删除引用的数据 布局模型: class Layout extends AppModel { var $name = 'Layout'; var $hasMany = array( 'LayoutComponentOrder' => array(
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'
),
);
};
布局构件模型(联接表):
布局控制器:
// 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有可能做到这一点吗?不幸的是,CakePHP的saveAll for中没有内置这一点,因为它有很多关系。我希望是这样,因为我发现这一页正在寻找解决同样问题的方法 不过,它似乎确实与HABTM关系紧密相连 见和
您必须实现自己的解决方案(如果表单验证,我的快速解决方案在saveAll之前运行deleteAll。但这并不理想,因为如果出现与表单验证无关的错误,您将丢失现有的关联项)。也许我不理解这个问题,但是,当父记录从蛋糕中删除时,我们是在谈论删除联接吗?如果在模型关系中使用“dependent”参数进行配置,则会执行此操作: dependent:当dependent键设置为true时,模型的 调用delete()方法时,cascade参数设置为true, 关联的模型记录也将被删除。在这种情况下,我们设置它 true,这样删除用户也将删除其关联的配置文件 这就是你要找的吗
// 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));
}