Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion ManyToMany with extra field, limit results
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jasper N. Brouwer  
View profile  
 More options Nov 7 2012, 11:25 am
From: "Jasper N. Brouwer" <jas...@nerdsweide.nl>
Date: Wed, 7 Nov 2012 17:25:21 +0100
Local: Wed, Nov 7 2012 11:25 am
Subject: Re: [doctrine-user] ManyToMany with extra field, limit results
When joining 2 (or more) entities, you cannot limit on root-entities only. You will have to do this in 2 steps:

In your case I suspect you want to reduce the number of queries needed to get the results (I don't see a WHERE statement). You could do it this way:

$q = $em->createQuery( 'SELECT a.id FROM Articulo a ORDER BY a.fecha DESC' );
$q->setMaxResult( 10 );

$ids = $q->getArrayResult();

$q = $em->createQuery( 'SELECT a, au, u FROM Articulo a LEFT JOIN a.autores au JOIN au.usuario u WHERE a.id IN :ids ORDER BY a.fecha DESC' );
$q->setParameter( 'ids', $ids );

$result = $q->getResult();

PS: If you are doing this for pagination, take a look at http://docs.doctrine-project.org/en/latest/tutorials/pagination.html

--
Jasper N. Brouwer

On 07-11-2012, at 17:04, Guga <gustavobu...@gmail.com> wrote:

> Hi

> Is there someone what can help me? I have a ManyToMany relationship with extra fields, my schema is this one (I'm using Symfony2):

> Articulo -> articles
> ArticuloAutor -> Intermediate table, with extra fields
> Usuario -> users

> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> Articulo:
> --------------------------------------------------------------------------- -----------------------
> /**
>  * @ORM\OneToMany(targetEntity="Gse\AppBundle\Entity\ArticuloAutor", mappedBy="articulo")
>  * @ORM\OrderBy({"orden" = "ASC"})
>  */
>  protected $autores;

>  public function __construct()
>  {
>      $this->autores = new ArrayCollection();
>             ....
>    }
> --------------------------------------------------------------------------- -----------------------

> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> ArticuloAutor:
> --------------------------------------------------------------------------- -----------------------
>     /**
>      * @ORM\Id
>      * @ORM\ManyToOne(targetEntity="Gse\AppBundle\Entity\Articulo", inversedBy="autores")
>      */
>     protected $articulo;

>     /**
>      * @ORM\Id
>      * @ORM\ManyToOne(targetEntity="Gse\AppBundle\Entity\Usuario")
>      */
>     protected $usuario;

>     // extras...

>     /**
>      * @var integer $orden
>      *
>      * @ORM\Column(type="integer", nullable=true)
>      */
>     private $orden;
> --------------------------------------------------------------------------- -------------------------

> If in my respository ArticuloRepository I do:

> $articulos =
>             $this->createQueryBuilder('a')
>             ->addSelect(array('u', 'au'))
>             ->leftJoin('a.autores', 'au')
>             ->innerJoin('au.usuario', 'u')
>             ->orderBy('a.fecha', 'DESC')
>             ->getQuery()
>             ->getResult();

> All is Perfect!! But, if I set a ->setMaxResult(10) (LIMIT 10), it return me 2 articles, with 5 authors each one, because, is applying the limit to all the registers, and not only to articles, any help?

> Thanks a lot, and sorry my english!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.