I upgraded cakephp to cakephp 3.4.7 version. My website is in multiple languages so an comment's and authors's title depends on the local.
How to query translated content in contained associations?
The php code in the controller looks like this:
//Comments belongsTo Authors
$this->loadModel('Comments');
$comments = $this->Comments->find('all')->where(['Comments.active' => 1])->contain([
'Authors' => function ($q) {
return $q
->where(['Authors.title LIKE' => '%'.$this->term.'%','Authors.active' => 1])
->order(['Authors.position' => 'ASC']);
}
])->toArray();
This works only for the default language, but when I change the language, I get always an empty array. Table i18n contains records for 'comments' and 'authors' in other languages. In 'author' model:
$this->addBehavior('Translate', ['fields' => ['title','text']]);
When I changed the code according to the example:How to query translated content when using the translate behavior? I got the following results:
//Authors hasMany Comments - IT WORKS!!!
$this->loadModel('Authors');
$authors = $this->Authors->find('all')->where(['Authors.active' => 1])->contain([
'Comments' => function ($q) {
return $q
->where(['Comments_title_translation.content LIKE' => '%'.$this->term.'%','Comments.active' => 1])
->order(['Comments.position' => 'ASC']);
}
])->toArray();
//Comments belongsTo Authors - IT DOES NOT WORK!!!
$this->loadModel('Comments');
$comments = $this->Comments->find('all')->where(['Comments.active' => 1])->contain([
'Authors' => function ($q) {
return $q
->where(['Authors_title_translation.content LIKE' => '%'.$this->term.'%','Authors.active' => 1])
->order(['Authors.position' => 'ASC']);
}
])->toArray();
In fact, my problem is second example //Comments belongsTo Authors
The following error is displayed:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Authors_title_translation.content' in 'on clause'
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…