Cakephp 3.0 $query->toArray();

岁酱吖の 提交于 2019-12-12 03:13:36

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/29362318/cakephp-3-0-query-toarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!