Display multiple elements in a select field using generateList

From Gruff Goat Wiki
Jump to: navigation, search

It is quite simple. Let's say you have a model called Person, and you want to set up a virtual field called "full_name", which will be comprised of the "first_name" and "last_name" fields. (I will assume you're using generateList to create the select list array.)

class Person extends AppModel {

    function afterFind($results) {
         foreach ($results as $key => $val) {
            $results[$key]['Person']['full_name'] =
$val['Person']['first_name'] . ' ' . $val['Person']['last_name'];
        }
        return $results;
    }

}

$this->Person->generateList(null, null, null, null,
"{n}.Person.full_name");

That's all there is to it.

From Nate (link)