【问题标题】:CakePHP select input : is setting a display member and a value member possible?CakePHP 选择输入:是否可以设置显示成员和值成员?
【发布时间】:2012-07-27 17:29:00
【问题描述】:

我有这个表单输入:

echo $this->Form->input('category_id',array('label'=>'Catégorie','type'=>"select",'options'=>$categories,'empty'=>false));

现在,$category 被这些信息填充了:

Array
(
[0] => Array
    (
        [categories] => Array
            (
                [id] => 1
                [nom] => Autre
            )

    )

[1] => Array
    (
        [categories] => Array
            (
                [id] => 2
                [nom] => Véhicule
            )

    )

[2] => Array
    (
        [categories] => Array
            (
                [id] => 3
                [nom] => Audio/video
            )

    )

)

我希望 Select 输入仅显示“nom”字段(名称),但它会在提交表单时发送 ID。在 CakePHP 中可能吗?我已经在 Google 和 CakePHP 的文档和论坛中爬了一段时间,但没有成功:-/

提前感谢你们,祝你们有美好的一天!

【问题讨论】:

    标签: arrays cakephp select input


    【解决方案1】:

    Cakephp 使用一维数组来选择选项。您将您的 id 作为您的键和 nom 作为选项标签的文本更改为单一维度,然后将其传递给选项键:

    $categories = Set::combine($categories,'{n}.categories.id','{n}.categories.nom');
    

    【讨论】:

      【解决方案2】:

      您应该尊重 Cakephp 的约定。在你的数据库中用“name”替换“nom”,如果你的关联做得很好,cakephp 会自动完成。

      【讨论】:

      • 我会这样做,但遗憾的是,我公司已经建立的惯例不允许我这样做。不过感谢您的回答。
      猜你喜欢
      • 2023-04-10
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多