0

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

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

hey jose I have some issue I am trying to add a hidden input can you suggest me in this where d_id I want to update this column here uer_id get updated but in hidden form it is not doing any action

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.