如何在doctrine dql中实现相交

时间:2016-02-08 11:13:51

标签: sql doctrine-orm doctrine dql

我正在寻找一种方法将交叉函数sql实现到doctrine dql中,以便将它用于某些查询。这是我想要与他们的结果相交的查询:

$sql1 = "SELECT distinct o1 FROM  Application\Entity\Object1 o1 INNER JOIN Application\Entity\Object2 o2 WITH o2.fkObject1 = o1.pkObject1  WHERE o1.etat = 'valide' AND o2.pkObject2 = $id ";

$sql2 = "SELECT distinct o1 FROM  Application\Entity\Object1 o1 INNER JOIN Application\Entity\Object2 o2 WITH o2.fkObject1 = o1.pkObject1  WHERE o1.etat = 'attente' AND o2.pkObject2 = $id_2 ";

$sql = "sql1 INTERSECT $sql2 ";

//返回[语法错误]第0行,第556行:错误:字符串的预期结束,得到' INTERSECT'

$query = $this->getEntityManager()->createQuery($sql);
$results = $query->getResult();

感谢。

1 个答案:

答案 0 :(得分:0)

它对我有用的方法是使用太多这样的内连接:

$sql =  "SELECT distinct o1 
         FROM  Application\Entity\Object1 o1 
         INNER JOIN Application\Entity\Object2 o2 WITH o2.fkObject1 = o1.pkObject1 
         INNER JOIN  Application\Entity\Object2 o3 WITH o3.fkObject1 = o1.pkObject1  
         WHERE o1.etat = 'valide' 
             AND o2.pkObject2 = $id 
             AND o1.etat = 'attente' 
             AND o3.pkObject3 = $id_2 ";

希望它有所帮助。