php - Cake PHP 选择下拉列表

标签 php forms cakephp-3.0

我是 CakePHP 的新手,需要一些帮助。我烘焙了我的第一个应用程序,但我无法让它按我想要的方式运行。

我有两个表,玩家和团队,以及一个连接表 players_teams。表格显示正常。但我想将选择区域更改为下拉菜单。随着时间的推移,玩家可能拥有多个团队(我为此在连接表中添加了起始日期和截止日期列),但我希望玩家在创建团队时就被分配到一个团队。

这是我尝试过的:

<?php
echo $this->Form->input('player_name');
        echo $this->Form->input('player_last_name');
        echo $this->Form->input('player_number');
        echo $this->Form->input('player_birthday');
        echo $this->Form->input('teams._ids', [ 'multiple' => false, 'options' => $teams,]);
    ?>

它不会变成下拉菜单。我也试过这个:

echo $this->Form->input('teams._ids',  ['type' => 'select', 'multiple' => false, 'options' => $teams, 'empty' => true]);

最后一个将选择更改为下拉列表,但无论我选择什么都不会进入数据库。

这是玩家 Controller :

public function add()
{
    $player = $this->Players->newEntity();
    if ($this->request->is('post')) {
        $player = $this->Players->patchEntity($player, $this->request->data);
        if ($this->Players->save($player)) {
            $this->Flash->success(__('The player has been saved.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The player could not be saved. Please, try again.'));
        }
    }
    $teams = $this->Players->Teams->find('list', ['limit' => 200]);
    $this->set(compact('player', 'teams'));
    $this->set('_serialize', ['player']);
}

这是 PlayersTeamController:

public function add()
{
    $playersTeam = $this->PlayersTeams->newEntity();
    if ($this->request->is('post')) {
        $playersTeam = $this->PlayersTeams->patchEntity($playersTeam, $this->request->data);
        if ($this->PlayersTeams->save($playersTeam)) {
            $this->Flash->success(__('The players team has been saved.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The players team could not be saved. Please, try again.'));
        }
    }
    $players = $this->PlayersTeams->Players->find('list', ['limit' => 200]);
    $teams = $this->PlayersTeams->Teams->find('list', ['limit' => 200]);
    $this->set(compact('playersTeam', 'players', 'teams'));
    $this->set('_serialize', ['playersTeam']);
}

任何帮助将不胜感激。谢谢!

最佳答案

The Cakebook: 3.0

你试过吗:

echo $this->Form->select(
  'teams'
  ,$teams->toArray()
  ,[
    'multiple' => false,
    'class' => '_ids',
    'empty' => true
  ]
);

关于php - Cake PHP 选择下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33181587/

相关文章:

javascript - 检查提交的电子邮件格式是否正确

angularjs - $http 获取带有表单的请求

Cakephp 3 : How loadModel function defines globally?

PHP:utf-8 编码,htmlentities 给出奇怪的结果

php - 如何使用 PHP 在 excel 单元格中换行?

javascript - 从JS发送数据到Controller中的函数,并从函数中获取结果

javascript - 同时输入reactjs表单填写

php - 加载 xdebug 时 CakePHP 命令行不工作

php - 在 Cakephp 中将两个模型之间的表链接在一起

PHP session 在同一页面中丢失?