cakephp 查找列表 [英] cakephp find list

查看:22
本文介绍了cakephp 查找列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用 find 生成一个列表,以便我可以在选择助手中使用.但有一个问题.我也想获取 id,name(first + last).那么我怎样才能实现它.我希望 first_name 和 last_name 作为 name 加入.我怎样才能实现它.

Hi I want to be able to generate a list using find so that I can use in select helper. but there is a problem. i want too fetch id,name(first + last). so how can I achieve it. I want first_name and last_name to be joined as name . How can I achieve it.

$this->User->find('all',array('fields' => array('first_name','last_name','id')));

我不能使用模型过滤器和回调 请建议我如何在控制器本身中做到这一点.

I can't use model filters and callback Please suggest me how can I do it in controllers itself.

推荐答案

我认为这可以使用模型中的 virtualFieldsdisplayField 属性来完成.

I think this can be done using the virtualFields and displayField properties in your model.

在您的模型中,为全名定义一个虚拟字段,如下所示:

In your model, define a virtual field for the full name like this:

public $virtualFields = array(
    'full_name' => 'CONCAT(User.first_name, " ", User.last_name)'
);

如果您现在将 displayField 设置为 full_name,您应该能够使用 $this->User->find('list') 方法获取您的用户列表,您可以使用该方法表单助手没有问题.

If you now set displayField to full_name you should be able to get a list of your users with the $this->User->find('list') method which you can use without problems with the Form-helper.

public $displayField = 'full_name';

... 或:

public $displayField = 'User.full_name';

自动获取id.

这篇关于cakephp 查找列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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