CakePHP - Possible to have a select box with multiple fields displayed in the options?
I want the user to be able to choose a restaurant from a dropdown list. I'm achieving this by the simple:
echo $this->Form->input(
'Restaurant', array('multiple'=>false, array('empty' => true)));
The issue I have is: some restaurants have duplicate names, and I need a way for the user to know which is which. I'd like to have the ID and/or address within the select options like:
<li value='62'>开发者_StackOverflow中文版McDonalds (1234 Happy St) - #62</li>
<li value='63'>McDonalds (9876 French Fry Ln) - #63</li>
...etc
Is there a way to do this? I'm obviously capable of doing it w/ normal HTML, but... would be nice to stay in CakePHP.
Thanks ahead of time for any thoughts/suggestions/direction!
When you load your restaurants you're actually getting an array like this
array (
ID => NAME,
ID => NAME
)
Basically, it's an associated array with the ID as the key and the display field as the value. So as long as you modify that array via the find
operation or via normal PHP array iteration, you can achieve your goal.
EDIT
So your answer is CakePHP VirtualFields
In your model you define it as
var $virtualFields = array(
'rest_unique_name' => 'CONCAT(Restaurant.first_name, " ", Restaurant.address)'
);
In your controller you do this
$opts = array(
'fields' => array('id', 'rest_unique_name')
);
$restaurants = $this->Restaurant->find('list', $opts);
精彩评论