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