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

provided passing test for mapping joins in native queries #9602

Closed
wants to merge 1 commit into from
Closed
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
148 changes: 148 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH9600Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
use Doctrine\ORM\Internal\SQLResultCasing;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Query\ResultSetMappingBuilder;
use Doctrine\Tests\OrmFunctionalTestCase;

/**
* @group GH-9600
*/
class GH9600Test extends OrmFunctionalTestCase
{
use SQLResultCasing;
use VerifyDeprecations;

/** @var AbstractPlatform */
private $platform = null;

/** @var array<string, list<class-string>> */
protected static $modelSets = [
'native_query_test' => [
GH9600User::class,
GH9600Address::class,
],
];

protected function setUp(): void
{
$this->useModelSet('native_query_test');
parent::setUp();

$this->platform = $this->_em->getConnection()->getDatabasePlatform();
}

public function testBasicNativeQuery(): void
{
$this->addRowToDb('Homer', 'Springfield', 'Evergreen Terrace');
$this->addRowToDb('Sherlock', 'London', 'Baker Street');

$rsm = new ResultSetMappingBuilder($this->_em);
$rsm->addRootEntityFromClassMetadata(GH9600User::class, 'u');
$rsm->addJoinedEntityFromClassMetadata(GH9600Address::class, 'a', 'u', 'address');

$query = '
SELECT
u.*,
a.*
FROM users u
LEFT JOIN addresses a ON (u.address_id = a.a_id)
';

$native = $this->_em->createNativeQuery($query, $rsm);
$result1 = $native->getResult();

$this->_em->clear();

$query = '
SELECT
a.*,
u.*
FROM users u
LEFT JOIN addresses a ON (u.address_id = a.a_id)
';

$native = $this->_em->createNativeQuery($query, $rsm);

$result2 = $native->getResult();

self::assertEquals($result1, $result2);
}

private function addRowToDb(string $name, string $city, string $street): void
{
$address = new GH9600Address();
$address->city = $city;
$address->street = $street;
$this->_em->persist($address);
$this->_em->flush();

$user = new GH9600User();
$user->name = $name;
$user->address = $address;
$this->_em->persist($user);
$this->_em->flush();

$this->_em->clear();
}
}

/**
* @ORM\Table(name="addresses")
* @ORM\Entity
*/
class GH9600Address
{
/**
* @ORM\Id
* @ORM\Column(type="integer", name="a_id")
* @ORM\GeneratedValue
*/
public $id;

/**
* @ORM\Column(type="string", name="a_street")
*/
public $street;

/**
* @ORM\Column(type="string", name="a_city")
*/
public $city;

/**
* @ORM\OneToOne(targetEntity="GH9600User", mappedBy="address")
*/
public $user;
}

/**
* @ORM\Table(name="users")
* @ORM\Entity
*/
class GH9600User
{
/**
* @ORM\Id
* @ORM\Column(type="integer",name="u_id")
* @ORM\GeneratedValue
*/
public $id;

/**
* @ORM\Column(type="string", name="u_name")
*/
public $name;

/**
* @ORM\OneToOne(targetEntity="GH9600Address", inversedBy="user")
* @ORM\JoinColumn(referencedColumnName="a_id")
*/
public $address;
}