我在查询中有一些奇怪的行为,我在一个包含中使用了select策略。
基本上,如果存在,则使用所请求的语言获取Sitedescription的内容(如果存在),如果不存在,则以英语提供,如果不存在,则使用法语:
$query = $this->Sites->find()
->contain(
[
'Sitedescriptions' => [
'strategy' => 'select',
'queryBuilder' => function ($q) use ($reqLanguage_id, $reqLanguageFr_id, $reqLanguageEn_id) {
return $q
->select(['isGivenLanguage' => $q->newExpr()->eq('language_id', $reqLanguage_id), 'site_id', 'language_id')
->where (['language_id IN ' => [$reqLanguage_id, $reqLanguageFr_id, $reqLanguageEn_id]])
->order(['isGivenLanguage' => 'DESC', 'language_id' => 'DESC'])
->limit(1)
;
},
],
])
;
这个工作得很好。 现在我想要完全相同的查询,但我还想从sitedescription,'paragraph_0'再获得一个字段。当然是一个现有的领域。
$query = $this->Sites->find()
->contain(
[
'Sitedescriptions' => [
'strategy' => 'select',
'queryBuilder' => function ($q) use ($reqLanguage_id, $reqLanguageFr_id, $reqLanguageEn_id) {
return $q
->select(['isGivenLanguage' => $q->newExpr()->eq('language_id', $reqLanguage_id), 'site_id', 'language_id', 'paragraph_0')
->where (['language_id IN ' => [$reqLanguage_id, $reqLanguageFr_id, $reqLanguageEn_id]])
->order(['isGivenLanguage' => 'DESC', 'language_id' => 'DESC'])
->limit(1)
;
},
],
])
;
在select()中添加此字段会使查询返回一个空的Sitedescription数组。
此处提供了查询和输出https://gist.github.com/caBBAlainB/a721eebb3e8aa04a3506。
关于附加字段出现问题的原因有什么想法吗?
修改 为第二种情况添加生成的sql查询,其中请求了paragraph_0:
有限制(1):
SHOW FULL COLUMNS FROM `sitedescriptions`
SHOW INDEXES FROM `sitedescriptions`
SELECT * FROM information_schema.key_column_usage AS kcu INNER JOIN information_schema.referential_constraints AS rc ON (kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME) WHERE kcu.TABLE_SCHEMA = 'modulwoomobtst' AND kcu.TABLE_NAME = 'sitedescriptions' and rc.TABLE_NAME = 'sitedescriptions'
SHOW TABLE STATUS WHERE Name = 'sitedescriptions'
SELECT (language_id = '1') AS `isGivenLanguage`, Sitedescriptions.id AS `Sitedescriptions__id`, Sitedescriptions.site_id AS `Sitedescriptions__site_id`, Sitedescriptions.language_id AS `Sitedescriptions__language_id`, Sitedescriptions.paragraph_0 AS `Sitedescriptions__paragraph_0` FROM sitedescriptions Sitedescriptions WHERE (Sitedescriptions.site_id in (63,64,827,828,838,841,849,859,866,908,1034,1116) AND language_id in (1,1,9)) ORDER BY isGivenLanguage DESC, language_id DESC
无限制(1):
SHOW FULL COLUMNS FROM `sitedescriptions`
SHOW INDEXES FROM `sitedescriptions`
SELECT * FROM information_schema.key_column_usage AS kcu INNER JOIN information_schema.referential_constraints AS rc ON (kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME) WHERE kcu.TABLE_SCHEMA = 'modulwoomobtst' AND kcu.TABLE_NAME = 'sitedescriptions' and rc.TABLE_NAME = 'sitedescriptions'
SHOW TABLE STATUS WHERE Name = 'sitedescriptions'
SELECT (language_id = '1') AS `isGivenLanguage`, Sitedescriptions.id AS `Sitedescriptions__id`, Sitedescriptions.site_id AS `Sitedescriptions__site_id`, Sitedescriptions.language_id AS `Sitedescriptions__language_id`, Sitedescriptions.paragraph_0 AS `Sitedescriptions__paragraph_0` FROM sitedescriptions Sitedescriptions WHERE (Sitedescriptions.site_id in (63,64,827,828,838,841,849,859,866,908,1034,1116) AND language_id in (1,1,9)) ORDER BY isGivenLanguage DESC, language_id DESC
据我所知,两个请求都是相同的,但结果却不同。