0

I have a dropdown that only shows the id of a table row. I like to show multiple values from that table to the user in the dropdown.

Do I need to add an array with columnames in the variabele from the controller or something like that?

Edit: In function add I've this variable

$addresses = $this->Users->Addresses->find('list', ['limit' => 200]);

In the view

echo $this->Form->input('address_id', ['options' => $addresses]);

I only want to show the values address_id, city, street, zipcode in the dropdown.

3
  • 1
    can you please add some code? And add some explanation about the desidered behavior? Commented Feb 4, 2016 at 11:23
  • i have added some code Commented Feb 4, 2016 at 11:39
  • @dnns you shouldn't need to tell Cake what the options array is if you use the plural of the model name as you are. i.e. Cake will assume that the variable $addresses populates the options for a foreign key $address_id. So your input code would look like $this->Form->input('address_id');. You should only need to pass the options value when using a variable other than the one Cake is expecting (if that makes sense). :-) Commented Feb 4, 2016 at 12:36

2 Answers 2

5

Use a virtual field:

in your Addresses entity file you can do

function _getFullName()
{
    $ret = $this->city.' '.$this->street.' '.$this->zipcode;
    return $ret;
}

and then in your controller you can do

$addresses = $this->Users->Addresses->find(
    'list', 
    ['limit' => 200, 'valueField' => 'full_name']
);
Sign up to request clarification or add additional context in comments.

4 Comments

This is better than what I suggested +1. Forgot about those.
Nice, I think this is a clean way of doing this. Exactly what I was looking for.
Is it also possible to acces the underlying table of Addresses and place it in $ret?
@dnns I am not entirely sure what you are asking for, maybe it's worth to shoot in another question about that?
0

The Form::Select() only needs one array for it's values to display. It uses the index to define the value and the the value of the array to display.

What you could do is fetch the needed columns from the table and use concat() to create one string from those two columns which you then put into an array.

Alternatively you could take a look at the displayField() in the Table class which accepts an array. The downside of doing this is when you use $table->find('list'); you'll get something along the lines of Sevvlor;1 which is probably not what you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.