I have two entities related by a OneToMany relation:
<?php
namespace CRMBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
 * User
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="CRMBundle\Entity\ContactRepository")
 */
class User
{
/*...*/
    /**
     * @ORM\OneToMany(targetEntity="CRMBundle\Entity\Message", mappedBy="user", cascade={"persist"})
     * @ORM\OrderBy({"datetime" = "DESC"})
     */
    protected $messages;
/*...*/
}
And
<?php
namespace CRMBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * Message
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Message
{
/*...*/
    /**
     * @ORM\ManyToOne(targetEntity="CRMBundle\Entity\User", inversedBy="messages")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="SET NULL")
     */
    private $user;
    /**
     * @var \DateTime
     *
     * @ORM\Column(name="Datetime", type="datetime", nullable=true)
     */
    private $datetime;
/*...*/
}
My question is how to create a query in the UserController to get every user with the last message (i.e. the most recent according to the datetime attribute) of each user?
 
     
    