2016-07-21 4 views
0

AvadiariesTable.php検索(リスト)いくつかのブランクのオプションを返し、検索(すべて)は、正しいデータを返しているが、なぜ{

$this->belongsTo('AlumnesGrups', [ 
     'foreignKey' => 'alumnes_grup_id', 
     'joinType' => 'INNER' 

AlumnesGrupsTable.php

$this->belongsTo('Alumnes', [ 
     'foreignKey' => 'alumne_id', 
     'joinType' => 'INNER' 
    ]); 
    $this->belongsTo('Grups', [ 
     'foreignKey' => 'grup_id', 
     'joinType' => 'INNER' 
    ]); 

でフォーマットAvadiariesController.php:

public function add() 
    { 
    $avadiary = $this->Avadiaries->newEntity(); 
     if ($this->request->is('post')) { 
      $avadiary = $this->Avadiaries->patchEntity($avadiary, $this->request->data); 
      $avadiary->user_id = $this->Auth->user('id'); 
      if ($this->Avadiaries->save($avadiary)) { 
       $this->Flash->success(__('The avadiary has been saved.')); 

       return $this->redirect(['action' => 'index']); 
      } else { 
       $this->Flash->error(__('The avadiary could not be saved. Please, try again.')); 
      } 
     } 
    $alumnesGrups = $this->Avadiaries->AlumnesGrups->find('all', [ 
    'fields' => ['Alumnes.name'], 
    'contain' =>['Alumnes', 'Grups'], 
    'conditions' => ['Grups.id =' => 1], 
    'order' => ['Alumnes.name' => 'ASC'] 
    ]); 
$this->set(compact('avadiary', 'alumnesGrups')); 
     $this->set('_serialize', ['avadiary']); 
    } 

正しいデータが返されますが、次のようにフォーマットされます。

{「名前」:「アンジェラスミス」}}?

私はちょうどアンジェラスミスを得ることができますか? find(all)をfind(list)に変更すると、selectboxには空白のオプションがいくつか表示されます。

ありがとうございました!

答えて

0

使用find('list')とその値が表示したいフィールドのvalueFieldkeyFieldを使用します。

$alumnesGrups = $this->Avadiaries->AlumnesGrups->find('list', [ 
    'keyField' => 'alumne.name', 
    'valueField' => 'alumne.name']) 
->contain(['Alumnes', 'Grups']) 
->where(['Grups.id =' => 1]) 
->order(['Alumnes.name' => 'ASC']); 

チェック: http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairs

+0

はどうもありがとうございましたが、私はすでにこれを試してみました、値がでている保ちますブランク。また、app.phpのデータソースセクションでエンコーディングがOKかどうかを確認しました。 –

+0

私は現在Hash :: extractで試していますが、致命的なエラーが発生します。Class Hash not found。 –

+0

'find( 'list')' – arilia

関連する問題