Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eagerly loaded indexed associations use hydrated values instead of database values #11185

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH11149/EagerProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11149;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table('gh11149_eager_product')]
class EagerProduct
{
#[ORM\Id]
#[ORM\Column(type: 'integer')]
public int $id;

#[ORM\OneToMany(
targetEntity: EagerProductTranslation::class,
mappedBy: 'product',
fetch: 'EAGER',
indexBy: 'locale_code',
)]
public Collection $translations;

public function __construct(int $id)
{
$this->id = $id;
$this->translations = new ArrayCollection();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11149;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table('gh11149_eager_product_translation')]
class EagerProductTranslation
{
#[ORM\Id]
#[ORM\ManyToOne(targetEntity: EagerProduct::class, inversedBy: 'translations')]
#[ORM\JoinColumn(nullable: false)]
public EagerProduct $product;

#[ORM\Id]
#[ORM\ManyToOne(targetEntity: Locale::class)]
#[ORM\JoinColumn(name: 'locale_code', referencedColumnName: 'code', nullable: false)]
public Locale $locale;

public function __construct(EagerProduct $product, Locale $locale)
{
$this->product = $product;
$this->locale = $locale;
}
}
108 changes: 108 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH11149/GH11149Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11149;

use Doctrine\ORM\PersistentCollection;
use Doctrine\Persistence\Proxy;
use Doctrine\Tests\OrmFunctionalTestCase;

class GH11149Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();

$this->setUpEntitySchema([
Locale::class,
EagerProduct::class,
EagerProductTranslation::class,
RegularProduct::class,
RegularProductTranslation::class,
]);
}

public function testFetchDefaultModeWithIndexBy(): void
{
// Load entities into database
$this->_em->persist($product = new RegularProduct(11149));
$this->_em->persist($locale = new Locale('de_DE'));
$this->_em->persist(new RegularProductTranslation($product, $locale));
$this->_em->flush();
$this->_em->clear();

// Fetch entity from database
$product = $this->_em->find(RegularProduct::class, 11149);

// Assert associated entity is not loaded eagerly
static::assertInstanceOf(RegularProduct::class, $product);
static::assertInstanceOf(PersistentCollection::class, $product->translations);
static::assertFalse($product->translations->isInitialized());
static::assertCount(1, $product->translations);

// Assert associated entity is indexed by given property
$translation = $product->translations->get('de_DE');
static::assertInstanceOf(RegularProductTranslation::class, $translation);
static::assertNotInstanceOf(Proxy::class, $translation);
}

public function testFetchDefaultModeThroughEagerRepositoryCall(): void
{
// Load entities into database
$this->_em->persist($product = new RegularProduct(11185));
$this->_em->persist($locale = new Locale('nl_NL'));
$this->_em->persist(new RegularProductTranslation($product, $locale));
$this->_em->flush();
$this->_em->clear();

// Fetch entity from database
$queryBuilder = $this->_em->createQueryBuilder();

$queryBuilder
->select('product', 'translations')
->from(RegularProduct::class, 'product')
->leftJoin('product.translations', 'translations')
->where($queryBuilder->expr()->eq('product.id', ':product_id'));

$product = $queryBuilder
->getQuery()
->setParameter('product_id', 11185)
->getSingleResult();

// Assert associated entity is loaded eagerly
static::assertInstanceOf(RegularProduct::class, $product);
static::assertInstanceOf(PersistentCollection::class, $product->translations);
static::assertTrue($product->translations->isInitialized());
static::assertCount(1, $product->translations);

// Assert associated entity is indexed by given property
$translation = $product->translations->get('nl_NL');
static::assertInstanceOf(RegularProductTranslation::class, $translation);
static::assertNotInstanceOf(Proxy::class, $translation);
}

public function testFetchEagerModeWithIndexBy(): void
{
// Load entities into database
$this->_em->persist($product = new EagerProduct(11149));
$this->_em->persist($locale = new Locale('fr_FR'));
$this->_em->persist(new EagerProductTranslation($product, $locale));
$this->_em->flush();
$this->_em->clear();

// Fetch entity from database
$product = $this->_em->find(EagerProduct::class, 11149);

// Assert associated entity is loaded eagerly
static::assertInstanceOf(EagerProduct::class, $product);
static::assertInstanceOf(PersistentCollection::class, $product->translations);
static::assertTrue($product->translations->isInitialized());
static::assertCount(1, $product->translations);

// Assert associated entity is indexed by given property
$translation = $product->translations->get('fr_FR');
static::assertInstanceOf(EagerProductTranslation::class, $translation);
static::assertNotInstanceOf(Proxy::class, $translation);
}
}
21 changes: 21 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH11149/Locale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11149;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table('gh11149_locale')]
class Locale
{
#[ORM\Id]
#[ORM\Column(type: 'string', enumType: LocaleCode::class)]
public LocaleCode $code;

public function __construct(string $code)
{
$this->code = LocaleCode::from($code);
}
}
12 changes: 12 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH11149/LocaleCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11149;

enum LocaleCode: string
{
case Dutch = 'nl_NL';

Check failure on line 9 in tests/Tests/ORM/Functional/Ticket/GH11149/LocaleCode.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
case French = 'fr_FR';
case German = 'de_DE';
}
31 changes: 31 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH11149/RegularProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11149;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table('gh11149_regular_product')]
class RegularProduct
{
#[ORM\Id]
#[ORM\Column(type: 'integer')]
public int $id;

#[ORM\OneToMany(
targetEntity: RegularProductTranslation::class,
mappedBy: 'product',
indexBy: 'locale_code',
)]
public Collection $translations;

public function __construct(int $id)
{
$this->id = $id;
$this->translations = new ArrayCollection();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH11149;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table('gh11149_regular_product_translation')]
class RegularProductTranslation
{
#[ORM\Id]
#[ORM\ManyToOne(targetEntity: RegularProduct::class, inversedBy: 'translations')]
#[ORM\JoinColumn(nullable: false)]
public RegularProduct $product;

#[ORM\Id]
#[ORM\ManyToOne(targetEntity: Locale::class)]
#[ORM\JoinColumn(name: 'locale_code', referencedColumnName: 'code', nullable: false)]
public Locale $locale;

public function __construct(RegularProduct $product, Locale $locale)
{
$this->product = $product;
$this->locale = $locale;
}
}
Loading