2014-03-07 93 views
0

如何选择父类别中选择框在admin_edit.ctp如何选择父类别中选择框在admin_edit.ctp

分类表stuture

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
//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'=>'')); 

Form Helper

你把两家母公司的关联,儿童为一体的模式,

是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和usetable如果要是从型号名称http://book.cakephp.org/2.0/en/models.html

+0

但如何对不同的选择父类的对应子类的表名? –

+0

你可以包含行为|条件是否你的模型是正确关联的 – Fury

+0

但我错误错误:模型ChildGroup的表child_groups在数据源中未找到默认 –