2011-01-10 10 views
0

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

答えて

1

残念ながら、これはhasMany関係のCakePHPのsaveAllに組み込まれていません。私は、このページが同じことに対する解決策を探しているのを見て、それがあったと思います。

しかし、HABTM関係で構築されているようです。

Is there a way to make saveAll() remove extraneous objects?およびShouldn't saveAll remove associated records as well?)を参照してください。

フォームを検証する場合、saveAllの前にdeleteAllを実行する私のクイックソリューションを実装する必要があります。これは理想的ではありません。フォームの検証に関連しないエラーが存在する場合、関連アイテム)。

0

多分私はその質問を理解していないかもしれませんが、親レコードがCakeから削除されたときに結合を削除することについて話していますか?それは依存 'パラメータを使用してモデルの関係に設定されている場合、それを行う行います

依存

:依存キーがtrueに設定され、モデルの 削除()メソッドは、カスケードパラメータセットで呼び出された場合trueにすると、対応するモデルレコードの も削除されます。この場合は、 をtrueに設定して、ユーザーを削除すると関連するプロフィールも削除されるようにします。

http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

あなたが探しているものということですか?

関連する問題