Multi Field Dropdown in Cakephp.

You get the picture – your database stores Firstname and Surname but you want Full Name to appear in a drop down.  The hardest part for me was finding the solution because nothing seemed to list it appropriately under Google.

Eventually I found a simple solution using virtual fields and here it is.

In your model, my example uses Student, put the following:

var $virtualFields = array('fullname' => 'CONCAT(Student.forename, " ", Student.surname)');

Now in your controller create the data array with the following:

$options = array(
    "fields" => array("id", "fullname"),
    "order" => array("Student.fullname ASC"),
    "conditions" => array('Student.published' => '1')
);
$students = $this->Student->find('list', $options);

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.