Reputation: 97
Im new to Cake PHP and this forum.
This is my question. I just want to create a select box using cake PHP. Data is coming from database table.
My Customers table looks like this :
id | first_name | last_name
--------------------------------
1 | John | Doe
--------------------------------
1 | Sam | Doe
I just tried to get the data using this method
$this->set('customers', $this->Customer->find('list', array(
'fields' => array('Customer.id', 'Customer.first_name')
)));
View code :
echo $this->Form->input('customers');
This create a select box just like this:
<select name="data[Order][customers]" id="OrderCustomers">
<option value="1">John</option>
<option value="2">Sam</option>
</select>
My question is how do I display first name & last name in select box just like below.
<option value="1">John Doe</option>
<option value="2">Sam Doe</option>
Much appreciate your answers! Thanks
Upvotes: 2
Views: 4042
Reputation: 6066
You can use virtual fields
The model would have
public $virtualFields = array(
'full_name' => 'CONCAT(Customer.first_name, " ", Customer.last_name)'
);
Then you can use it in the find
call:
$this->set('customers', $this->Customer->find('list', array(
'fields' => array('Customer.id', 'Customer.full_name')
)));
Upvotes: 7