If you called the virtual field 'name,' wouldn't the automagic take
care of everything else (the select box) from there?

On Dec 17, 8:26 pm, cricket <zijn.digi...@gmail.com> wrote:
> On Fri, Dec 17, 2010 at 6:25 PM, eldorado2768
>
>
>
> <davidbrotman2...@gmail.com> wrote:
> > I am having a lot of difficulty understanding how to get my select box
> > to display properly. I have a table called owners and a related table
> > called buildings with the owner_id as the foreign key. I figured out
> > how cakephp looks for a field called name and then if it doesn't find
> > it will just default to listing the id in the option select box. What
> > I want to display is the last_name + "," + first_name and list them
> > alphabetically by last name.
>
> > I figured out that the find() command could help me accomplish this.
> > Using cake bake console I created the default controllers for
> > buildings and owners. In the add() and edit() functions in the
> > building controller, I found the following find() command:
>
> > $owners = $this->Building->Owner->find('list');
>
> > Using just this command pulls up the id only in the drop down. I am
> > trying to get the first and last name to show so I modified it to be:
>
> > $owners = $this->Building->Owner->find('list',
> >                   array('fields' =>
> > array('Owner.id','Owner.last_name','Owner.first_name')),
> >                   array('order' => array('Owner.last_name ASC'))
> >                   );
>
> > In the view file, I just have the following form command:
>
> > echo $form->input('owner_id');
>
> > The result is kind of strange. I have 2 records for owners:
> > Mitch Friedman
> > Bob Miller
>
> > I am having somewhat of a success since the two owners are listed but
> > they are stacked on top of each other so the dropdown looks like this:
> > Mitch
> > Friedman
> > Bob
> > Miller
>
> > Anyone have any suggestions on how to at least get the names to
> > display properly? Ideally I would like to see  last_name + "," +
> > first_name but if I can end up with a normal looking list that isn't
> > stacked up like they are, that would be good enough.
>
> You can create a virtual field[1] in the model.
>
> public $virtualFields = array(
>     'full_name' => 'CONCAT(User.last_name, ", ", User.first_name)'
> );
>
> Then, I believe (haven't tried this) that you could specify full_name
> as the model's display field:
>
> public $displayField = 'full_name';
>
> $owners = $this->Building->Owner->find(
>     'list',
>     array(
>         'order' => array('Owner.last_name' => 'ASC')
>     )
> );
>
> [1]http://book.cakephp.org/view/1608/Virtual-fields

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to