【发布时间】:2015-08-17 16:48:26
【问题描述】:
模板表单元素是:
$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);
这会创建两个(丑陋的)多个选择框,每个选择框都有一个由选项数组中的元素组成的列表,没有一个被选中。
为了设置初始值,我在控制器中尝试了一些东西但没有成功:
$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'
);
);
}
【问题讨论】:
标签: php cakephp cakephp-2.0