Search code examples
phpsymfonydoctrine-ormdql

How to fetch an object from a oneToMany relationship using createQuery()


My structure is as simple as One user -> has many notifications

so to fetch the notifications of the logged in user I type this on the controller

$notifications = $this->getUser()->getNotifications();

Now I need to paginate the results but since this won't work

$notifications = $this->getUser()->getNotifications()->setMaxResults(2)..

I guess I need to use a createQuery to fetch the results? what query would be the DQL equivalent of

"$this->getUser()->getNotifications()" ?


Solution

  • You can't limit . Try slice

    If you have Doctrine 2.1 you can use ->slice() on the collection:

     $notifications = $this->getUser()->getNotifications();    
    
        $result  = $notification->slice(0, 2);