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

Consider usage of setFetchMode when checking for simultaneous usage of fetch-mode EAGER and WITH condition #11174

Closed
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
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Query/SqlWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ public function walkJoinAssociationDeclaration($joinAssociationDeclaration, $joi
}
}

if ($relation['fetch'] === ClassMetadata::FETCH_EAGER && $condExpr !== null) {
if (($this->query->getHint('fetchMode')[$assoc['sourceEntity']][$assoc['fieldName']] ?? $relation['fetch']) === ClassMetadata::FETCH_EAGER && $condExpr !== null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (($this->query->getHint('fetchMode')[$assoc['sourceEntity']][$assoc['fieldName']] ?? $relation['fetch']) === ClassMetadata::FETCH_EAGER && $condExpr !== null) {
$fetchMode = $this->query->getHint('fetchMode')[$assoc['sourceEntity']][$assoc['fieldName']] ?? $relation['fetch'];
if ($fetchMode === ClassMetadata::FETCH_EAGER && $condExpr !== null) {

throw QueryException::eagerFetchJoinWithNotAllowed($assoc['sourceEntity'], $assoc['fieldName']);
}

Expand Down
97 changes: 97 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH11128Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Query;
use Doctrine\Tests\OrmFunctionalTestCase;

class GH11128Test extends OrmFunctionalTestCase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding a new test-case and model, you can just add another test to the existing by adapting for example EagerFetchCollectionTest::testSubselectFetchJoinWithNotAllowed

{
protected function setUp(): void
{
parent::setUp();

$this->setUpEntitySchema([
GH11128Owner::class,
GH11128Inverse::class,
]);
}

public function testSetFetchModeEagerWhenNotEagerInMapping(): void
{
$query = $this->_em->createQueryBuilder()
->select('o', 'i')
->from(GH11128Owner::class, 'o')
->innerJoin('o.inverseLazy', 'i', Query\Expr\Join::WITH, 'i.field IS NOT NULL')
->getQuery();
$query->setFetchMode(GH11128Owner::class, 'inverseLazy', ORM\ClassMetadataInfo::FETCH_EAGER);
$this->expectExceptionMessage('Associations with fetch-mode=EAGER may not be using WITH conditions in
"' . GH11128Owner::class . '#inverseLazy".');
$query->getSql();
}

public function testSetFetchModeNotEagerWhenEagerInMapping(): void
{
$query = $this->_em->createQueryBuilder()
->select('o', 'i')
->from(GH11128Owner::class, 'o')
->innerJoin('o.inverseEager', 'i', Query\Expr\Join::WITH, 'i.field IS NOT NULL')
->getQuery();
$query->setFetchMode(GH11128Owner::class, 'inverseEager', ORM\ClassMetadataInfo::FETCH_LAZY);
$this->assertIsString($query->getSql());
}
}

/**
* @ORM\Entity
*/
class GH11128Owner
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @var ?int
*/
public $id;

/**
* @ORM\ManyToOne(targetEntity="GH11128Inverse", fetch="EAGER")
*
* @var GH11128Inverse
*/
private $inverseEager;

/**
* @ORM\ManyToOne(targetEntity="GH11128Inverse", fetch="LAZY")
*
* @var GH11128Inverse
*/
private $inverseLazy;
}

/**
* @ORM\Entity
*/
class GH11128Inverse
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*
* @var ?int
*/
public $id;

/**
* @ORM\Column(type="text", nullable=true)
*
* @var string
*/
private $field;
}