Reputation: 142
Hey I am trying to find a solution I am retrieving the data from mysql using this command :
$meals = TableRegistry::get('Users');
$query = $meals
->find()
->select(['id' ,'username'])
->where(['role' => 'patient']);
$data = $query->toArray();
This is my code after using query->toarray() I am getting this value
{ "id": 4, "username": "s2" }
I want to put this value in my form which is like this :
echo $this->Form->input('user_id', [
'options' => [1 => 'Admin', 2 => 'Author']
]) ;
How to use foreach to get value as id and usernmae as name any quick solution
Upvotes: 0
Views: 3225
Reputation: 9614
Use find('list')
$users = TableRegistry::get('Users')
->find('list', ['valueField' => 'username'])
->select(['id' ,'username'])
->where(['role' => 'patient']);
$this->set('users', $users);
echo $this->Form->input('user_id', [
'options' => $users
]);
http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairs
Upvotes: 3