2

I m creating options for my dropdown using find with list. But i want to fetch the data of associated table. Given below is my code

$users = $this->UserRoles
    ->find('list', [
        'contain' => ['Users'],
        'limit' => 200,
        'keyField' => 'id',
        'valueField' => 'Users.first_name'
    ])
    ->where([
        'Users.status' => true,
        'Users.company_id' => $company_id,
        'UserRoles.role_id' => 4
    ]);

But it returns me null as output.

ndm
  • 59,784
  • 9
  • 71
  • 110
Akshay Naik
  • 553
  • 3
  • 17
  • Whenever you have problems with associations, please also explain the association setup, I can only assume it's `UserRoles belongsTo Users`. Also please be a little more specific as to where exactly the `null` value occurs, is it `$users` that is `null`, or are the keys and/or values in the list `null`, or...? – ndm Nov 15 '19 at 10:45

1 Answers1

1

The example from the book has the contain query chained as follows:

$query = $articles->find('list', [ 'keyField' => 'id', 'valueField' => 'author.name' ])->contain(['Authors']);

So perhaps try:

$users = $this->UserRoles ->find('list', [ 'limit' => 200, 'keyField' => 'id', 'valueField' => 'Users.first_name' ]) ->contain(['Users']) ->where([ 'Users.status' => true, 'Users.company_id' => $company_id, 'UserRoles.role_id' => 4 ]);

Erebus
  • 1,998
  • 2
  • 19
  • 32