CakePHP表单选择值为table.id,选择选项文本为table.textfield [英] CakePHP form select value as table.id, select option text as table.textfield

查看:86
本文介绍了CakePHP表单选择值为table.id,选择选项文本为table.textfield的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Cake的新人。我已经设置了一些东西,相关的用户和配置文件表,控制器,模型和视图。在我的 profiles / add 视图中,我试图输出一个 select 输入值为 user.id ,选项文本为 user.username 。最终, user.id 的值将保存在 profile.user_id 中。



我已经阅读了许多文档,并成功地设置了 select ,以从用户表,但它使用 user.id 作为值和选项文本。



控制器:

  $ this-> set('users',$ this-> Profile-> User-> find('list')); 

检视:

 code> echo $ this-> Form-> input('user_id'); 

看起来像这样的事情是常规的,所以随时回答只是一个链接



谢谢!

解决方案

blockquote>

成功地设置了select从用户表中输出选项,但它使用user.id作为值和选项文本。




find('list')将默认使用主键作为键,模型的 displayField 作为值。



如果模型没有明确的 displayField ,但有一个名为 / code>或 name Cake将自动选择此字段,否则它不会猜测,只是使用主键字段 - 这是问题中发生了什么。



不过,您可以在 find('list')调用的结果中指定所需的字段。因此,要么:



设置模型displayField



  class User extends AppModel {

public $ displayField ='username';

}

,然后使用:

  $ users = $ this-> User-> find('list'); 



明确选择字段



 $ users = $ this-> User-> find('list',array(
'fields'=> array('id','username')
));

在这两种情况下,结果将是:


$ b b

  array(
1 =>'firstuser',
...
76 =>'AD7six'


I'm new to Cake. I've gotten some things set up, relevantly a "users" and a "profiles" table, controller, model and view. In my profiles/add view, I'm attempting to output a select input with the value being the user.id and the option text being the user.username. Ultimately, the value user.id will be saved in profile.user_id.

I have read through a lot of documentation, and successfully set up the select to output the options from the user tables, but it uses the user.id as both the value and the option text.

Controller:

    $this->set('users', $this->Profile->User->find('list'));

View:

    echo $this->Form->input('user_id');

It seems like this sort of thing would be routine, so feel free to answer with just a link to the documentation if I've overlooked it completely.

Thanks!

解决方案

successfully set up the select to output the options from the user tables, but it uses the user.id as both the value and the option text.

find('list') will by default use the primary key as the key, and the model's displayField as the value.

If a model does not have an explicit displayField but has a field named title or name Cake will automatically pick this field, otherwise it will not guess and simply use the primary-key field - which is what's happening in the question.

You can however simply specify which fields you want in the results of your find('list') call. Therefore either:

Set the model displayField

class User extends AppModel {

    public $displayField = 'username';

}

and then use:

$users = $this->User->find('list');

Explicitly choose the fields

$users = $this->User->find('list', array(
    'fields' => array('id', 'username')
));

In both cases - the result will be e.g.:

array(
    1 => 'firstuser',
    ...
    76 => 'AD7six'
)

这篇关于CakePHP表单选择值为table.id,选择选项文本为table.textfield的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