Skip to content

Commit 38360e9

Browse files
committed
Merge pull request #542: Fix columns mapping in BelongsToMorphedLoader
2 parents 396900f + f27307a commit 38360e9

File tree

6 files changed

+51
-42
lines changed

6 files changed

+51
-42
lines changed

psalm-baseline.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,17 @@
258258
<InvalidThrow>
259259
<code><![CDATA[throw $state->getLastError()]]></code>
260260
</InvalidThrow>
261+
<PossiblyUnusedReturnValue>
262+
<code><![CDATA[StateInterface]]></code>
263+
</PossiblyUnusedReturnValue>
261264
<PropertyNotSetInConstructor>
262265
<code><![CDATA[$unitOfWork]]></code>
263266
</PropertyNotSetInConstructor>
264267
</file>
265268
<file src="src/EntityManagerInterface.php">
266269
<PossiblyUnusedReturnValue>
270+
<code><![CDATA[StateInterface]]></code>
271+
<code><![CDATA[self]]></code>
267272
<code><![CDATA[static]]></code>
268273
</PossiblyUnusedReturnValue>
269274
</file>
@@ -2341,7 +2346,6 @@
23412346
<code><![CDATA[$row]]></code>
23422347
<code><![CDATA[$self->scope]]></code>
23432348
<code><![CDATA[$this->morphKey]]></code>
2344-
<code><![CDATA[$where[$this->outerKey[$i]]]]></code>
23452349
</MixedAssignment>
23462350
<MixedMethodCall>
23472351
<code><![CDATA[parseRow]]></code>
@@ -2350,6 +2354,9 @@
23502354
<code><![CDATA[$grouped]]></code>
23512355
<code><![CDATA[array<non-empty-string, array<non-empty-string, mixed>>]]></code>
23522356
</MixedReturnTypeCoercion>
2357+
<PossiblyNullOperand>
2358+
<code><![CDATA[$this->fieldAlias($this->outerKey[$i])]]></code>
2359+
</PossiblyNullOperand>
23532360
<PropertyNotSetInConstructor>
23542361
<code><![CDATA[BelongsToMorphedLoader]]></code>
23552362
<code><![CDATA[BelongsToMorphedLoader]]></code>

src/Select/Loader/Morphed/BelongsToMorphedLoader.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,10 @@ public function isLoaded(): bool
103103
private function applyCriteria(SelectQuery $query, array $criteria): SelectQuery
104104
{
105105
// Map criteria to inner keys
106-
$where = [];
107106
foreach ($this->innerKey as $i => $key) {
108-
$where[$this->outerKey[$i]] = $criteria[$key];
107+
$query->where($this->getAlias() . '.' . $this->fieldAlias($this->outerKey[$i]), $criteria[$key]);
109108
}
110109

111-
$query->where(\key($where), \reset($where));
112-
113110
return $query;
114111
}
115112

tests/ORM/Functional/Driver/Common/Integration/Case428/CaseTest.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428;
66

7-
use Cycle\ORM\EntityManager;
87
use Cycle\ORM\Select;
98
use Cycle\ORM\Tests\Functional\Driver\Common\BaseTest;
109
use Cycle\ORM\Tests\Functional\Driver\Common\Integration\IntegrationTestTrait;
@@ -32,17 +31,14 @@ public function testCreate(): void
3231
$user = new Entity\User('Test User', 'test@example.com');
3332
$post = new Entity\Post('New title', 'New content');
3433
$post->user = $user;
34+
$user->id = 42;
3535

36-
$em = new EntityManager($this->orm);
37-
$em->persist($post);
38-
$em->run();
36+
$this->save($post);
3937

4038
$post->best_comment = new Entity\Comment(42, 'New comment content', $post, $user);
4139

4240
// Store changes and calc write queries
43-
$em = new EntityManager($this->orm);
44-
$em->persist($post);
45-
$em->run();
41+
$this->save($post);
4642

