Skip to content

Commit fa3263e

Browse files
Fix bug when a JTI parent had children fields (#65)
Co-authored-by: Maxim Smakouz <m.smakouz@gmail.com>
1 parent 94181a9 commit fa3263e

8 files changed

Lines changed: 124 additions & 5 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"require": {
99
"php": ">=8.0",
1010
"cycle/orm": "^2.2.0",
11-
"cycle/schema-builder": "^2.2",
11+
"cycle/schema-builder": "^2.3",
1212
"doctrine/annotations": "^1.13 || ^2.0",
1313
"spiral/attributes": "^2.8|^3.0",
1414
"spiral/tokenizer": "^2.8|^3.0",

src/Entities.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function run(Registry $registry): Registry
8181
}
8282

8383
foreach ($children as $e) {
84-
$registry->registerChild($registry->getEntity($this->utils->findParent($e->getClass())), $e);
84+
$registry->registerChildWithoutMerge($registry->getEntity($this->utils->findParent($e->getClass())), $e);
8585
}
8686

8787
return $this->normalizeNames($registry);
@@ -157,7 +157,7 @@ private function resolveTarget(Registry $registry, string $name): ?string
157157
return $name;
158158
}
159159

160-
if (! $registry->hasEntity($name)) {
160+
if (!$registry->hasEntity($name)) {
161161
// point all relations to the parent
162162
foreach ($registry as $entity) {
163163
foreach ($registry->getChildren($entity) as $child) {

src/TableInheritance.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ private function initInheritance(
133133
$parent->getInheritance()->setDiscriminator($annotation->getName());
134134
}
135135

136+
$parent->merge($entity);
137+
136138
return $parent;
137139
}
138140
if ($inheritance instanceof Inheritance\JoinedTable) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Annotated\Tests\Fixtures\Fixtures21;
6+
7+
use Cycle\Annotated\Annotation\Column;
8+
use Cycle\Annotated\Annotation\Entity;
9+
use Cycle\Annotated\Annotation\Inheritance\JoinedTable;
10+
11+
/**
12+
* @Entity
13+
* @JoinedTable
14+
*/
15+
#[Entity]
16+
#[JoinedTable]
17+
class Buyer extends Person
18+
{
19+
/** @Column(type="string") */
20+
#[Column(type: 'string')]
21+
public string $foo;
22+
23+
/** @Column(type="string") */
24+
#[Column(type: 'string')]
25+
public string $bar;
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Annotated\Tests\Fixtures\Fixtures21;
6+
7+
use Cycle\Annotated\Annotation\Column;
8+
use Cycle\Annotated\Annotation\Entity;
9+
use Cycle\Annotated\Annotation\Inheritance\JoinedTable;
10+
11+
/**
12+
* @Entity
13+
* @JoinedTable
14+
*/
15+
#[Entity]
16+
#[JoinedTable]
17+
class Employee extends Person
18+
{
19+
/** @Column(type="integer") */
20+
#[Column(type: 'integer')]
21+
public int $salary;
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Annotated\Tests\Fixtures\Fixtures21;
6+
7+
use Cycle\Annotated\Annotation\Column;
8+
use Cycle\Annotated\Annotation\Entity;
9+
use Cycle\Annotated\Annotation\Inheritance\JoinedTable;
10+
11+
/**
12+
* @Entity
13+
* @JoinedTable
14+
*/
15+
#[Entity]
16+
#[JoinedTable]
17+
class Executive extends Employee
18+
{
19+
/** @Column(type="integer") */
20+
#[Column(type: 'integer')]
21+
public int $bonus;
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\Annotated\Tests\Fixtures\Fixtures21;
6+
7+
use Cycle\Annotated\Annotation\Column;
8+
use Cycle\Annotated\Annotation\Entity;
9+
10+
/**
11+
* @Entity
12+
*/
13+
#[Entity]
14+
class Person
15+
{
16+
/** @Column(type="primary") */
17+
#[Column(type: 'primary')]
18+
protected int $id;
19+
20+
/** @Column(type="string") */
21+
#[Column(type: 'string')]
22+
public string $name;
23+
24+
/** @Column(type="string") */
25+
#[Column(type: 'string')]
26+
public string $type;
27+
}

tests/Annotated/Functional/Driver/Common/Inheritance/JoinedTableTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Cycle\Database\Schema\AbstractIndex;
1717
use Cycle\ORM\EntityManager;
1818
use Cycle\ORM\Schema;
19+
use Cycle\ORM\SchemaInterface;
1920
use Cycle\Schema\Compiler;
2021
use Cycle\Schema\Generator\GenerateRelations;
2122
use Cycle\Schema\Generator\GenerateTypecast;
@@ -192,11 +193,30 @@ public function testAddIndex(ReaderInterface $reader): void
192193
$this->assertTrue(end($indexes)->isUnique());
193194
}
194195

195-
private function compile(ReaderInterface $reader): array
196+
/**
197+
* @dataProvider allReadersProvider
198+
*/
199+
public function testJtiParentColumns(ReaderInterface $reader): void
200+
{
201+
$schema = $this->compile($reader, 'Fixtures21');
202+
203+
$this->assertNotEmpty($schema);
204+
205+
$this->assertArrayHasKey(SchemaInterface::COLUMNS, $schema['person']);
206+
207+
// assert that parent doesn't have jti columns
208+
$this->assertSame([
209+
'id' => 'id',
210+
'name' => 'name',
211+
'type' => 'type',
212+
], $schema['person'][SchemaInterface::COLUMNS]);
213+
}
214+
215+
private function compile(ReaderInterface $reader, string $fixtures = 'Fixtures16'): array
196216
{
197217
$tokenizer = new Tokenizer(
198218
new TokenizerConfig([
199-
'directories' => [__DIR__ . '/../../../../Fixtures/Fixtures16'],
219+
'directories' => [sprintf(__DIR__ . '/../../../../Fixtures/%s', $fixtures)],
200220
'exclude' => [],
201221
])
202222
);

0 commit comments

Comments
 (0)