I have issues writing this code correctly. I have companies
, categories
, companies_tags
and tags
tables. The relations are as follow (baked automatically) :
// CompaniesTable.php
$this->belongsToMany('Tags', [
'foreignKey' => 'company_id',
'targetForeignKey' => 'tag_id',
'joinTable' => 'companies_tags'
]);
$this->belongsTo('Categories', [
'foreignKey' => 'categorie_id'
]);
// CategoriesTable.php
$this->hasMany('Etablissements', [
'foreignKey' => 'categorie_id'
]);
// TagsTable.php
$this->belongsToMany('Companies', [
'foreignKey' => 'tag_id',
'targetForeignKey' => 'company_id',
'joinTable' => 'companies_tags'
]);
// CompaniesTags.php
$this->belongsTo('Companies', [
'foreignKey' => 'company_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Tags', [
'foreignKey' => 'tag_id',
'joinType' => 'INNER'
]);
I have to select companies which name, category name, or one of its tag name contains a specific text.
$ets = (new TableRegistry())
->get('Companies')
->find('published')
->distinct()
->contain([
"Tags",
"Categories"
])
->leftJoinWith('Tags', function (\Cake\ORM\Query $q) use ($quoi) {
return $q->where(['OR' => ['Tags.nom LIKE ' => '%' . $quoi . '%', 'Tags.description LIKE' => '%' . $quoi . '%']]);
})
->where(['OR' => ["Companies.nom LIKE" => "%" . $quoi . "%",
"Companies.description LIKE" => "%" . $quoi . "%",
"Categories.description LIKE" => "%" . $quoi . "%",
"Categories.nom LIKE" => "%" . $quoi . "%",
]
]);
This query is what I can imagine, but it seems like my LEFT JOIN on tags
is not working. Can someone help me fix this ?