Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORM expr evaluate empty string

Tags:

symfony

I have been using and still learning query expr(). I have a complex query where I cannot use if first to check if a parameter is '' - empty string. I have to check it inside andX with nested orX using something like:

->andWhere($expr->orX($expr->eq(':sid', ''), $expr->neq('s.id', ':sid')))

Note: I know this line can be done by using if check first, I am using it just for an example, I got error says:

Error: Expected Literal, got ' OR '

I really need to compare empty string inside expr(), how?

like image 686
Bin Chen Avatar asked Dec 04 '22 02:12

Bin Chen


1 Answers

Because '' is not an empty string. It's nothing and so it evaluates to nothing in DQL/SQL. Normally doctrine expects an named parameter. Either you create one to get quoted empty string or supply an empty string quoted by yourself.

Named parameter:

$qb->expr->eq('foo', ':foo');
$qb->expr->setParameter('foo', '');

quoted by yourself:

$qb->expr->eq('foo', "''");
like image 66
Emii Khaos Avatar answered Dec 09 '22 01:12

Emii Khaos