php - How can I use CONCAT in JOIN conditions with CakePHP? -


i'm working on cakephp 3.0 backend have following tables : 'prescriptions' , 'profils' (names french).

'prescriptions' has foreign key directed 'profils' : prescriptions.profils_id.

'profils' has 2 fields can searched : 'nom' , 'prenom'

the relationship has been declared :

class prescriptionstable extends table {         public function initialize(array $config)     {         $this->table('prescriptions');         $this->belongsto('profils', [             'foreignkey' => 'profils_id',         ]);      } }  class profilstable extends table {     public function initialize(array $config)     {         $this->table('profils');         $this->hasmany('prescriptions', [             'foreignkey' => 'profils_id',         ]);     } } 

the prescriptionscontroller has search action user can set various filters. ones working on allows him search prescription after substring of name of person (profil) owning prescription.

thus, 'al' match 'albert dupont' or 'marc vidal' not 'eugène brun'. likewise, 'ert dup' should match 'albert dupont' (as 'ont al' should', that's story).

the search() method has following lines query :

$q1 = $this->prescriptions->find()->where(['prescriptions.users_id = ' . $userid]);  $q1->matching('profils', function ($q) use($filter) {                 return $q->where(["concat(profils.prenom, ' ', profils.nom) like" => "%$filter%"]); }); [...] return $q1->select(['id'])->toarray(); 

unfortunately when run query, cakephp throws me following error :

error: sqlstate[42s22]: column not found: 1054 unknown column 'profils.nom' in 'on clause'

i wanted check query object tried :

$q1->matching('profils', function ($q) use($filter) {                     return $q->where(["concat(profils.prenom, ' ') like" => "%$filter%"]);  

which obvously not interesting concat, works. error rises when use 2 fields : second 1 not use alias profils table name 'profils' instead.

i've been stuck on error several hours , i'll appreciate insights on doing wrong here.

the used syntax isn't ment support that

the syntax using ment support constructs like

tablealias.column sql_expression 

the compiler lowercase expression part, ie found after possible tablealias.column identifier, in case concat(profils.prenom, treated identifier, , rest being seen separated via whitespace, ' ', profils.nom) like, being treated expression (neither being checked validity), hence being lowercased, , that's happens, finaly query profils.nom, doesn't exist, alias profils.

use expressions

you should use proper expressions, portable, combinable, , support value binding.

cakephp ships support various sql functions out box, including concat. supports various comparison expressions can combined other expressions, ie can pass concat function expression like comparison expression , should good:

return $q     ->where(function (\cake\database\expression\queryexpression $exp, \cake\orm\query $query) use($filter) {         return $exp             ->like(                 $query->func()->concat([                     'profils.prenom' => 'identifier',                     ' ',                     'profils.nom' => 'identifier'                 ]),                 "%$filter%",                 'string'             );     }); 

see also


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -