1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
use Doctrine\ORM\Mapping\DiscriminatorMap;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\InheritanceType;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\Tests\OrmFunctionalTestCase;
/** @group DDC-2346 */
class DDC2346Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->createSchemaForModels(
DDC2346Foo::class,
DDC2346Bar::class,
DDC2346Baz::class
);
}
/**
* Verifies that fetching a OneToMany association with fetch="EAGER" does not cause N+1 queries
*/
public function testIssue(): void
{
$foo1 = new DDC2346Foo();
$foo2 = new DDC2346Foo();
$baz1 = new DDC2346Baz();
$baz2 = new DDC2346Baz();
$baz1->foo = $foo1;
$baz2->foo = $foo2;
$foo1->bars[] = $baz1;
$foo1->bars[] = $baz2;
$this->_em->persist($foo1);
$this->_em->persist($foo2);
$this->_em->persist($baz1);
$this->_em->persist($baz2);
$this->_em->flush();
$this->_em->clear();
$this->getQueryLog()->reset()->enable();
$fetchedBazs = $this->_em->getRepository(DDC2346Baz::class)->findAll();
self::assertCount(2, $fetchedBazs);
$this->assertQueryCount(2, 'The total number of executed queries is 2, and not n+1');
}
}
/** @Entity */
class DDC2346Foo
{
/**
* @var int
* @Id
* @Column(type="integer")
* @GeneratedValue
*/
public $id;
/**
* @var DDC2346Bar[]|Collection
* @OneToMany(targetEntity="DDC2346Bar", mappedBy="foo")
*/
public $bars;
/** Constructor */
public function __construct()
{
$this->bars = new ArrayCollection();
}
}
/**
* @Entity
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"bar" = "DDC2346Bar", "baz" = "DDC2346Baz"})
*/
class DDC2346Bar
{
/**
* @var int
* @Id
* @Column(type="integer")
* @GeneratedValue
*/
public $id;
/**
* @var DDC2346Foo
* @ManyToOne(targetEntity="DDC2346Foo", inversedBy="bars", fetch="EAGER")
*/
public $foo;
}
/** @Entity */
class DDC2346Baz extends DDC2346Bar
{
}
|