How to know if an object from a relation exists in the database

doctrine, php, symfony1

Solution

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.

Problem

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.

Original source