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

Failing test for GH8443 #8855

Merged
merged 2 commits into from
Aug 3, 2021
Merged
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 @@ -1109,7 +1109,7 @@ public function walkJoinAssociationDeclaration($joinAssociationDeclaration, $joi
if ($targetClass->isInheritanceTypeJoined()) {
$ctiJoins = $this->generateClassTableInheritanceJoins($targetClass, $joinedDqlAlias);
// If we have WITH condition, we need to build nested joins for target class table and cti joins
if ($withCondition) {
if ($withCondition && $ctiJoins) {
$sql .= '(' . $targetTableJoin['table'] . $ctiJoins . ') ON ' . $targetTableJoin['condition'];
} else {
$sql .= $targetTableJoin['table'] . ' ON ' . $targetTableJoin['condition'] . $ctiJoins;
Expand Down
140 changes: 140 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH8443Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\Query;
use Doctrine\Tests\Models\Company\CompanyManager;
use Doctrine\Tests\Models\Company\CompanyPerson;
use Doctrine\Tests\OrmFunctionalTestCase;

use function assert;
use function count;

final class GH8443Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
$this->useModelSet('company');
parent::setUp();
$this->_schemaTool->createSchema([$this->_em->getClassMetadata(GH8443Foo::class)]);
}

protected function tearDown(): void
{
parent::tearDown();
$this->_schemaTool->dropSchema([$this->_em->getClassMetadata(GH8443Foo::class)]);
}

/**
* @group GH-8443
*/
public function testJoinRootEntityWithForcePartialLoad(): void
greg0ire marked this conversation as resolved.
Show resolved Hide resolved
{
$person = new CompanyPerson();
$person->setName('John');

$manager = new CompanyManager();
$manager->setName('Adam');
$manager->setSalary(1000);
$manager->setDepartment('IT');
$manager->setTitle('manager');

$manager->setSpouse($person);

$this->_em->persist($person);
$this->_em->persist($manager);
$this->_em->flush();
$this->_em->clear();

$manager = $this->_em->createQuery(
"SELECT m from Doctrine\Tests\Models\Company\CompanyManager m
JOIN m.spouse s
WITH s.name = 'John'"
)->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true)->getSingleResult();
$this->_em->refresh($manager);

$this->assertEquals('John', $manager->getSpouse()->getName());
}

/**
* @group GH-8443
*/
public function testJoinRootEntityWithOnlyOneEntityInHierarchy(): void
{
$bar = new GH8443Foo('bar');

$foo = new GH8443Foo('foo');
$foo->setBar($bar);

$this->_em->persist($bar);
$this->_em->persist($foo);
$this->_em->flush();
$this->_em->clear();

$foo = $this->_em->createQuery(
'SELECT f from ' . GH8443Foo::class . " f JOIN f.bar b WITH b.name = 'bar'"
)->getSingleResult();
assert($foo instanceof GH8443Foo);

$bar = $foo->getBar();
assert($bar !== null);
$this->assertEquals('bar', $bar->getName());
}
}
/**
* @Entity
* @Table(name="GH2947_foo")
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({
* "foo" = "GH8443Foo"
* })
*/
class GH8443Foo
{
/**
* @var int|null
* @Id
* @Column(type="integer")
* @GeneratedValue
*/
private $id;

/**
* @var string
* @Column
*/
private $name;

/**
* @var GH8443Foo|null
* @OneToOne(targetEntity="GH8443Foo")
* @JoinColumn(name="bar_id", referencedColumnName="id")
*/
private $bar;

public function __construct(string $name)
{
$this->name = $name;
}

public function getName(): ?string
{
return $this->name;
}

public function setBar(GH8443Foo $bar): void
{
if ($bar !== $this->bar) {
$this->bar = $bar;
$this->bar->bar = $this;
}
}

public function getBar(): ?GH8443Foo
{
return $this->bar;
}
}