教条2[语法错误]错误:期望的文字,得到'-'

时间:2022-09-24 06:47:11

I am using doctrine2 with symfony2 and I am trying to perform a simple select query:

我使用了symfony2的理论2,我正在尝试执行一个简单的select查询:

I want to run:

我想运行:

SELECT * FROM table WHERE status in (1, -1)

This PHP code:

这个PHP代码:

$queryBuilder = $this->_em->createQueryBuilder();
        $queryBuilder
        ->select('n')
        ->from('MyBundle:Table', 'n')
        ->where('n.status IN (1, -1)');
return $queryBuilder->getQuery()->getResult();

Gives the following exception:

给以下异常:

[Syntax Error] line 0, col 96: Error: Expected Literal, got '-'

This is the attribute definition within the entity:

这是实体内的属性定义:

/**
 * @var integer
 *
 * @ORM\Column(name="status", type="integer", nullable=true)
 */
private $status;

If I use only positive numbers within the in argument, it will work. The exception only happens with negative numbers.

如果我在论证中只使用正数,它就会起作用。例外情况只发生在负数上。

What causes this exception?

导致此异常的原因是什么?

1 个解决方案

#1


12  

Should do the trick :

应该这样做:

$queryBuilder = $this->_em->createQueryBuilder();
        $queryBuilder
        ->select('n')
        ->from('MyBundle:Table', 'n')
        ->where('n.status IN (:status)')
        ->setParameter('status', array(1, -1));

#1


12  

Should do the trick :

应该这样做:

$queryBuilder = $this->_em->createQueryBuilder();
        $queryBuilder
        ->select('n')
        ->from('MyBundle:Table', 'n')
        ->where('n.status IN (:status)')
        ->setParameter('status', array(1, -1));