我是 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']);
}
任何帮助将不胜感激。谢谢!
最佳答案
你试过吗:
echo $this->Form->select(
'teams'
,$teams->toArray()
,[
'multiple' => false,
'class' => '_ids',
'empty' => true
]
);
关于php - Cake PHP 选择下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33181587/