dql没有返回mysql所返回的预期结果

wydwbb8l  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(378)

最近一个小时左右,我试图让下面的mysql查询在dql中工作,但是它没有返回预期的结果,而是什么也没有输出。
这是输出正确结果的mysql查询。
mysql数据库:

SELECT vp.id FROM vehicle_photo AS vp
INNER JOIN vehicle AS v ON vp.vehicle_id = v.id AND vp.manualMaintenanceCheckedOn IS NULL AND vp.type_id = 1
LEFT JOIN vehicle_maintenance_history AS vmh ON vmh.vehicle_id = v.id AND vmh.source != 'kip' AND vmh.source != 'haan'
WHERE vmh.vehicle_id IS NULL
LIMIT 1;

dql公司:

$qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder();
    $qb->select('vp.id')
        ->from('VaartlandIntranetBundle:VehiclePhoto', 'vp')
        ->innerJoin('vp.vehicle', 'v')
        ->leftJoin('v.vehicleMaintenanceHistory', 'vmh', \Doctrine\ORM\Query\Expr\Join::WITH, 'vmh.vehicle = v.id')
        ->where('vp.type = 1')
        ->andWhere('vp.manualMaintenanceCheckedOn is null')
        ->andWhere('vmh.source != :kip')
        ->andWhere('vmh.source != :haan')
        ->andWhere('vmh.vehicle IS NULL')
        ->setParameter('kip','kip')
        ->setParameter('haan','haan')
        ->setMaxResults(1);

    $ers = $qb->getQuery();
    $res = $qb->getQuery()->getResult();

据我们所知,vmh.source出了问题。当省略“andwhere('vmh.source!=”时:haan')“和”andwhere('vmh.source!=:“dql查询不输出结果。然而,这些过滤器需要被激活,因为两者都需要被过滤掉。
我希望你们有办法解决这个问题。

snvhrwxg

snvhrwxg1#

我知道你原来的sql和条令生成的不同。我会尝试这样的方法:

$qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder();
$qb->select('vp.id')
   ->from('VaartlandIntranetBundle:VehiclePhoto', 'vp')
   ->innerJoin('vp.vehicle', 'v')
   ->leftJoin(
       'v.vehicleMaintenanceHistory',
       'vmh',
       \Doctrine\ORM\Query\Expr\Join::WITH,
       $qb->expr()->andx(
           $qb->expr()->eq('vmh.vehicle', 'v.id'),
           $qb->expr()->neq('vmh.source', ':kip'),
           $qb->expr()->neq('vmh.source', ':haan')
       )
   )
   ->where('vp.type = 1')
   ->andWhere('vp.manualMaintenanceCheckedOn is null')
   ->andWhere('vmh.vehicle IS NULL')
   ->setParameter('kip', 'kip')
   ->setParameter('haan', 'haan')
   ->setMaxResults(1);

$ers = $qb->getQuery();
$res = $qb->getQuery()->getResult();

如果 haan 以及 kip 动态参数将始终设置为 kip 以及 haan 我会跳过作业,直接写在表达式中。
更多信息:
条令解释类

相关问题