【发布时间】:2016-01-15 20:41:42
【问题描述】:
我是 CakePHP 的新手,需要一些帮助。我烘焙了我的第一个应用程序,但我无法让它按我想要的方式运行。
我有两个表,players 和 teams,还有一个 join 表 player_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]);
最后一个将选择更改为下拉菜单,但无论我选择什么都不会进入数据库。
这是玩家的控制器:
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']);
}
任何帮助将不胜感激。谢谢!
【问题讨论】:
标签: php forms cakephp-3.0