FriendsOfCake / search

CakePHP: Easy model searching

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't search in associated Model

MiloII opened this issue · comments

First of all i would like to thank you all for the work you have done.

Back to the point, I'm having trouble including associated model fields in the search.
My goal is to have the ability in Users/index to search for Roles.name as well. Users and Roles are associated as ManyToMany.

Here it is the config i used:

$this->searchManager()
  ->add('search', 'Search.Like', [
      'before' => true,
      'after' => true,
      'fieldMode' => 'OR',
      'comparison' => 'LIKE',
      'wildcardAny' => '*',
      'wildcardOne' => '?',
      'fields' => [
          'Users.id',
          'Users.password',
          'Users.email',
          'Users.first_name',
          'Users.last_name',
          'Users.profile_img',
          'Roles.name',
      ],
      'beforeProcess' => function (\Cake\ORM\Query $query, array $args, \Search\Model\Filter\Base $filter) {
          $query->contain(['Roles']);
      },
      'colType' => [
          'Users.id' => 'string',
          'Users.password' => 'string',
          'Users.email' => 'string',
          'Users.first_name' => 'string',
          'Users.last_name' => 'string',
          'Users.profile_img' => 'string',
      ],
  ]);

Anyway when try to search i got:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Roles.name' in 'where clause'

I have tried to take a look at the query object before and after the instruction:
$query->contain(['Roles']);

What I noticed is that after the contain statement, Roles.name is added as condition in the WHERE clause but the JOIN with the Roles table is not written and Roles.name is not present in the SELECT fields list. Here the full SQL generated (after adding contain Roles to $query in beforeProcess):

SELECT 
  Users.id AS Users__id, 
  Users.uuid AS Users__uuid, 
  Users.email AS Users__email, 
  Users.password AS Users__password, 
  Users.gender AS Users__gender, 
  Users.first_name AS Users__first_name, 
  Users.last_name AS Users__last_name, 
  Users.profile_img AS Users__profile_img, 
  Users.active AS Users__active, 
  Users.last_login AS Users__last_login, 
  Users.locked AS Users__locked, 
  Users.locked_time AS Users__locked_time, 
  Users.login_attempts AS Users__login_attempts, 
  Users.created AS Users__created, 
  Users.creator_id AS Users__creator_id, 
  Users.modified AS Users__modified, 
  Users.modifier_id AS Users__modifier_id, 
  Modifiers.id AS Modifiers__id, 
  Modifiers.uuid AS Modifiers__uuid, 
  Modifiers.email AS Modifiers__email, 
  Modifiers.password AS Modifiers__password, 
  Modifiers.gender AS Modifiers__gender, 
  Modifiers.first_name AS Modifiers__first_name, 
  Modifiers.last_name AS Modifiers__last_name, 
  Modifiers.profile_img AS Modifiers__profile_img, 
  Modifiers.active AS Modifiers__active, 
  Modifiers.last_login AS Modifiers__last_login, 
  Modifiers.locked AS Modifiers__locked, 
  Modifiers.locked_time AS Modifiers__locked_time, 
  Modifiers.login_attempts AS Modifiers__login_attempts, 
  Modifiers.created AS Modifiers__created, 
  Modifiers.creator_id AS Modifiers__creator_id, 
  Modifiers.modified AS Modifiers__modified, 
  Modifiers.modifier_id AS Modifiers__modifier_id, 
  Creators.id AS Creators__id, 
  Creators.uuid AS Creators__uuid, 
  Creators.email AS Creators__email, 
  Creators.password AS Creators__password, 
  Creators.gender AS Creators__gender, 
  Creators.first_name AS Creators__first_name, 
  Creators.last_name AS Creators__last_name, 
  Creators.profile_img AS Creators__profile_img, 
  Creators.active AS Creators__active, 
  Creators.last_login AS Creators__last_login, 
  Creators.locked AS Creators__locked, 
  Creators.locked_time AS Creators__locked_time, 
  Creators.login_attempts AS Creators__login_attempts, 
  Creators.created AS Creators__created, 
  Creators.creator_id AS Creators__creator_id, 
  Creators.modified AS Creators__modified, 
  Creators.modifier_id AS Creators__modifier_id 
FROM 
  users Users 
  LEFT JOIN users Modifiers ON Modifiers.id = (Users.modifier_id) 
  LEFT JOIN users Creators ON Creators.id = (Users.creator_id) 
WHERE 
  (
    Users.id like '%Developer%' 
    OR Users.password like '%Developer%' 
    OR Users.email like '%Developer%' 
    OR Users.first_name like '%Developer%' 
    OR Users.last_name like '%Developer%' 
    OR Users.profile_img like '%Developer%' 
    OR Roles.name like '%Developer%'
  ) 
LIMIT 
  20 OFFSET 0

Am i missing something or maybe i misread the docs?

Thank you in advance.

commented

The ORM doesn't create a join when containing a belongstomany associated model.

Closing as your issue isn't directly related to the plugin. Please use one of the CakePHP forums to learn how to generate the query you need.