結合テーブルを自動的に更新/削除したい。これを行うための回避策として、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で可能ですか?