Get the last element of OneToMany's doctrine

I have a Student model that is associated with many Transfer elements in Symfony2 using Doctrine.

How can I effectively use the last Transfer element that is associated with the current Student?

That is, for example, creating a method of type getLastTransfer () in the Student class.

I heard that it is not recommended to use an entity manager inside a modal to be able to use dependency injection on it, etc.

thanks

+4
source share
1 answer

Suppose your Transfer object has a date field, the Student class will look like this:

 class Student { // ... /** * @OneToMany(targetEntity="Transfer" mappedBy="student") * @OrderBy({"date" = "ASC"}) */ private $transfers; // ... } 

Transfers are stored in an ArrayCollection , so just call:

 $student -> getTransfers() -> last(); 
+14
source

All Articles