BelongsTo在cakephp和html选择的问题,我不明白如何做到这一点 [英] BelongsTo problem in cakephp and html select, i can't understand how to do that

查看:117
本文介绍了BelongsTo在cakephp和html选择的问题,我不明白如何做到这一点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cakephp noob的简单问题:

Simple question by a cakephp noob:

我有两个模型,Player和Team。

i have two models, Player and Team.

具有id(int)和cool_name(varchar)。

Team has an id (int) and a cool_name (varchar).

玩家有一个id(int),cool_name(varchar) (int)。

Player has an id (int), a cool_name (varchar) and a reference for the team table, team_id (int).

Cool_name而不是名称,因为我没有英文表格,所以我不能使用字段'name'。

Cool_name instead of name because i don't have tables in english, so i can't use the field 'name'.

我的播放器模型:

所以,一个团队有很多玩家和一个玩家belongsTo团队。 b $ b

My Player model:

class Player extends AppModel {
    var $name = 'Player';
    var $belongsTo = array('Team');
}

(团队模型除了name以外没有别的东西)

(Team model has nothing other than name inside)

PlayersController:

PlayersController:

class PlayersController extends AppController {
    var $name = 'Players';

    function add() {
          $this->set('teams', $this->Player->Team->find('list'));
   //then save... 
    }

播放器:

//...
echo $form->input('squadra_id');
//...

我不想要id,但是团队的名称,然后保存id:像(在html中)

Ok so i have a select with the team ids inside; i don't want the id, but the name of the team and then save the id: something like (in html)

<select name="data[Player][team_id]" id="PlayerTeamId">
<option value="1">Cool Name 1</option>

<option value="2">Cool Name 2</option>
<!-- -->
<option value="{team id}">{team cool name}</option>
</select>

我该怎么办?


  • 解决方案 -

  • Solution -

而不是

$this->set('teams', $this->Player->Team->find('list'));

放入

$this->set('teams', $this->Player->Team->find('list', array('fields' => array('cool name') ) ) );


推荐答案

> 模型,并且该模型具有名称,为您的属性(CakePHP默认识别为显示字段),你应该得到的是一个关联数组,其中关键是团队ID,值是团队名称。与你在一个名为 teams 的变量中设置该数组的事实和Cake将为你工作。

Since you're doing a find( 'list' ) against your Team model and that model has a name property (which CakePHP recognizes as a display field by default), what you should be getting back is an associative array where the key is the team ID and the value is the team name. Couple that with the fact that you're setting that array in a variable named teams and Cake will do the work for you.

在您的表单中,我不确定'squadra_id'来自哪里,但将其更改为'teams',您应该会看到选择列表填充很好:

In your form, I'm not sure where 'squadra_id' came from, but change that to 'teams' and you should see the select list populate nicely:

echo $form->input( 'teams' );

如果我正确理解你,你有一切正确的启用这个魔法,除了输入定义在您的视图。您可以详细了解如何使用 find('list')自动填充 http://book.cakephp.org/view/189/Automagic-Form-Elements

If I understand you correctly, you have everything correct to enable this "magic" except for the input definition in your view. You can read more about how to use find( 'list' ) to automagically populate select boxes at http://book.cakephp.org/view/189/Automagic-Form-Elements.

希望这有助。

这篇关于BelongsTo在cakephp和html选择的问题,我不明白如何做到这一点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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