我想在关联的 belongsToMany 关系中查询翻译。根据文档和这个问题,应该可以在翻译中查询关联。我尝试了以下(简化)代码:
$result = $this->table()->find()
->where([
$this->Activities->Tags->translationField('name') . ' LIKE' =>
'%' . $request->filter . '%'
])
->leftJoinWith('Tags')
->contain(['Tags'])
->all()
->toArray();
标签和活动具有多对多关系。
活动:
$this->belongsToMany('Tags', [
'foreignKey' => 'activity_id',
'targetForeignKey' => 'tag_id',
'joinTable' => 'activities_tags'
]);
$this->addBehavior('Translate', ['fields' => ['name', 'description']]);
标签:
$this->belongsToMany('Activities', [
'foreignKey' => 'tag_id',
'targetForeignKey' => 'activity_id',
'joinTable' => 'activities_tags'
]);
$this->addBehavior('Translate', ['fields' => ['name']]);
活动标签:
$this->belongsTo('Activities', [
'foreignKey' => 'activity_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Tags', [
'foreignKey' => 'tag_id',
'joinType' => 'INNER'
]);
但是,我得到以下生成的 SQL:
SELECT
...
FROM `activities` `Activities`
LEFT JOIN `activities_tags` `ActivitiesTags` ON `Activities`.`id` = (`ActivitiesTags`.`activity_id`)
LEFT JOIN `tags` `Tags` ON `Tags`.`id` = (`ActivitiesTags`.`tag_id`)
LEFT JOIN `i18n` `Activities_name_translation` ON (
`Activities_name_translation`.`model` = :c0
AND `Activities_name_translation`.`field` = :c1
AND `Activities_name_translation`.`locale` = :c2
AND `Activities`.`id` = (`Activities_name_translation`.`foreign_key`)
)
LEFT JOIN `i18n` `Activities_description_translation` ON (
`Activities_description_translation`.`model` = :c3
AND `Activities_description_translation`.`field` = :c4
AND `Activities_description_translation`.`locale` = :c5
AND `Activities`.`id` = (`Activities_description_translation`.`foreign_key`)
)
WHERE `Tags_name_translation`.`content` like :c6
这导致我出现以下错误:
QLSTATE [42S22]:找不到列:1054 'where 子句'中的未知列'Tags_name_translation.content'
缺少以下联接:
LEFT JOIN `i18n` `Tags_name_translation` ON (
`Tags_name_translation`.`model` = :c6
AND `Tags_name_translation`.`field` = :c7
AND `Tags_name_translation`.`locale` = :c8
AND `Tags`.`id` = (`Tags_name_translation`.`foreign_key`)
)
现在我的问题/编辑:
为了生成缺少的连接,我缺少什么来配置 CakePHP?我的意图是通过翻译的标签过滤活动。它适用于非翻译。