php - Cakephp DropDown with Parent and child -
to begin here example of want achieve using cake-php way create combo-box populated 2 tables categories , subcategories.
-- category1 --- subcategory1 --- subcategory2 --- ... -- category2 --- subcategory1 --- subcategory2 --- ...
in controller tried list of categories subcategories using code bellow didn't work , returning list of subcategories:
$this->set('category',($this->category->subcategory->find('list')));
here associations i'm using between models:
category model
<?php class category extends appmodel{ public $hasmany=array( 'subcategory'=>array( 'classname'=>'subcategories', ) ); }
subcategory model
<?php class subcategory extends appmodel{ public $belongsto=array( 'category'=>array( 'classname'=>'categories', 'foreignkey'=> 'categories_id' ) ); }
i hope question clear , lot.
$options = array(); foreach($this->category->find('all') $key => $category) { $options[$category['category']['name']] = $this->category->subcategory->find('list') } $this->set('category', $options);
Comments
Post a Comment