How to Create OptGroup Drop down list from DataBase


#1

HI,

i want to create a dropdown field with group-values.

My table users has the fields: name and company

What i want to do is:

$users= array(
‘company 1’ => array(
‘name 1’ => ‘name 1’,
‘name 2’ => name 2’,
‘name 3’ => ‘name 3’,
),
‘company 2’ => array(
‘name 1’ => ‘name 1’,
‘name 2’ => name 2’,
‘name 3’ => ‘name 3’,
),
);

How can i create this dropdown field?

many thanks

mitch


#2

See Hash::combine

I think you have to call the $query->toList() or toArray first


#3

https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairs

there is key groupField in options in ->find('list', ...) for this