如何在WHERE子句中将SELECT与Symfony一起使用?我有错误

时间:2019-02-12 09:23:14

标签: mysql symfony doctrine where

我正在将Mysql SQL迁移到Symfony项目,并且在执行SQL时从TLP收到错误消息。我猜问题出在WHERE子句中的第二个SELECT中。

如果我使用原始SQL,并且没有使用symfony框架,则不会有任何问题。此外,如果我在WHERE子句中使用“((vars.ns IN(SELECT ...)))”,则不会出现任何错误,但SQL一定不能那样。

date_default_timezone_set('UTC');
    $em = $this->getContainer()->get('doctrine')->getManager();
    $query = $em->createQuery("SELECT vars.dm, vars.ns, vars.mc, vars.ac, vars.sf, vars.cn, vars.mp, vars.ta, vars.mo, vars.mh, vars.pa, vars.sp, vars.so, vars.sh, vars.sn, vars.mm, vars.ms, vars.gs, vars.a1, vars.a2, vars.updated FROM BaseBundle:Vars vars WHERE (SELECT device.id FROM BaseBundle:Device device WHERE device.ns = vars.ns AND device.idUser IS NOT NULL AND device.idUser != '422' AND device.idUser != '819') AND vars.idDevice IS NOT NULL AND vars.updated > :time")->setParameter('time', date('Y-m-d H:i:s', strtotime('-31 days')));
    $historics = $query->getResult(); 

我收到此错误:

  [Doctrine\ORM\Query\QueryException]
  [Syntax Error] line 0, col 233: Error: Expected Literal, got 'SELECT'

此SQL必须返回276行,但确实如此,但不是symfony。对这个问题有什么想法吗? 非常感谢你,

更新: MYSQL原始查询

SELECT 
vars.dm,
vars.ns,
vars.mc,
vars.ac,
vars.sf,
vars.cn,
vars.mp,
vars.ta,
vars.mo,
vars.mh,
vars.pa,
vars.sp,
vars.so,
vars.sh,
vars.sn,
vars.mm,
vars.ms,
vars.gs,
vars.a1,
vars.a2,
vars.updated
FROM
table_3.vars
WHERE
   (SELECT 
        device.id_user
    FROM
        table_3.device
    WHERE
        device.ns = vars.ns
            AND device.id_user IS NOT NULL
            AND device.id_user != '422'
            AND device.id_user != '819')
    AND vars.id_device IS NOT NULL
    AND vars.updated > NOW() - INTERVAL 30 DAY

2 个答案:

答案 0 :(得分:0)

首先,Doctrine不使用SQL,而是使用DQL。 DQL看起来很相似,但是当您谈论属性和字段时,其工作原理有所不同。在创建Doctrine Query时,您必须使用属性名称而不是字段名称,并且所引用的实体必须与此类属性一起存在。您的BaseBundle:Vars实体存在吗?并具有查询字符串中提到的属性吗?设备实体呢?

答案 1 :(得分:0)

您收到此错误

  

预期字面量为'SELECT'

因为该学说试图解析子查询条件:

(SELECT device.id FROM BaseBundle:Device device WHERE device.ns = vars.ns AND device.idUser IS NOT NULL AND device.idUser != '422' AND device.idUser != '819') > 0

,并希望它是Literal

之所以会发生这种情况,是因为没有为上述条件定义comparison,并且主义认为这应该是文字,并且当“看到”它以“ SELECT”开头时,就会抛出错误。

要克服此问题,请对条件添加一个比较(例如 > 0

View view = findViewById(R.id.id_of_constraintLayout); 
// Here you assign the view Variable to the id of the View hosting all your Views. 

view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

Bitmap screenshot = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);