2013-05-08 64 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