我希望自动更新/删除连接表。我在 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 可以做到这一点吗?