[英]Complex Query in Doctrine 2
I have a multilingual database(inspired from here, the 3rd option .) 我有一个多语言数据库(灵感来自这里,第三个选项 。)
My database schema looks like this: 我的数据库架构如下所示:
Article: (id, fk_name, fk_description, fk_reference, fk_um)
Translation: (id)
TranslationText: (id, fk_translation, fk_language,text)
Language (id, name)
Article --(M:1)--> Translation --(1:M)--> TranslationText --(M:1)--> Language
I'm using Symfony 2 with Doctrine and i need a report for all Articles in a certain language(passsed as parameter). 我正在使用Symfony 2和Doctrine,我需要一份针对某种语言的所有文章的报告(作为参数传递)。
The problem appers when an Article has a Name , Description or Reference in a particular language but doesn't have Um (at least not in the language requested). 当文章具有特定语言的名称 , 描述或参考但没有Um (至少不是所请求的语言)时,问题就会出现。
My best aproach so far is this SQL that i can't implement in Doctrine 2 due to the lack of nested joins.( language_id = 28 ) 到目前为止,我最好的方法是这个SQL,由于缺少嵌套连接,我无法在Doctrine 2中实现。( language_id = 28 )
select ing.*
, tx_name.text, tx_description.text, tx_reference.text, tx_um.text
from Article ing
left join (Translation t_name left
join TranslationText tx_name
on t_name.id = tx_name.fk_translation
and tx_name.fk_language = 28)
on ing.fk_name = t_name.id
left join (Translation t_description
left join TranslationText tx_description
on t_description.id = tx_description.fk_translation
and tx_description.fk_language = 28)
on ing.fk_description = t_description.id
left join (Translation t_reference
left join TranslationText tx_reference
on t_reference.id = tx_reference.fk_translation
and tx_reference.fk_language = 28)
on ing.fk_reference = t_reference.id
left join (Translation t_um
left join TranslationText tx_um
on t_um.id = tx_um.fk_translation
and tx_um.fk_language = 28)
on ing.fk_um = t_um.id
;
Is there anyway to simplify the query and to make it work in Doctrine's DQL ? 无论如何,是否有简化查询并使其在Doctrine的DQL中工作?
Kind Regards, 亲切的问候,
Dan Cearnau Dan Cearnau
I'll try to write your query without subselects(subjoins): The indented join are at the same level of dbms pov, but for us are dependent on previous tables. 我将尝试编写没有子选择(子连接)的查询:缩进连接与dbms pov处于同一级别,但对我们而言依赖于以前的表。
select ing.*
, tx_name.text
, tx_description.text
, tx_reference.text
, tx_um.text
from Article ing
left join Translation t_name on ing.fk_name = t_name.id
left join TranslationText tx_name
on (t_name.id = tx_name.fk_translation
and tx_name.fk_language = 28)
left join Translation t_description on ing.fk_description = t_description.id
left join TranslationText tx_description
on (t_description.id = tx_description.fk_translation
and tx_description.fk_language = 28)
left join Translation translation_reference on ing.fk_reference = translation_reference.id
left join TranslationText tx_reference
on (translation_reference.id = tx_reference.fk_translation
and tx_reference.fk_language = 28)
left join Translation translation_um on ing.fk_um = translation_um.id
left join TranslationText tx_um
on (translation_um.id = tx_um.fk_translation
and tx_um.fk_language = 28);
Also, I think your model is overnormalized. 另外,我认为你的模型过于标准化了。 For me, this is better:
对我来说,这更好:
Article: (id, another_columns)
TranslationText: (fk_article_id, fk_language,name_text, description_text, reference_text, um_text)
Language (id, name)
As I see now, this is the model indicate in the most voted answer for the question indicated in your link 正如我现在看到的,这是模型表明链接中指出的问题的最多投票答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.