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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
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\ORM\Mapping\OneToMany;
use Doctrine\ORM\Query;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;
#[Group('DDC-618')]
class DDC618Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->createSchemaForModels(DDC618Author::class, DDC618Book::class);
try {
// Create author 10/Joe with two books 22/JoeA and 20/JoeB
$author = new DDC618Author();
$author->id = 10;
$author->name = 'Joe';
$this->_em->persist($author);
// Create author 11/Alice with two books 21/AliceA and 23/AliceB
$author = new DDC618Author();
$author->id = 11;
$author->name = 'Alice';
$author->addBook('In Wonderland');
$author->addBook('Reloaded');
$author->addBook('Test');
$this->_em->persist($author);
$this->_em->flush();
$this->_em->clear();
} catch (UniqueConstraintViolationException) {
}
}
public function testIndexByHydrateObject(): void
{
$dql = 'SELECT A FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Author A INDEX BY A.name ORDER BY A.name ASC';
$result = $this->_em->createQuery($dql)->getResult(Query::HYDRATE_OBJECT);
$joe = $this->_em->find(DDC618Author::class, 10);
$alice = $this->_em->find(DDC618Author::class, 11);
self::assertArrayHasKey('Joe', $result, "INDEX BY A.name should return an index by the name of 'Joe'.");
self::assertArrayHasKey('Alice', $result, "INDEX BY A.name should return an index by the name of 'Alice'.");
}
public function testIndexByHydrateArray(): void
{
$dql = 'SELECT A FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Author A INDEX BY A.name ORDER BY A.name ASC';
$result = $this->_em->createQuery($dql)->getResult(Query::HYDRATE_ARRAY);
$joe = $this->_em->find(DDC618Author::class, 10);
$alice = $this->_em->find(DDC618Author::class, 11);
self::assertArrayHasKey('Joe', $result, "INDEX BY A.name should return an index by the name of 'Joe'.");
self::assertArrayHasKey('Alice', $result, "INDEX BY A.name should return an index by the name of 'Alice'.");
}
#[Group('DDC-1018')]
public function testIndexByJoin(): void
{
$dql = 'SELECT A, B FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Author A ' .
'INNER JOIN A.books B INDEX BY B.title ORDER BY A.name ASC';
$result = $this->_em->createQuery($dql)->getResult(Query::HYDRATE_OBJECT);
self::assertCount(3, $result[0]->books); // Alice, Joe doesn't appear because he has no books.
self::assertEquals('Alice', $result[0]->name);
self::assertTrue(isset($result[0]->books['In Wonderland']), 'Indexing by title should have books by title.');
self::assertTrue(isset($result[0]->books['Reloaded']), 'Indexing by title should have books by title.');
self::assertTrue(isset($result[0]->books['Test']), 'Indexing by title should have books by title.');
$result = $this->_em->createQuery($dql)->getResult(Query::HYDRATE_ARRAY);
self::assertCount(3, $result[0]['books']); // Alice, Joe doesn't appear because he has no books.
self::assertEquals('Alice', $result[0]['name']);
self::assertTrue(isset($result[0]['books']['In Wonderland']), 'Indexing by title should have books by title.');
self::assertTrue(isset($result[0]['books']['Reloaded']), 'Indexing by title should have books by title.');
self::assertTrue(isset($result[0]['books']['Test']), 'Indexing by title should have books by title.');
}
#[Group('DDC-1018')]
public function testIndexByToOneJoinSilentlyIgnored(): void
{
$dql = 'SELECT B, A FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Book B ' .
'INNER JOIN B.author A INDEX BY A.name ORDER BY A.name ASC';
$result = $this->_em->createQuery($dql)->getResult(Query::HYDRATE_OBJECT);
self::assertInstanceOf(DDC618Book::class, $result[0]);
self::assertInstanceOf(DDC618Author::class, $result[0]->author);
$dql = 'SELECT B, A FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Book B ' .
'INNER JOIN B.author A INDEX BY A.name ORDER BY A.name ASC';
$result = $this->_em->createQuery($dql)->getResult(Query::HYDRATE_ARRAY);
self::assertEquals('Alice', $result[0]['author']['name']);
}
#[Group('DDC-1018')]
public function testCombineIndexBy(): void
{
$dql = 'SELECT A, B FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Author A INDEX BY A.id ' .
'INNER JOIN A.books B INDEX BY B.title ORDER BY A.name ASC';
$result = $this->_em->createQuery($dql)->getResult(Query::HYDRATE_OBJECT);
self::assertArrayHasKey(11, $result); // Alice
self::assertCount(3, $result[11]->books); // Alice, Joe doesn't appear because he has no books.
self::assertEquals('Alice', $result[11]->name);
self::assertTrue(isset($result[11]->books['In Wonderland']), 'Indexing by title should have books by title.');
self::assertTrue(isset($result[11]->books['Reloaded']), 'Indexing by title should have books by title.');
self::assertTrue(isset($result[11]->books['Test']), 'Indexing by title should have books by title.');
}
}
#[Entity]
class DDC618Author
{
/** @var int */
#[Id]
#[Column(type: 'integer')]
public $id;
/** @var string */
#[Column(type: 'string', length: 255)]
public $name;
/** @phpstan-var Collection<int, DDC618Book> */
#[OneToMany(targetEntity: 'DDC618Book', mappedBy: 'author', cascade: ['persist'])]
public $books;
public function __construct()
{
$this->books = new ArrayCollection();
}
public function addBook(string $title): void
{
$book = new DDC618Book($title, $this);
$this->books[] = $book;
}
}
#[Entity]
class DDC618Book
{
/** @var int */
#[Id]
#[GeneratedValue]
#[Column(type: 'integer')]
public $id;
public function __construct(
#[Column(type: 'string', length: 255)]
public string $title,
#[ManyToOne(targetEntity: 'DDC618Author', inversedBy: 'books')]
public DDC618Author $author,
) {
}
}
|