如何在admin_edit.ctp
中的选择框中选择父类别分类表结构
ID Name ParentID
1 Parent1 0
2 Parent2 0
3 Child1 1
4 Child2 1
5 Child3 2
6 Child4 1
Category.php(模型)
var $belongsTo = array(
'ParentGroup' =>
array('className' => 'Category',
'foreignKey' => 'parent_id'
),
);
var $hasMany = array(
'ChildGroup' =>
array('className' => 'Category',
'foreignKey' => 'parent_id'
),
);
admin_edit.ctp
<?php echo $this->Form->create('Category'); ?>
<?php echo $this->Form->input("id" ,array('type' => 'hidden', 'label' => false,'div' => false))?>
<?php echo $this->Form->input("parent_id" ,array('label' => false,'div' => false,'class'=>"text-field" ))?>
<?php echo $this->Form->input("category" ,array('label' => false,'div' => false,'class'=>"text-field" ))?>
<?php echo $this->Form->end('edit'); ?>
注意显示在parent_id选择框中的数据
答案 0 :(得分:0)
//Controller
$categories = $this->Category->find('list', array('conditions'=>array(all your conditions)));
$this->set('categories', $categories);
//View
$this->Form->input('category_id');
//you can set the type if it doen't work
$this->Form->input('category_id', array('type'=>''));
你把父母,孩子的关联放在一个模型中,
belongs_to必须在子模型
中父模型和has_many应该进入父
//Category Model
var $belongsTo = array(
'ChildGroup' =>
array('className' => 'ChildGroup',
'foreignKey' => 'parent_id'
),
);
//Child Model
var $hasMany = array(
'ParentGroup' =>
array('className' => 'ParentGroup',
'foreignKey' => 'parent_id'
),
);
如果您的表名与您的模型名称http://book.cakephp.org/2.0/en/models.html
不同,请阅读此文章以获取模型关联http://book.cakephp.org/2.0/en/models.html和usetable