Cakephp 3.0 $query->toArray();

Cakephp 3.0 $query->toArray();

嘿,我正在尝试找到一个解决方案,我正在使用此命令从 mysql 检索数据:

$meals = TableRegistry::get('Users');
$query = $meals
->find()
->select(['id' ,'username'])
->where(['role' => 'patient']);
 $data = $query->toArray();

这是我使用 query->toarray() 后的代码,我得到了这个值

 { "id": 4, "username": "s2" }  

我想将此值放入我的表单中,如下所示:

   echo $this->Form->input('user_id', [
        'options' => [1 => 'Admin', 2 => 'Author']
    ]) ;

如何使用foreach获取值作为id和usernmae作为名称任何快速解决方案

使用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