views:

269

answers:

4

Hi,

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? :)

A: 

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))
TheDeadMedic
does not work sadly, that just adds another exactly the same notice :(
iggnition
Backstep one more - prepend `!is_object($installedbase) ||` to the `if` statement.
TheDeadMedic
does not work either, i really dont get why is_null would not be working in the first place, its working fine on another page :/
iggnition
The trouble is not with `is_null()` - it's that either `$installedbase` or `$installedbase->Spare` is not an object. You're trying to access a property of something that is not an object - hence the error. Did you place the additional `is_object` at the beginning of the `if`?
TheDeadMedic
Above comment is right... when the relation doesn't exist, Spare isn't an object, so you can't access its property. You either need to change what the query returns or add some kind of check before calling the is_null.
Tom
A: 

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.

Tom
A: 

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 '';
    }
}
takeshin
A: 

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))
johnwards