I’m using Doctrine 2 in Zend.
I’m trying to figure out how to access the properties/method for related models from the current object.
For example, we have two tables: Schools and Students.
Many Students belong to a school, so this is a many to one relationship and I’m only interested in listing all the students for each school. I do not wish to query student records to find the details of the school they belong to therefore this is classed as a unidirectional relationship.
Now to set up the many to one relationship in Doctrine 2 between the tables I’d add this in the Students entity, as it is the owning side:
/**
* @ManyToOne(targetEntity="Schools")
* @JoinColumn(name="school_id", referencedColumnName="school_id")
*/
private $schoolId;
Where the name values correspond the column names in the students table and schools table respectively.
So if I have an object of a Schools record, how do I access the student properties/methods?
echo $oSchool->Students->getName(); // doesn't work
I can’t understand what I have done wrong, the proxy class is being generated. Appreciate it if anyone could point me in the right direction.
In the Schools entity you’d want to have something like this
With this you could do something like the following, which gives you an ArrayCollection with all student objects
Hope that helps… Docrine 2 is very powerful but some things are not documented very well. Some more information on this in the Documentation at Working with Assiciations.