cakephp查找列表 [英] cakephp find list

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

问题描述

您好我想能够使用find生成列表,以便我可以在选择帮助中使用。但有一个问题。我想太fetch id,名称(第一+最后)。所以我怎么能实现它。我想要first_name和last_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.

推荐答案

我认为这可以使用你的模型中的virtualFields和displayField var。

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

在你的模型中,为这个全名定义一个虚拟域,如下所示:

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

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

如果你现在将displayField设置为full_name,你应该能够得到一个你的用户列表$

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.

var $displayField = 'full_name';

自动提取ID。

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

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