从这里开始,我想用cake-php方法创建一个从两个表类别和子类别填充的组合框。
-- Category1
--- Subcategory1
--- Subcategory2
--- ...
-- Category2
--- Subcategory1
--- Subcategory2
--- ...
在我的控制器中,我尝试使用下面的代码获取带有子类别的类别列表,但它不起作用,只返回子类别列表:
$this->set('category',($this->category->subcategory->find('list')));
以下是我在模型之间使用的关联:
<?php
class category extends AppModel{
public $hasMany=array(
'subcategory'=>array(
'className'=>'subcategories',
)
);
}
<?php
class subcategory extends AppModel{
public $belongsTo=array(
'category'=>array(
'className'=>'categories',
'foreignKey'=> 'categories_id'
)
);
}
我希望我的问题很清楚,非常感谢。
答案 0 :(得分:0)
$options = array();
foreach($this->Category->find('all') as $key => $category) {
$options[$category['Category']['name']] = $this->Category->SubCategory->find('list')
}
$this->set('category', $options);