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
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\ORM\Mapping\Cache;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\Tests\OrmFunctionalTestCase;
use function uniqid;
/** @group #6217 */
final class GH6217Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
$this->enableSecondLevelCache();
parent::setUp();
$this->createSchemaForModels(
GH6217AssociatedEntity::class,
GH6217FetchedEntity::class
);
}
public function testLoadingOfSecondLevelCacheOnEagerAssociations(): void
{
$lazy = new GH6217AssociatedEntity();
$eager = new GH6217AssociatedEntity();
$fetched = new GH6217FetchedEntity($lazy, $eager);
$this->_em->persist($eager);
$this->_em->persist($lazy);
$this->_em->persist($fetched);
$this->_em->flush();
$this->_em->clear();
$repository = $this->_em->getRepository(GH6217FetchedEntity::class);
$filters = ['eager' => $eager->id];
self::assertCount(1, $repository->findBy($filters));
$this->getQueryLog()->reset()->enable();
/** @var GH6217FetchedEntity[] $found */
$found = $repository->findBy($filters);
self::assertCount(1, $found);
self::assertInstanceOf(GH6217FetchedEntity::class, $found[0]);
self::assertSame($lazy->id, $found[0]->lazy->id);
self::assertSame($eager->id, $found[0]->eager->id);
$this->assertQueryCount(0, 'No queries were executed in `findBy`');
}
}
/**
* @Entity
* @Cache(usage="NONSTRICT_READ_WRITE")
*/
class GH6217AssociatedEntity
{
/**
* @var string
* @Id
* @Column(type="string", length=255)
* @GeneratedValue(strategy="NONE")
*/
public $id;
public function __construct()
{
$this->id = uniqid(self::class, true);
}
}
/**
* @Entity
* @Cache(usage="NONSTRICT_READ_WRITE")
*/
class GH6217FetchedEntity
{
/**
* @var GH6217AssociatedEntity
* @Id
* @Cache("NONSTRICT_READ_WRITE")
* @ManyToOne(targetEntity=GH6217AssociatedEntity::class)
*/
public $lazy;
/**
* @var GH6217AssociatedEntity
* @Id
* @Cache("NONSTRICT_READ_WRITE")
* @ManyToOne(targetEntity=GH6217AssociatedEntity::class, fetch="EAGER")
*/
public $eager;
public function __construct(GH6217AssociatedEntity $lazy, GH6217AssociatedEntity $eager)
{
$this->lazy = $lazy;
$this->eager = $eager;
}
}
|