我有一个模型“Catgeory”具有与自身与相同型号的外键的CakePHP 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' => ''
)
);
一个HABTM关系这是我用来保存类别代码。我想建立一个关系,使得这一类的父($ ID)的子
$this->Category->create();
$c["Category"]["title"] = $this->request->data["title"];
$c["SubCategory"]["CategoriesSubcategory"]["category_id"] = $id;
的结构似乎我的权利,但这并不创建关系。然而,如果我尝试并使用
$c["SubCategory"]["CategoriesSubcategory"]["subcategory_id"] = $id;
这是行得通的,但父母和孩子是错误的方式。所以这暗示了ForeignKey和AssociationForeignKey是错误的。所以,我当时在我的关系改变他们身边:
public $hasAndBelongsToMany = array(
'SubCategory' => array(
'className' => 'Category',
'joinTable' => 'categories_subcategories',
'foreignKey' => 'subcategory_id',
'associationForeignKey' => 'category_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
这意味着现在的关系得到妥善保存,但是,我认为这种关系是南辕北辙,因为当我的findAll类别,孩子们不再来通过在结果中。
任何想法,我错了吗?我是否在保存错误的东西?我是否需要创建模型子类?
您是否考虑过使用[树行为](http://book.cakephp.org /2.0/en/core-libraries/behaviors/tree.html)?它可能更适合你的目的。 编辑:哎呀,刚看到比赛帕提到它已经在下面。 – 2013-05-09 16:11:38