How does Doctrine2 One-to-Many fetch = EAGER work?

I am using Symfony 2.8 / Doctrine ORM 2.5.2.

I have 2 objects, GalleryOneToManyFile

class Gallery
{
    /**
     * @var File[]
     *
     * @ORM\OneToMany(targetEntity="File", mappedBy="gallery", fetch="EAGER")
     */
    private $files;
}

I see 2 things in the documentation.

First, OneToMany relationships now have a parameter fetch=EAGER( specified here ). This was not in previous versions.

Secondly, the manual installation for this selection of the method in the request does not seem to be for OneToMany, but I do not know if the documentation is updated as indicated:

Changing the sampling mode during a query is possible only for one-to-one and many-to-one relationships.

I tried both, anyway, here is my query:

public function findWithEager()
{
    $qb = $this->createQueryBuilder('g');

    $query = $qb->getQuery();
    $query->setFetchMode("CommonBundle\\Entity\\Gallery", "files", ClassMetadata::FETCH_EAGER);

    return $query->getResult();
}

But when I do this:

foreach ($galleryRepository->findWithEager() as $gallery) {
    foreach ($gallery->getFiles() as $file) {
        $file->getId();
    }
}

Then I received 1 + n requests. The first one is SELECT * FROM Gallery, and the next n -SELECT * FROM File WHERE id = :galleryId

, Doctrine 1 + 1 , - SELECT * FROM File WHERE id IN (:galleryListIds)

- ? Doctrine?

:

"--" fetch = "EAGER" , , indexBy.

, .

, !

+4
1

( Doctrine ORM 2.5.6 on PHP 5.6) .

Gallery File .

Left Join.

  • , ->find*, fetch="EAGER" .
  • DQL: SELECT g, f FROM Gallery g LEFT JOIN g.files f

, ->setFetchMode('Gallery', 'files', ClassMetadata::FETCH_EAGER) DQL-

... " " . " ", .

n Gallery.

File.

  • Doctrine (fetch="LAZY") 1 Gallery#$files, (1 + n , ).

PR EAGER_BATCHED , , ( Gallery File ), , , , , .

->find*, fetch="EAGER". . .

DQL , Left Join " " , select.

, DQL , , " " , .

+1

Source: https://habr.com/ru/post/1619438/


All Articles