Hey,
I am working on a project where I want to create a where clause focused on an associations table. The problem is in the following code:
> private function _getLikeArray($columns, $query) {
> $whereColumnQuery = [];
>
> foreach ($columns as $key => $value) {
> $leftHand = $value . ' LIKE';
> array_push($whereColumnQuery, [$leftHand => '%' . $query . '%']);
> }
>
> return $whereColumnQuery;
> }
>
> $columns = ['unique_identifier', 'birth_date', 'social_security_number', 'Users.last_name', 'Users.UserLocations.Locations.name' ]
>
>
> $likeArray = $this->_getLikeArray($columns, $query)
> $full_name = $query->func()->concat([
> 'Users.first_name' => 'identifier', ' ',
> 'Users.insertion' => 'identifier', ' ',
> 'Users.last_name' => 'identifier'
> ]);
>
> $query->where(function($exp, $q) use ($textQuery, $full_name, $likeArray) {
> return $exp->or_($likeArray)->like($full_name, '%' . $textQuery . '%');
> });
The query above has been performed on a table with the following contains:
'contains' => [ 'Users' => function ($q) use ($findType) { return $q->find($findType); }, 'Users.UserLocations.Locations', 'Users.UserDepartments.LocationDepartments' ]
Along with the (probably redundant) leftJoinWith
:
$query->leftJoinWith('Users.UserLocations.Locations');
As you can see, I am dynamically creating an array containing columns which I would like to filter on by the same LIKE condition. All goes right except for the ‘Users.UserLocations.Locations.name’ scenario. This has probably something to do with the fact that the association nesting is rather deep. However, as far as I am concerned this should not be a problem.
Nevertheless, the following error is shown:
Error: [PDOException] SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.name like ‘%Loca%’ OR (CONCAT(Users.first_name, ’ ', Users.insertion, ’ ‘, User’ at line 1
Anyone any ideas?
Thank you for your help!