如何在doctrine2 queryBuilder中实现ON(... OR ...)mysql查询

时间:2015-03-24 15:04:47

标签: mysql symfony doctrine-orm

我在MYSQL中有一个SQL查询: 例如

SELECT s.* FROM vplanning.cities as c1
INNER JOIN vplanning.cities as c2
ON (c1.id = c2.area_id)
INNER JOIN vplanning.storages as s
ON (s.city_id = c2.id OR s.city_id = c1.id)
WHERE c1.id = 109;

在学说中我可以写这样的东西(来自我的工作代码):

$query = $em->getRepository('VplanningPageBundle:Storage')
            ->createQueryBuilder('s')
            ->innerJoin('s.city', 'c1')
            ->innerJoin('c1.area', 'c2')
            ->innerJoin('s.storagestype', 'st')
            ->where('c2.id = :cityID')
            ->andWhere('st.typename = :storagesTypeName')
            ->andWhere('s.active = :active')
            ->setParameters(array(
                'cityID' => $cityID,
                'storagesTypeName' => $storagesTypeName,
                'active' => 1
            ))
            ->orderBy('s.adress')
            ->getQuery();

如你所见,我在

中显示我的关系
->innerJoin('s.city', 'c1')

但我还需要像

这样的关系
->innerJoin('s.city', 'c2')

有这个条件:

ON (s.city_id = c2.id OR s.city_id = c1.id)

但它引发了这个错误:

  

错误:' c2'已定义

c1c2是同一个实体并且具有内在关系。

3 个答案:

答案 0 :(得分:1)

试试这个:

$repository = $em->getRepository('VplanningPageBundle:Storage');
$qb = $repository->createQueryBuilder('storage');
//We can now use the expr()
$qb->join('storage.city', 'city', Join::WITH)
   ->join('city.area', 'area', Join::WITH, $qb->expr()->eq('area.id', ':cityID'))
   ->join('storage.storagestype', 'type', Join::WITH, $qb->expr()->eq('type.typename', ':storagesTypeName'))
   ->where('storage.active = :active')
   ->setParameters(array(
       'cityID' => $cityID,
       'storagesTypeName' => $storagesTypeName,
       'active' => 1
   ))
   ->orderBy('storage.adress');
$query = $qb->getQuery();

答案 1 :(得分:0)

像这样尝试smth

$qb = $this->getRepository('VplanningPageBundle:City')->createQueryBuilder('c');
$qb->leftJoin('c.area', 'a')
    ->join('c.storage', 's', Join::ON, $qb->expr()->orX($qb->expr()->eq('c.id', 's.id'), $qb->expr()->eq('a.id', 's.id')))
    ->innerJoin('s.storagestype', 'st')
    ->where('c.id = :cityID')
    ->andWhere('st.typename = :storagesTypeName')
    ->andWhere('s.active = :active')
    ->setParameters(array(
       'cityID' => $cityID,
       'storagesTypeName' => $storagesTypeName,
       'active' => 1,
    ))
    ->orderBy('s.adress')
    ->getQuery()
;

答案 2 :(得分:0)

问题的解决方案对我来说非常困难,我必须研究它:)

这是我在一些论坛上的问题的答案:

$qb = $em->getRepository('VplanningPageBundle:Storage')->createQueryBuilder('storage');
            $query = $qb->join('storage.city', 'city1', Join::WITH)
                ->leftJoin('city1.area', 'area', Join::WITH, $qb->expr()->eq('area.id', ':cityID'))
                ->leftJoin('storage.city', 'city2', Join::WITH, $qb->expr()->eq('city2.id', ':cityID'))
                ->join('storage.storagestype', 'type', Join::WITH, $qb->expr()->eq('type.typename', ':storagesTypeName'))
                ->where('storage.active = :active')
                ->andWhere($qb->expr()->orX($qb->expr()->isNotNull('city2'), $qb->expr()->isNotNull('area')))
                ->setParameters(array(
                'cityID' => $cityID,
                'storagesTypeName' => $storagesTypeName,
                'active' => 1
            ))
                ->orderBy('storage.adress')
            ->getQuery();