【问题标题】:How can one set selected options for a cake php FormHelper multiple select?如何为蛋糕 php FormHelper 多项选择设置选定选项?
【发布时间】: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


    【解决方案1】:

    实际上,生产中的问题是选项数组中不存在这些值。所以这行得通:

    $this->request->data['Customer']['FavouriteIngredients'] = array(1,2);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 2014-11-18
      • 2019-09-05
      • 2020-08-29
      • 2016-10-01
      • 2015-07-08
      相关资源
      最近更新 更多