Cakephp 2.3:如何不使用'id'作为$ belongsTo的标签< select>选项 [英] Cakephp 2.3: How to not use 'id' as the label of $belongsTo <select> options

查看:198
本文介绍了Cakephp 2.3:如何不使用'id'作为$ belongsTo的标签< select>选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到这个困境与Cakephp很多,我很漂亮确定这是一个结果,我不跟踪/理解一个约定在某个地方。在任何种类的 belongsTo (hasAnd或其他),我一般发现,由于 id 往往是外键,它也最终是在下拉菜单中显示的,这当然是语义上无用的。



这是我通常做的一个例子;我希望有人可以让我直接:(我只是弥补了这些表,因为他们为一般问题的一个很好的模板。)



假设:



用户表有列 id | firstname | lastname



帐户 id | user_id



>



使用蛋糕烘焙全部,我的 Accounts / add 视图构建了一个下拉列表,用于显示ID号的 user_id 我发现了几个方法,但他们都涉及基本上组装的信息自己。我或者通过在控制器中构建 user_id 下拉菜单 $ options c $ c> Accounts / add



控制器 >

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

$ options = Set :: combine($ users,'{n} .User.id',array('{0} {1} ,'{n} .User.firstname','{n} .User.lastname'));



>查看



echo $ this-> Form-> input b $ b'div'=> array('id'=>'UserId'),
'options'=> $ options,'type'=>'select'
) 。\\\
\\\
;



或者:



控制器



$ users = $ this-> Account-> User - > find('all',array('recursive'=> 1));



然后以几乎相同的方式解析View中的数据。在任何一种情况下,我总是有确定的,我可以建立我的模型,以解决这一点 - 它似乎太基本的需要没有被这样广泛的框架解决。

解决方案

对于单列表,简单的$ displayField ='column_name'就是答案。



但是对于列的连接,还有一些代码涉及:

  public $ virtualFields = array(full_name=>CONCAT(first_name,'',last_name)); 
public $ displayField ='full_name';

如果它的一个真正复杂的$ displayField,一个afterFind解决方案可能是必要的,但在大多数情况下,这是解决方案。



希望有帮助。


I run into this dilemma a lot with Cakephp, and I'm prettty sure it's a consequence of my not following/understanding a convention somewhere. In any kind of belongsTo (hasAnd or otherwise), I generally find that since id tends to be the foreign key, it also ends up being what is displayed in dropdown menus, and this is of course semantically useless.

Here's an example of what I've generally done; I'm hoping someone can set me straight: (I'm just making up these tables because they make a good template for the general problem.)

Suppose:

Users table has columns id|firstname|lastname

Accounts has id|user_id

(nothing I'm doing is this simple, but this is the general form of the problem)

Using cake bake all, my Accounts/add view builds a dropdown for user_id that literally displays ID numbers. I have found a couple of ways around this, but they all involve basically assembling the information myself. I've either done it by building, in the controller, an $options array for the user_id dropdown of Accounts/add like so:

Controller

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

$options = Set::combine($users, '{n}.User.id', array('{0} {1}', '{n}.User.firstname', '{n}.User.lastname'));

View

echo $this->Form->input('user_id', array( 'div' => array( 'id' =>'UserId'), 'options'=>$options,'type'=>'select' ) )."\n\n";

Or else by:

Controller

$users = $this->Account->User->find('all', array('recursive' => 1) );

And then parsing the data at the View in almost the same fashion. In either case, I've always had the lingering certainty that I could be building my models in such a way that addresses this—it just seems like too basic a need to have not been addressed by such an extensive framework.

解决方案

For single column tables, a simple $displayField = 'column_name' is the answer.

But for concatenations of columns, there's a bit more code involved:

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

If its a really complicated $displayField, an afterFind solution may be necessary, but for most cases this is the solution.

Hope that helps.

这篇关于Cakephp 2.3:如何不使用'id'作为$ belongsTo的标签< select>选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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