2013-05-08 63 views
0

我無法保存一些HABTM數據。CakePHP保存與同一型號的HABTM關係

我的模型「類別」與本身HABTM關係稱爲「子類別」

$this->Category->create(); 
$c["Category"]["display_order"] = $this->Category->getNextDisplayOrder(); 
$c["Category"]["title"] = $this->request->data["title"]; 
$c["SubCategory"]["category_id"] = $id; //The id of the parent category 

$new_cat = $this->Category->saveAll($c); 

這將創建我的新的類別,並將其保存在數據庫中的精品,但是ID是被保存在錯誤的順序子類別表。 (category_id =>我剛剛創建的新貓ID,subcategory_id => parent_id)

任何想法爲什麼ID保存在錯誤的列?

這是我HABTM關係

public $hasAndBelongsToMany = array(
     'SubCategory' => array(
      'className' => 'Category', 
      'joinTable' => 'categories_subcategories', 
      'foreignKey' => 'category_id', 
      'associationForeignKey' => 'subcategory_id', 
      'unique' => 'keepExisting', 
      'conditions' => '', 
      'fields' => '', 
      'order' => '', 
      'limit' => '', 
      'offset' => '', 
      'finderQuery' => '', 
      'deleteQuery' => '', 
      'insertQuery' => '' 
     ) 
    ); 

回答

0

林通過您的文章混淆。

如果

$c["SubCategory"]["category_id"] = $id; //The id of the parent category 

被這應該是剛創建的類別或父類的id?你在帖子中說了兩件不同的事。

(CATEGORY_ID =>新的類別ID我剛纔提出,subcategory_id => PARENT_ID)

無論哪種方式,如果你想要的ID來是該行一個你剛剛創建的變化這

$c["SubCategory"]["category_id"] = $this->Category->id; 

$c["SubCategory"]["category_id"] = $this->Category->getLastInsertID(); 
+0

我想創建一個類,並在同一時間,存放一段關係p是它的父母($ id) – user195257 2013-05-08 16:27:19