Question

I'm trying to check if a result in my DQL is NULL.

I got the following DQL query:

    $q = self::createQuery("l")
        ->select('i.*, s.aantal, m.naam, c.cat_naam, a.app_id')
        ->from('InstalledBase i, i.Spare s, i.Apparaat a, a.Categorie c, a.Merk m')
        ->execute();

    return $q;

Now i want to check if the s.aantal is NULL so i do:

if(is_null($installedbase->Spare->spare_id))

when the variable is NOT null everything works, but when it is actually NULL i get a E notice message:

Notice: Trying to get property of non-object in \installedbase\templates\_index.php on line 29

It does see that it is null though because the if condition is executed.

Weird thing is i'm doing the exact same thing on another page where it works no problem. But i must be doing something wrong or stupid since those messages generally don't show for nothing.

So can anybody explain this to me? :)

Was it helpful?

Solution

I'm really not familiar with DQL, but you could extend your if statement to;

if (!is_object($installedbase->Spare) || is_null($installedbase->Spare->spare_id))

OTHER TIPS

How about....

if(isset($installedbase->Spare->spare_id)) {
  if(is_null($installedbase->Spare->spare_id)) {
    // do something
  }
}

...or maybe try using the PHP get_object_vars function, something like:

if(!empty(get_object_vars($installedbase->Spare)) && is_null($installedbase->Spare->spare_id)) {
  // do something
  }

What I'm not so clear about it is how you're getting spare_id from s.aantal.

Is it really needed to check if it is strictly null?

 if ($q) ...

should be enough for most cases. Take a look at PHP type comparsion tables.

Also, you may be interested in Doctrine_Null:

final class Doctrine_Null
{ 
    public function exists()
    {
        return false;    
    }

    public function __toString()
    {
        return '';
    }
}

Should you not be left joining in i.Spare?

So:

$q = self::createQuery("l")
        ->select('i.*, s*, m.naam, c.cat_naam, a.app_id')
        ->from('InstalledBase i')
        ->leftJoin('i.Spare s')
        ->innerJoin('i.Apparaat a, a.Categorie c, a.Merk m')
        ->execute();

return $q;

Unless spare_id isn't the primary key that is. Then maybe changing the select statement to what I have done might help then adding this to your test:

if(isset($installedbase->Spare) && is_null($installedbase->Spare->spare_id))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top