5

I have this mapped property inside my product entity:

/**
 * @ORM\ManyToMany(targetEntity="Group", mappedBy="products", indexBy="id", fetch="EAGER")
 *
 */
protected $groups;

I wonder, my understanding for fetch="EAGER" is that it should get the groups once the product is selected, this is what happens but it uses 2 queries whenever i do something like findBy() one query to get the product and another one to get the groups.

Is there anyway to make findBy() or other helper methods get the product along with its groups in one query or the only way is to write a custom repository function and do a LEFT-JOIN myself?

UPDATE

I tried multiple solutions and endup overwriting the findBy() function something like:

public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
{
    $q = $this
    ->createQueryBuilder('u')
    ->select('u, g')
    ->leftJoin('u.groups', 'g')
    ->setFirstResult( $offset )
    ->setMaxResults( $limit );

    foreach ($criteria as $field => $value)
    {
        $q
            ->andWhere(sprintf('u.%s = :%s', $field, $field))
            ->setParameter($field, $value)
        ;
    }

    foreach ($orderBy as $field => $value)
    {
        $q->addOrderBy(sprintf('u.%s',$field),$value);
    }

    try
    {
        $q = $q->getQuery();
        $users = $q->getResult();
        return $users;
    }
    catch(ORMException $e)
    {
        return null;
    }
}

Questions

1- Can i use fetch="EAGER" to make findBy return the product along with its groups in one query

2- If not then is there any case that i use fetch="EAGER" with many-to-many entities without killing performance

3- Is overriding the findBy is a good approach? any drawbacks?

Thanks,

4
  • As far as I know, it should not execute 2 queries - that's the whole point of EAGER fetching. Now that I think of it, I'm not really sure. I will try this as soon as I get to my dev box... Commented Oct 17, 2013 at 9:25
  • @jperovic i tried it with two different project, it's still executing 2 queries. if it's the case i don't see why i would use findBy or findOneBy for entities that has relationships with other entities which it's always about 70% to 90% of the entities in any project. Commented Oct 18, 2013 at 3:38
  • IT really looks like that with many to many, fetch=eager is not much usefull, just for curiosity, do you see any diferences if you make the product entity the owning side? (change mappedBy="products" for inversedBy="products") Commented Oct 21, 2013 at 8:00
  • @Francesc not at all. i tried both ways and still the same. Commented Oct 21, 2013 at 9:10

1 Answer 1

1

The FETCH_EAGER mode has it's problems. In fact there is an open request here to resolve this issue but hasn't been closed yet.

I recommend using a custom repository to fetch the data in the manner you want it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.