4743
// Check write queries count
4844
$this->orm->getHeap()->clean();
@@ -64,15 +60,15 @@ public function setUp(): void
6460
private function makeTables(): void
6561
{
6662
$this->makeTable('user', [
67-
'id' => 'int',
63+
'id' => 'int,primary',
6864
'name' => 'string',
6965
'email' => 'string',
7066
'created_at' => 'datetime',
7167
'updated_at' => 'datetime',
7268
]);
7369

7470
$this->makeTable('category', [
75-
'id' => 'int',
71+
'id' => 'int,primary',
7672
'name' => 'string',
7773
'created_at' => 'datetime',
7874
'updated_at' => 'datetime',
@@ -91,7 +87,7 @@ private function makeTables(): void
9187
]);
9288

9389
$this->makeTable('comment', [
94-
'id' => 'int',
90+
'id' => 'int,primary',
9591
'content' => 'string',
9692
'post_id' => 'int',
9793
'user_id' => 'int',
@@ -128,7 +124,7 @@ private function fillData(): void
128124
$this->getDatabase()->table('post')->insertMultiple(
129125
['title', 'content', 'best_comment_id', 'user_id', 'category_id', 'data'],
130126
[
131-
['Title 1', 'Foo-bar-baz content 1', 2, 1, 1, 'metadata'],
127+
['Title 1', 'Foo-bar-baz content 1', null, 1, 1, 'metadata'],
132128
],
133129
);
134130

@@ -140,5 +136,11 @@ private function fillData(): void
140136
[3, 1, 1, 'Foo-bar-baz comment 3'],
141137
],
142138
);
139+
140+
$this->getDatabase()->table('post')->update([
141+
'best_comment_id' => 2,
142+
], [
143+
'id' => 1,
144+
])->run();
143145
}
144146
}

