I'm working on a CakePHP 3.0 backend where I have the following tables : 'prescriptions' and 'profils' (names are french).
'prescriptions' has a foreign key directed to 'profils' : prescriptions.profils_id.
'profils' has two fields that can be searched : 'nom' and 'prenom'
The relationship has been declared like this :
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 a search action where the user can set various filters. The ones I am working on allows him to search a Prescription after a substring of the name of the person (Profil) owning that Prescription.
Thus, 'al' could match 'Albert Dupont' or 'Marc Vidal' but not 'Eugène Brun'. Likewise, 'ert dup' should match 'Albert Dupont' (as 'ont Al' should', but that's another story).
The search() method has the following lines for the 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 I run the query, CakePHP throws me the following error :
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'profils.nom' in 'on clause'
I wanted to check the query object so I tried :
$q1->matching('Profils', function ($q) use($filter) {
return $q->where(["CONCAT(Profils.prenom, ' ') like" => "%$filter%"]);
which is obvously not a very interesting CONCAT, but it works. The error only rises when I use two fields : the second one does not use the alias Profils but the table name 'profils' instead.
I've been stuck on this error for several hours now and I'll appreciate any insights on what I am doing wrong here.
on clause
, is there a place where you might be performing a JOIN and maybe that is failing ?Profils
table have anom
column ?