cakephp – problem with form helper and list – Education Career Blog

I want to make a SELECT item with the result of a query, I have this in the view:

echo $this->Form->input('person_id');

I want the value to be the person’s id, and display the concatenation of firstname and lastname, like so:

<select id="ResearchGroupPersonId" name="dataResearchGroupperson_id">
    <option value="2">lastname2 firstname2</option>
    <option value="1">lastname1 firstname1</option>
</select>

I tried this is the controller without success

$people = $this->ResearchGroup->Person->find('list', 
    array(
        'fields' => array('Person.id', 'CONCAT (Person.lastname, " ", Person.firstname) AS fullname'), 
        'order' => array('Person.lastname'),
        )
    );

the sql log show that rows are retreived, they just don’t get displayed. It works witout the concatenation, this:

    $people = $this->ResearchGroup->Person->find('list', 
    array(
        'fields' => array('Person.id', 'Person.lastname'), 
        'order' => array('Person.lastname'),
        )
    );

gives this

<select id="ResearchGroupPersonId" name="dataResearchGroupperson_id">
    <option value="2">Doe</option>
    <option value="1">Ray</option>
</select>

Why’s that?

,

That is a know problem. The easiest solution I have found and that I use (for v1.2) is here – http://nuts-and-bolts-of-cakephp.com/2008/09/04/findlist-with-three-or-combined-fields/.

You will see a link to the cookbook at the top if you use v1.3.

,

For CakePHP 1.3 there are Virtual Fields in the model. Check this link

Leave a Comment