SYMFONY2:SQL to CreateQueryBuilder或CreateQuery

时间:2014-09-15 19:38:54

标签: sql symfony query-builder

有人可以帮助我将这个sql转换为symfony吗?

SELECT cl.* from computador_coleta cl inner join class_property p on cl.id_class_property = p.id_class_property where p.id_class = 15 AND cl.id_computador = 2510;

cl.id_computador是一个变量。

2 个答案:

答案 0 :(得分:3)

$em = $this->getDoctrine()->getManager();
        $query = $em->createQuery(
            'SELECT cc FROM CacicCommonBundle:ComputadorColeta cc INNER JOIN CacicCommonBundle:ClassProperty cp WITH cc.classProperty = cp.idClassProperty WHERE cp.idClass = 15 AND cc.computador = :id'
        )->setParameter('id', $computador);

        $result = $query->getResult();

答案 1 :(得分:0)

 $em = $this->getDoctrine()->getManager();
 $qb = $em->createQueryBuilder();

 $result = $qb->select('c')
             ->from('CacicCommonBundle:ComputadorColeta','cc')
             ->innerJoin('cc.classProperty','cp')
             ->where('cp.idClass = :idClass')
             ->andWhere('cc.idComputador = :idComputador')
             ->setParameter('idClass', 15)
             ->setParameter('idComputador', 2510)
             ->getQuery()
             ->getOneOrNullResult();

   if(!$result) {
     throw new \Exception('no results')
   }

我建议使用类似于教条的内容,因为它更容易阅读