phpcakephpselectcakephp-bake

CakePHP select option attributes


I'm new with CakePHP and I tried to do an admin panel.

In my dropdown list I can't show the 'label' instead of the id. I tried so many things...

My database is like :

etats

projets

I tried to do this :

Controller :

$etats = $this->Projets->Etats->find('all');

View :

echo $this->Form->input('etat_id', ['options' => $etats]);

Output :

<select name="etat_id" id="etat-id">
  <option value="0">
    {"id": 1, "label": "En cours"}
  </option>
</select>

Now, if i try to do this :

Controller :

$etats = $this->Projets->Etats->find('list', array('fields' => array('Etats.id', 'Etats.label')));

(same view)

The output is :

<select name="etat_id" id="etat-id">
  <option value="1">
    1
  </option>
</select>

It doesn't want to take the 'label' in my DB... I don't know what to do.

Thank you in advance and sorry again for this poor english :'(


Solution

  • Ok, i found it!

    $etats = $this->Projets->Etats->find('list', ['keyField' => 'id', 'valueField' => 'label']);
    

    Will do :

    <option value="ID">LABEL</option>