属于 cakephp 和 html select 中的问题,我不明白该怎么做

发布于 2024-08-02 21:33:56 字数 1437 浏览 7 评论 0 原文

cakephp菜鸟提出的简单问题:

我有两个模型,玩家和团队。

Team 有一个id (int) 和一个cool_name (varchar)。

玩家有一个 id (int)、一个 Cool_name (varchar) 和一个团队表引用 team_id (int)。

Cool_name 而不是 name 因为我没有英文表,所以我不能使用字段“name”。

所以,一个球队有很多球员并且一名球员属于球队。

我的玩家模型:(

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

团队模型除了里面的名称之外什么都没有)

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,而是团队的名称,然后保存 id:类似于(在 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>

我该怎么办?

  • 解决方案 -

而不是

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

把这个

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

Simple question by a cakephp noob:

i have two models, Player and Team.

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

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

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

So, a team hasMany players and a player belongsTo team.

My Player model:

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

(Team model has nothing other than name inside)

PlayersController:

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

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

In the add view of the player:

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

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>

How can i do?

  • Solution -

instead of

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

put this

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2024-08-09 21:33:56

由于您正在针对您的 Team 模型执行 find( 'list' ) 并且该模型具有 name 属性(CakePHP 将其识别为默认情况下显示字段),您应该返回的是一个关联数组,其中键是团队 ID,值是团队名称。再加上您将该数组设置在名为 teams 的变量中,Cake 将为您完成这项工作。

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

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

如果我明白你是对的,除了你视图中的输入定义之外,你拥有启用此“魔法”的所有正确内容。您可以阅读有关如何使用 find( 'list' ) 自动填充选择框的更多信息,网址为 http://book.cakephp.org/view/189/Automagic-Form-Elements

希望这有帮助。

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.

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' );

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.

Hope this helps.

停滞 2024-08-09 21:33:56

数据未通过“belongsTo”模型进行验证!我可以使用 team_id 并且它有效!

The data is not getting validated with the belongsTo model ! I can use and team_id and it works !!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文