How to specify null value as filter in a Doctrine query?
Asked Answered
P

2

85

I am using Doctrine 1.1 in Zend. I am trying to write a query that will return records that have a null value in a certain column.

    $q = Doctrine_Query::create()
    ->select('a.*')
    ->from('RuleSet a')
    ->where('a.vertical_id = ?', null);

    $ruleset_names_result = $q->execute(array(), Doctrine::HYDRATE_ARRAY);

I have three records in the ruleset table which have a NULL value in the vertical_id column yet the query doest not find these.

Appreciate the help.

Sid.

Pictish answered 1/3, 2011 at 12:16 Comment(0)
S
182

I use doctrine with symfony, and this is how I do:

where('a.vertical_id is NULL');

Stiletto answered 1/3, 2011 at 12:20 Comment(2)
incredible... you had to have caps on the NULL, took me an hour to find the error... thanks. should have googled stackoverflow first.Bouillon
By the was the opposite would be where('a.vertical_id is not NULL');Wellappointed
P
21

If you are using Symfony 2 and above, you can use this code:

->where($qb->expr()->isNull('a.vertical_id'));

Reference:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/query-builder.html#the-expr-class

Phonemics answered 10/2, 2018 at 21:24 Comment(1)
Your answer references to version 2 of doctrine. The question concerns version 1...Bushman

© 2022 - 2024 — McMap. All rights reserved.