2015-10-17 168 views
3

我是CakePHP的新手,需要一些帮助。我烘烤了我的第一个应用程序,但我无法使其表现得如我所愿。蛋糕PHP选择下拉菜单

我有两个表格,球员和球队,以及一个联盟表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,]); 
    ?> 

它不更改为dropdow。我也试过这样:

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

任何帮助将非常感激。谢谢!

回答

0

The Cakebook: 3.0

你试过:

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

我得到这个:错误:不能使用Cake \ ORM \ Query类型的对象作为数组 –

+0

@MiguelCedenoAgamez我实际上对CakePHP没有任何了解;我只是一个快速的Google员工。对不起 – Terminus

+0

不好运! @Terminus –

0

自己只是类似于所需的数据结构,即

[ 
    'teams' => [ 
     '_ids' => [ 
      // single id here 
     ] 
    ] 
] 

Cookbook > Database Access & ORM > Saving Data > Converting BelongsToMany Data

正确的,基于阵列ISH元素名称

例如,您可以指定合适的元素name属性值做到这一点的,像

echo $this->Form->input(
    'teams', 
    [ 
     'name' => 'teams[_ids][0]' 
     //'options' => $teams // this is optional when following the conventions 
    ] 
); 

,它会自动选择一个select型的基础上的选项。

您将需要一些额外的逻辑来防止进一步的选择被注入,因为像teams[_ids][0]这样的数组名称将允许没有安全组件能够检测到这种篡改。这可以使用验证或通过准备Model.beforeMarshal事件中的数据完成。

这里有一个验证例子

public function validationCreate(Validator $validator) { 
    $validator = $this->validationDefault($validator); 

    $teamsValidator = (new Validator()) 
     ->requirePresence('_ids') 
     ->notEmpty('_ids') 
     ->add('_ids', 'valid', [ 
      'rule' => function($value) { 
       return is_array($value) && count($value) === 1; 
      } 
     ]); 

    $validator 
     ->requirePresence('teams') 
     ->notEmpty('teams') 
     ->addNested('teams', $tagsValidator); 

    return $validator; 
} 
$player = $this->Players->patchEntity($player, $this->request->data, [ 
    'validate' => 'create' 
]); 

定制,单值元素名称

另一选项将是指定非阵列上下的名称,与在Model.beforeMarshal事件准备数据相结合,像

echo $this->Form->input('teams', ['name' => 'initial_team']); 
public function beforeMarshal(Event $event, \ArrayObject $data, \ArrayObject $options) 
{ 
    if (isset($data['initial_team'])) { 
     $data['teams'] = [ 
      '_ids' => [ 
       $data['initial_team'] 
      ] 
     ]; 
    } 
} 

Cookbook > Database Access & ORM > Saving Data > Modifying Request Data Before Building Entities

注意

两者都会保留CakePHP的魔法,就像编组人员将传递的_ids转换为实体,以便正确保存关联,自动获取输入选项,自动选择输入类型等。

0

如果您已经制作正确的associations,你可以直接从PlayersController中保存。 PlayersTeamController根本不需要。当您使用请求数据修补新创建的播放器entity时,关联的表格数据将自动进入并由实体save()命令保存为其关联的一个级别。