I'm having a "Student" model that is related to many "Transfer" elements in Symfony2 using Doctrine.
How can I access the last "Transfer" element that is related to the current "Student" in an efficient way?
That is to say for example, creating a method like "getLastTransfer()" in the "Student" class.
I have hears that it is not recommended to use the entity manager inside of a modal in order to be able to use dependency injections on it etc...
Thank you
Granted that your Transfer
entity has a date
field, Student
class will look like:
class Student
{
// ...
/**
* @OneToMany(targetEntity="Transfer" mappedBy="student")
* @OrderBy({"date" = "ASC"})
*/
private $transfers;
// ...
}
The transfers are stored in a ArrayCollection
, so just call:
$student -> getTransfers() -> last();
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