2011-11-20 121 views

回答

0

假設我正確理解你的問題,你想使用optgroup標籤?

我不確定是否有一個automagical方式做這個時只使用belongsTo關係。下面的解決方案是有點難看,但你想要做什麼:

控制器:

/** 
* Get all categories and subcategories and declare selectBox array 
*/ 
$categories = $this->Subcategory->Category->find('all'); 
$selectBox = array();  

/** 
* Iterate over categories & subcategories creating a formatted 
* array that works with Form->input() to create a select box with 'optgroups' 
*/ 
foreach ($categories as $category) { 
    foreach ($category['Subcategory'] as $subcategory) {   
    $selectBox[$category['Category']['title']][$subcategory['id']] = $subcategory['title'];   
    } 
} 

$this->set('selectBox', $selectBox);  

查看:

<?php echo $this->Form->input('subcategory_id', array('options' => $selectBox)); ?> 
相關問題