在Symfony2查询构建器中转换本机SQL查询(在join中选择) [英] Convert native sql query in Symfony2 query builder ( with select in join )

查看:70
本文介绍了在Symfony2查询构建器中转换本机SQL查询(在join中选择)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个(巨大的)SQL查询可以转换为doctrine2。



我已经成功使用 createNativeQuery()但有时结果与原始结果不匹配(来自mysql)



这是查询:

 选择e1_.etapes AS etapes3,e1_.position AS position4,c0_.exercicesData_id AS Exercice_id,exo.titre作为Exo,age.bornInf作为ageInf,age.bornSup作为ageSup,imc.bornInf作为imcInf ,imc.bornSup作为imcSup,sexe.bornInf作为sexeInf,sexe.bornSup作为sexeSup 

FROM ContrainteData c0_

LEFT JOIN ExerciceData e1_ ON c0_.exercicesData_id = e1_.id
LEFT JOIN锻炼exo ON e1_.exercice_id = exo.id
LEFT JOIN冲突conintedata_entrydata c3_ ON c0_.id = c3_.contraintedata_id
INNER JOIN(SELECT * FROM`entrydata` GROUP BY type_id,bornInf, bornSup)AS age ON age.type_id = 1
INNER JOIN(选择*来自ʻentrydata` GROUP BY type_id,bornInf,bornSup)AS imc ON imc.type_id = 4
INNER JOIN( SELECT * FROM`entrydata` GROUP BY type_id,bornInf,bornSup)作为sexe在sexe.type_id = 5

WHERE
(age.bornSup> = 30和age.bornInf< = 30)和
(imc.bornSup> = 20且imc.bornInf< = 20)和
(sexe.bornSup> = 1且sexe.bornInf< = 1)和
e1_.entrainement_id = 6

GROUP BY etapes3,位置4

订单由etapes3 ASC,位置4 ASC

使用queryBuilder,我已完成以下操作:

  $ qb1 = $ this-> createQueryBuilder('c'); 
$ qb2 = $ this-> createQueryBuilder(’t’);


$ qb2-> select('t')
-> from('bonk\AppBundle\Entity\EntryData','en')
-> groupBy('en.type')
-> addGroupBy('en.bornInf')
-> addGroupBy('en.bornSup');


$ qb1-> select('c')
-> leftJoin('c.exercicesData','e')
-> leftJoin ('e.exercice','ex')
-> join($ qb2-> getDql(),'age','with','age.type = 1')
- > join($ qb2-> getQuery()-> getDql(),'imc','with','imc.type = 4')
-> join($ qb2-> getQuery ()-> getDql(),'sexe','with','sexe.type = 5')
->其中('age.bornSup> = 30并且age.bornInf< = 30 ')
-> andWhere('imc.bornSup> = 20 and imc.bornInf< = 20')
-> andWhere('sexe.bornSup> = 1 and sexe.bornInf < = 1')
-> andWhere('e.entrainment = 6')
-> groupBy('e.etapes','ASC')
-> addGroupBy ('e.position','ASC');

返回$ qb1-> getQuery()-> getArrayResult();

我有以下错误:

  [语义错误]第0行,'SELECT t FROM'附近的col 116:错误:未定义类'SELECT' 


解决方案

我终于成功获得了一个mysql视图,该视图由学说和DQL查询中的伪实体调用。 p>

要在sql上创建视图,只需执行以下行:

 在我的示例中,创建视图VIEWNAME YOUQUERY 

 创建视图entrydataView SELECT * FROMʻentrydata` GROUP BY type_id,bornInf,bornSup 


i have this ( huge ) sql query to convert into doctrine2 .

i already succeeded with createNativeQuery() but sometimes results don't match original one ( from mysql )

here is the query :

 SELECT e1_.etapes AS etapes3, e1_.position AS position4, c0_.exercicesData_id AS Exercice_id, exo.titre as Exo, age.bornInf as ageInf, age.bornSup as ageSup, imc.bornInf as imcInf, imc.bornSup as imcSup, sexe.bornInf as sexeInf, sexe.bornSup as sexeSup

 FROM ContrainteData c0_ 

 LEFT JOIN ExerciceData e1_ ON c0_.exercicesData_id = e1_.id 
 LEFT JOIN Exercice exo ON e1_.exercice_id = exo.id 
 LEFT JOIN contraintedata_entrydata c3_ ON c0_.id = c3_.contraintedata_id 
 INNER JOIN (SELECT * FROM `entrydata` GROUP BY type_id, bornInf, bornSup) AS age ON   age.type_id = 1
 INNER JOIN (SELECT * FROM `entrydata` GROUP BY type_id, bornInf, bornSup) AS imc ON imc.type_id = 4
 INNER JOIN (SELECT * FROM `entrydata` GROUP BY type_id, bornInf, bornSup) AS sexe ON sexe.type_id = 5

 WHERE
      ( age.bornSup >= 30 and age.bornInf <= 30 ) and
      ( imc.bornSup >= 20 and imc.bornInf <= 20 ) and
      ( sexe.bornSup >= 1 and sexe.bornInf <= 1 ) and
      e1_.entrainement_id = 6

 GROUP BY etapes3, position4

 ORDER BY etapes3 ASC, position4 ASC

with the queryBuilder , i have done the following :

    $qb1            = $this->createQueryBuilder('c');
    $qb2            = $this->createQueryBuilder('t');


    $qb2->select( 't' )
        ->from  ( 'bonk\AppBundle\Entity\EntryData', 'en' )
        ->groupBy( 'en.type')
        ->addGroupBy( 'en.bornInf' )
        ->addGroupBy( 'en.bornSup' );


    $qb1->select( 'c' )
        ->leftJoin( 'c.exercicesData', 'e'  )
        ->leftJoin( 'e.exercice'     , 'ex' )
        ->join    ( $qb2->getDql(), 'age', 'with', 'age.type = 1' )
        ->join    ( $qb2->getQuery()->getDql(), 'imc', 'with', 'imc.type = 4' )
        ->join    ( $qb2->getQuery()->getDql(), 'sexe', 'with', 'sexe.type = 5')
        ->where   ( 'age.bornSup >= 30 and age.bornInf <= 30'                    )
        ->andWhere( 'imc.bornSup >= 20 and imc.bornInf <= 20'                    )
        ->andWhere( 'sexe.bornSup >= 1 and sexe.bornInf <= 1'                    )
        ->andWhere( 'e.entrainement = 6'                                         )
        ->groupBy ( 'e.etapes', 'ASC'                                            )
        ->addGroupBy( 'e.position', 'ASC'                                        );

    return $qb1->getQuery()->getArrayResult();

i have the following error :

      "[Semantical Error] line 0, col 116 near 'SELECT t FROM': Error: Class 'SELECT' is not defined "

解决方案

i finally succeed with a mysql view which is called by a fake entity in doctrine and DQL Query .

to create a view on sql , simply execute this line :

"create view VIEWNAME YOUQUERY"

in my example :

"create view entrydataView SELECT * FROM `entrydata` GROUP BY type_id, bornInf, bornSup"

这篇关于在Symfony2查询构建器中转换本机SQL查询(在join中选择)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