我想要連接表自動更新/刪除。我在saveAll()之前做了一個deleteAll()作爲解決方法。CakePHP saveAll()不會刪除或更新連接表
當我提交表單時,佈局和組件模型正確更新,但佈局組件模型(它是連接表)插入新數據(這是我想要的),但不刪除引用的數據。
佈局模型:
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?