2015-10-17 234 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()命令保存爲其關聯的一個級別。