0

我在 mysql 中有以下具有给定关系的表,并且在 symfony2 中定义了相同的表。我想实现我在下面写下的以下查询。有人可以帮助我如何在querybuilder中编写它吗?

 unitids(id,unitid,databaseid)

 Mappaths(id,ref_unitid2(forigen key for unitids id)  ,ref_unitid1 (forigen key for unitids id),nomal value)

 traveltime(id,ref_map_path(forigen key for mappaths id),  traveltime,noobjs,ondate)

我的mysql查询是这样的:

SELECT t.ID,t.ondate, un.databaseid as source1, 
 un1.databaseid as  desitnation, t.traveltime, t.noobjs 
 FROM test.traveltime t
 left join test.mappaths p on t.ref_map_path = p.ID
 left join test.unitids un on (p.ref_unitids1 = un.id )
 left join test.unitids un1 on (p.ref_unitids2= un1.id)
 where un.databaseid=50 and un1.databaseid =1 limit 1;

这给了我每一行源和目标以及对象等,如下所示:在 symfony2 中,当我运行此查询时

 $query = $em->createQueryBuilder();
      $results = $query->select('un.databaseid,un1.databaseid')
      ->from('ApiMapBundle:Traveltime', 't')
            ->leftJoin('t.refMapPath', 'p')
            ->leftJoin('p.refUnitids2', 'un')
            ->leftJoin('p.refUnitids1', 'un1')
            ->where('un.databaseid = :bdatabaseid1')
            ->setParameter('bdatabaseid1', 2)
            ->andwhere('un1.databaseid = :bdatabaseid2')
             ->setParameter('bdatabaseid2',1)
            //->setMaxResults(1)
            ->getQuery()
            ->getResult();

它给了我这样的输出

 Array ( [0] => Array ( [databaseid] => 1 ) [1] => Array ( [databaseid] => 1 ))

但它应该给我

  Array ( [0] => Array ( [databaseid] => 1 ) [1] => Array ( [databaseid] => 2 ))

我怎样才能实现上述输出????

4

1 回答 1

1

假设您正在存储库中编写代码,并且您还在实体中定义了关系

$em = $this->getEntityManager();
$query = $em->getRepository('BUNDLE:traveltime')->createQueryBuilder('t');
$query->select('t.Id,..')
   ->leftJoin('t.ref_map_path','p')
   ->leftJoin('p.ref_unitids1','un')
   ->leftJoin('p.ref_unitids2','un1')
   ->where('un.databaseid = :bdatabaseid')
   ->setParameter('bdatabaseid',1)
   ->orWhere('un1.databaseid = :bdatabaseid1')
   ->setParameter('bdatabaseid1',2);
于 2016-02-05T09:53:03.753 回答