Cakephp 2.0:使用FormHelper更改选择项文本而不修改ID

时间:2012-03-28 17:06:35

标签: cakephp formhelper

我确信这很简单,但我无法弄清楚如何实现它:

我的模型包含titlesub_title列;频繁的条目具有相同的title,并且只能由sub_title区分。所以,我需要CRUD命令的视图才能有效地显示:

<option>Title: Subtitle</option>

但我不知道如何让FormHelper这样做。有人可以为此通用形式提供策略(即:

echo $this->Form->input('title', array(
                                        //whatever option(s) solve this 
                                       ));

非常感谢解决方案或建议!

1 个答案:

答案 0 :(得分:1)

使用Set类:

设置不同的选项
$results = $this->Model->find('all');
$options = Set::combine($results, '{n}.Model.id', array('{0}: {1}', '{n}.Model.title', '{n}.Model.sub_title'));

所以你的选项数组看起来像

array(
  0 => 'Title: sub title',
  1 => 'Title: different sub'
);

并设置下拉列表以使用这些选项

$this->Form->input('title', array('options' => $options));