2016-06-24 34 views
1

我在數據庫Element和Section中有2個實體。CakePHP 2.5.4中的相關刪除

1部分具有0-N的元素,以便在元件表我有一個外鍵

CONSTRAINT `elements_sections` FOREIGN KEY (`section_id`) REFERENCES `sections` (`id`) ON UPDATE CASCADE ON DELETE CASCADE 

元.PHP具有此型號:

public $belongsTo = array(
     'Section' => array(
      'className' => 'Section', 
      'foreignKey' => 'section_id', 
      'conditions' => '', 
      'fields' => '', 
      'order' => '' 
     ) 
    ); 

在節模型我有:

public $hasMany = array(
    'Element' => array(
     'className' => 'Element', 
     'foreignKey' => 'section_id', 
     'dependent' => false, 
     'conditions' => '', 
     'fields' => '', 
     'order' => '', 
     'limit' => '', 
     'offset' => '', 
     'exclusive' => '', 
     'finderQuery' => '', 
     'counterQuery' => '' 
    ) 
); 

所有這些代碼看起來不錯,但是,當我刪除I​​D = X的部分的一個元素,如果有一個e在ID = X的情況下(在任何其他部分),該元素也被刪除。這是我在整個應用程序中使用的代碼,但僅在此處失敗。 $ this-> data是Element對象。

$this->Element->delete($this->data) 

詳細示例:

第20節 - >元件400 第3章 - >元件20

如果我刪除元件400,元件20也被刪除。

我想我缺少一個配置參數或一些。

在此先感謝。

回答

0

對於有人能面臨同樣的問題,這可能幫助:

型號:: delete()方法可以刪除多條記錄。

[ 
    'id' => 1, 
    'category_id' => 3, 
    'price' => 10000, 
] 

會產生

DELETE FROM elements WHERE id IN (1, 3, 10000).