理论2中的复杂查询

时间:2011-11-10 22:36:53

标签: sql database symfony1 doctrine

我有一个多语种数据库(灵感来自here, the 3rd option。)

我的数据库架构如下所示:

 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

我正在使用带有Doctrine的Symfony 2,我需要一份针对某种语言的所有文章的报告(作为参数传递)。

文章在特定语言中包含名称描述参考但问题时,问题就会出现没有(至少不是所要求的语言)。

到目前为止,我最好的方法是这个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
     ;

是否有简化查询并使其在Doctrine的DQL中工作?

亲切的问候,

Dan Cearnau

1 个答案:

答案 0 :(得分:1)

我会尝试编写没有子选择(子连接)的查询: 缩进连接与dbms p.o.v处于同一级别,但对我们而言依赖于以前的表。

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);

另外,我认为你的模型过于标准化了。 对我来说,这更好:

Article:          (id, another_columns)
TranslationText:  (fk_article_id, fk_language,name_text, description_text, reference_text, um_text)
Language          (id, name)   

正如我现在所看到的,这是模型表明您link

中指出的问题的最多投票答案