Reputation: 115
I wanted to prefill a multiple Checkbox form with Data from MySql Database.
My Database Data :
id customer_id language_id preferred
1 10 150 yes
2 10 149 yes
The query in Controller "Preferredcustomerlanguages"
$clientlanguages = $this->Preferredcustomerlanguages
->find('list')
->select(['language_id'])
->where(['customer_id =' => $customer_id])
->where(['preferred =' => 'yes'])
->toArray()
;
If i start the query with $customer_id = 10 the result is this:
[
(int) 2 => (int) 2,
(int) 1 => (int) 1
]
I thought the query would give me a list of the language_id which i need to prefill my form.
Maybe someone can give me a hint where iḿ thinking wrong
Upvotes: 2
Views: 11744
Reputation: 1158
If you want to select a field different to displayField
, you can use the list arguments for this:
$clientlanguages = $this->Preferredcustomerlanguages
->find('list', [
'keyField' => 'id',
'valueField' => 'language_id'
])
->where(['customer_id =' => $customer_id])
->where(['preferred =' => 'yes'])
->toArray();
https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairs
Upvotes: 10