模板Form元素将是:
$ingredientOptions = array('Potato','Peanut','Parsley');
echo $this->Form->input('FavouriteIngredients', array('multiple' => true, 'options' => $ingredientOptions);
echo $this->Form->input('AllergicIngredients', array('multiple' => true, 'options' => $ingredientOptions);
这会创建两个(丑陋)多个选择框,每个选择框都包含一个由options数组中的元素组成的列表,其中没有一个被选中。
要设置初始值,我在控制器中尝试了一些事情但没有成功:
$this->set('FavouriteIngredients',array(1,2));
$this->request->data['FavouriteIngredients'] = array(1,2);
这些多项选择适用于客户模型中定义的两个HABTM关系:
class Customer extends AppModel {
public $hasAndBelongsToMany = array(
'FavouriteIngredients' =>
array(
'className' => 'Ingredient',
'joinTable' => 'customer_ingredients_favourite'
);
'AllergicIngredients' =>
array(
'className' => 'Ingredient',
'joinTable' => 'customer_ingredients_allergic'
);
);
}
答案 0 :(得分:0)
实际上,生产中的问题是选项数组中没有值。所以这有效:
$this->request->data['Customer']['FavouriteIngredients'] = array(1,2);