Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine2 [Syntax Error] Error: Expected Literal, got '-'

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

I want to run:

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

This PHP code:

$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?

like image 284
ilyes kooli Avatar asked Jul 18 '13 09:07

ilyes kooli


1 Answers

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));
like image 79
Sybio Avatar answered Sep 25 '22 12:09

Sybio