views:

77

answers:

2

Hello. I'm using Symfony with Doctrine.

I have two classes defined, Person and Student, a relation one to one.

Each Student is related to a Person, but not every Person has a relation with a Student.

When I call ...

$person->getStudent();

... I always get and object, regardless some Person's doesn't have a Student. How can I know it doesn't (the Student) exist in the database?

Thanks.

+3  A: 

I think

$person->getStudent()->exists();

should do it. At least according to the Doctrine API documentation.
The object you get is probably some kind of Null record.

Felix Kling
@Felix thanks! But it would be better if it returned a `null` when a record doesn't exist ¿isn't it? Thanks for your help.
Kiewic
@Kiewic: Yes I also had problems with this and expected a `null` value instead of an object. If you find the answer useful, please accept it.
Felix Kling
@Felix Sorry, I forgot to accept it.
Kiewic
A: 

There's a pretty new method (I think since Doctrine 1.2): $person->hasReference("Student"); returns a boolean for whether there is actually a Student associated to the person, no matter whether it was saved already or not in database, and as desired without creating a new Student record. This call can be suitable in situations when application logic doesn't care about the persistence of the related object, e.g. while within a transaction (I guess). Hope that helps a bit, cheers, RAPHAEL

Raphael Schumacher