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.
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.
You can also use Doctrine_Record::relatedExists()
, which is kind of a complement to hasReference()
You use it like this:
if ($person->relatedExists('Student'))
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With