tests/ORM/Functional/Driver/Common/Integration/Issue356/CaseTest.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function testSelectAll(): void
6969
$this->captureReadQueries();
7070
foreach ($logs as $log) {
7171
$this->assertInstanceOf(Entity\LogRecord::class, $log);
72-
\in_array($log->id, [9], true)
72+
\in_array($log->lid, [9], true)
7373
? self::assertNull($log->actor)
7474
: self::assertInstanceOf(Entity\Actor::class, $log->actor);
7575
}
@@ -80,6 +80,7 @@ public function testUpdateMany(): void
8080
{
8181
// Eager load morphed relation
8282
$this->captureReadQueries();
83+
/** @var list<Entity\LogRecord> $logs */
8384
$logs = (new Select($this->orm, Entity\LogRecord::class))
8485
->fetchAll();
8586
$this->assertNumReads(1);
@@ -95,7 +96,7 @@ public function testUpdateMany(): void
9596
// Check result
9697
$this->captureReadQueries();
9798
foreach ($logs as $log) {
98-
\in_array($log->id, [9], true)
99+
\in_array($log->lid, [9], true)
99100
? self::assertNull($log->actor)
100101
: self::assertInstanceOf(Entity\Actor::class, $log->actor);
101102
}
@@ -110,7 +111,7 @@ public function testScoped(): void
110111
$logs = (new Select($this->orm, Entity\LogRecord::class))
111112
->load('actor')
112113
->wherePK(9, 10)
113-
->orderBy('id')
114+
->orderBy('l_id')
114115
->fetchAll();
115116
$this->assertNumReads(3);
116117

@@ -136,19 +137,19 @@ public function setUp(): void
136137
private function makeTables(): void
137138
{
138139
$this->makeTable(Entity\LogRecord::ROLE, [
139-
'id' => 'primary', // autoincrement
140-
'message' => 'string',
141-
'actor_id' => 'int,nullable',
142-
'actor_type' => 'string,nullable',
143-
'created_at' => 'datetime',
140+
'l_id' => 'primary', // autoincrement
141+
'l_message' => 'string',
142+
'l_actor_id' => 'int,nullable',
143+
'l_actor_type' => 'string,nullable',
144+
'l_created_at' => 'datetime',
144145
]);
145146

146147
$this->makeTable(Entity\User::ROLE, [
147148
// The columns order is matters here for testSelectAll purpose
148-
'active' => 'bool',
149-
'name' => 'string',
150-
'id' => 'primary',
151-
'created_at' => 'datetime',
149+
'u_active' => 'bool',
150+
'u_name' => 'string',
151+
'u_id' => 'primary',
152+
'u_created_at' => 'datetime',
152153
]);
153154

154155
$this->makeTable(Entity\Tenant::ROLE, [
@@ -161,7 +162,7 @@ private function makeTables(): void
161162
private function fillData(): void
162163
{
163164
$this->getDatabase()->table(Entity\User::ROLE)->insertMultiple(
164-
['name', 'active'],
165+
['u_name', 'u_active'],
165166
[
166167
['user-1', true],
167168
['user-2', true],
@@ -181,7 +182,7 @@ private function fillData(): void
181182
],
182183
);
183184
$this->getDatabase()->table(Entity\LogRecord::ROLE)->insertMultiple(
184-
['message', 'actor_type', 'actor_id', 'created_at'],
185+
['l_message', 'l_actor_type', 'l_actor_id', 'l_created_at'],
185186
[
186187
['log-1 for user-1', Entity\User::ROLE, 1, new \DateTimeImmutable()],
187188
['log-2 for user-2', Entity\User::ROLE, 2, new \DateTimeImmutable()],

tests/ORM/Functional/Driver/Common/Integration/Issue356/Entity/LogRecord.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class LogRecord
88
{
99
public const ROLE = 'log';
1010

11-
public ?int $id = null;
11+
public ?int $lid = null;
1212
public string $message;
1313
public string $actor_type;
1414
public int $actor_id;

tests/ORM/Functional/Driver/Common/Integration/Issue356/schema.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
Schema::SOURCE => Source::class,
2121
Schema::DATABASE => 'default',
2222
Schema::TABLE => 'log',
23-
Schema::PRIMARY_KEY => ['id'],
24-
Schema::FIND_BY_KEYS => ['id'],
23+
Schema::PRIMARY_KEY => ['lid'],
24+
Schema::FIND_BY_KEYS => ['lid'],
2525
Schema::COLUMNS => [
26-
'id' => 'id',
27-
'message' => 'message',
28-
'actor_id' => 'actor_id',
29-
'actor_type' => 'actor_type',
30-
'created_at' => 'created_at',
26+
'lid' => 'l_id',
27+
'message' => 'l_message',
28+
'actor_id' => 'l_actor_id',
29+
'actor_type' => 'l_actor_type',
30+
'created_at' => 'l_created_at',
3131
],
3232
Schema::RELATIONS => [
3333
'actor' => [
@@ -46,7 +46,7 @@
4646
],
4747
Schema::SCOPE => null,
4848
Schema::TYPECAST => [
49-
'id' => 'int',
49+
'lid' => 'int',
5050
'message' => 'string',
5151
'actor_id' => 'int',
5252
'actor_type' => 'string',
@@ -64,16 +64,18 @@
6464
Schema::PRIMARY_KEY => ['id'],
6565
Schema::FIND_BY_KEYS => ['id'],
6666
Schema::COLUMNS => [
67-
'id' => 'id',
68-
'name' => 'name',
69-
'created_at' => 'created_at',
67+
'id' => 'u_id',
68+
'name' => 'u_name',
69+
'created_at' => 'u_created_at',
70+
'active' => 'u_active',
7071
],
7172
Schema::RELATIONS => [],
7273
Schema::SCOPE => ActiveScope::class,
7374
Schema::TYPECAST => [
7475
'id' => 'int',
7576
'name' => 'string',
7677
'created_at' => 'datetime',
78+
'active' => 'bool',
7779
],
7880
Schema::SCHEMA => [],
7981
],

0 commit comments

Comments
 (0)