diff --git a/psalm-baseline.xml b/psalm-baseline.xml index b1a1d970..e111f6b7 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -258,12 +258,17 @@ getLastError()]]> + + + + + @@ -2341,7 +2346,6 @@ scope]]> morphKey]]> - outerKey[$i]]]]> @@ -2350,6 +2354,9 @@ >]]> + + fieldAlias($this->outerKey[$i])]]> + diff --git a/src/Select/Loader/Morphed/BelongsToMorphedLoader.php b/src/Select/Loader/Morphed/BelongsToMorphedLoader.php index d6763e47..89b56ef5 100644 --- a/src/Select/Loader/Morphed/BelongsToMorphedLoader.php +++ b/src/Select/Loader/Morphed/BelongsToMorphedLoader.php @@ -103,13 +103,10 @@ public function isLoaded(): bool private function applyCriteria(SelectQuery $query, array $criteria): SelectQuery { // Map criteria to inner keys - $where = []; foreach ($this->innerKey as $i => $key) { - $where[$this->outerKey[$i]] = $criteria[$key]; + $query->where($this->getAlias() . '.' . $this->fieldAlias($this->outerKey[$i]), $criteria[$key]); } - $query->where(\key($where), \reset($where)); - return $query; } diff --git a/tests/ORM/Functional/Driver/Common/Integration/Case428/CaseTest.php b/tests/ORM/Functional/Driver/Common/Integration/Case428/CaseTest.php index 3af27f1b..3b9669b1 100644 --- a/tests/ORM/Functional/Driver/Common/Integration/Case428/CaseTest.php +++ b/tests/ORM/Functional/Driver/Common/Integration/Case428/CaseTest.php @@ -4,7 +4,6 @@ namespace Cycle\ORM\Tests\Functional\Driver\Common\Integration\Case428; -use Cycle\ORM\EntityManager; use Cycle\ORM\Select; use Cycle\ORM\Tests\Functional\Driver\Common\BaseTest; use Cycle\ORM\Tests\Functional\Driver\Common\Integration\IntegrationTestTrait; @@ -32,17 +31,14 @@ public function testCreate(): void $user = new Entity\User('Test User', 'test@example.com'); $post = new Entity\Post('New title', 'New content'); $post->user = $user; + $user->id = 42; - $em = new EntityManager($this->orm); - $em->persist($post); - $em->run(); + $this->save($post); $post->best_comment = new Entity\Comment(42, 'New comment content', $post, $user); // Store changes and calc write queries - $em = new EntityManager($this->orm); - $em->persist($post); - $em->run(); + $this->save($post); // Check write queries count $this->orm->getHeap()->clean(); @@ -64,7 +60,7 @@ public function setUp(): void private function makeTables(): void { $this->makeTable('user', [ - 'id' => 'int', + 'id' => 'int,primary', 'name' => 'string', 'email' => 'string', 'created_at' => 'datetime', @@ -72,7 +68,7 @@ private function makeTables(): void ]); $this->makeTable('category', [ - 'id' => 'int', + 'id' => 'int,primary', 'name' => 'string', 'created_at' => 'datetime', 'updated_at' => 'datetime', @@ -91,7 +87,7 @@ private function makeTables(): void ]); $this->makeTable('comment', [ - 'id' => 'int', + 'id' => 'int,primary', 'content' => 'string', 'post_id' => 'int', 'user_id' => 'int', @@ -128,7 +124,7 @@ private function fillData(): void $this->getDatabase()->table('post')->insertMultiple( ['title', 'content', 'best_comment_id', 'user_id', 'category_id', 'data'], [ - ['Title 1', 'Foo-bar-baz content 1', 2, 1, 1, 'metadata'], + ['Title 1', 'Foo-bar-baz content 1', null, 1, 1, 'metadata'], ], ); @@ -140,5 +136,11 @@ private function fillData(): void [3, 1, 1, 'Foo-bar-baz comment 3'], ], ); + + $this->getDatabase()->table('post')->update([ + 'best_comment_id' => 2, + ], [ + 'id' => 1, + ])->run(); } } diff --git a/tests/ORM/Functional/Driver/Common/Integration/Issue356/CaseTest.php b/tests/ORM/Functional/Driver/Common/Integration/Issue356/CaseTest.php index ceb60059..ee129fbe 100644 --- a/tests/ORM/Functional/Driver/Common/Integration/Issue356/CaseTest.php +++ b/tests/ORM/Functional/Driver/Common/Integration/Issue356/CaseTest.php @@ -69,7 +69,7 @@ public function testSelectAll(): void $this->captureReadQueries(); foreach ($logs as $log) { $this->assertInstanceOf(Entity\LogRecord::class, $log); - \in_array($log->id, [9], true) + \in_array($log->lid, [9], true) ? self::assertNull($log->actor) : self::assertInstanceOf(Entity\Actor::class, $log->actor); } @@ -80,6 +80,7 @@ public function testUpdateMany(): void { // Eager load morphed relation $this->captureReadQueries(); + /** @var list $logs */ $logs = (new Select($this->orm, Entity\LogRecord::class)) ->fetchAll(); $this->assertNumReads(1); @@ -95,7 +96,7 @@ public function testUpdateMany(): void // Check result $this->captureReadQueries(); foreach ($logs as $log) { - \in_array($log->id, [9], true) + \in_array($log->lid, [9], true) ? self::assertNull($log->actor) : self::assertInstanceOf(Entity\Actor::class, $log->actor); } @@ -110,7 +111,7 @@ public function testScoped(): void $logs = (new Select($this->orm, Entity\LogRecord::class)) ->load('actor') ->wherePK(9, 10) - ->orderBy('id') + ->orderBy('l_id') ->fetchAll(); $this->assertNumReads(3); @@ -136,19 +137,19 @@ public function setUp(): void private function makeTables(): void { $this->makeTable(Entity\LogRecord::ROLE, [ - 'id' => 'primary', // autoincrement - 'message' => 'string', - 'actor_id' => 'int,nullable', - 'actor_type' => 'string,nullable', - 'created_at' => 'datetime', + 'l_id' => 'primary', // autoincrement + 'l_message' => 'string', + 'l_actor_id' => 'int,nullable', + 'l_actor_type' => 'string,nullable', + 'l_created_at' => 'datetime', ]); $this->makeTable(Entity\User::ROLE, [ // The columns order is matters here for testSelectAll purpose - 'active' => 'bool', - 'name' => 'string', - 'id' => 'primary', - 'created_at' => 'datetime', + 'u_active' => 'bool', + 'u_name' => 'string', + 'u_id' => 'primary', + 'u_created_at' => 'datetime', ]); $this->makeTable(Entity\Tenant::ROLE, [ @@ -161,7 +162,7 @@ private function makeTables(): void private function fillData(): void { $this->getDatabase()->table(Entity\User::ROLE)->insertMultiple( - ['name', 'active'], + ['u_name', 'u_active'], [ ['user-1', true], ['user-2', true], @@ -181,7 +182,7 @@ private function fillData(): void ], ); $this->getDatabase()->table(Entity\LogRecord::ROLE)->insertMultiple( - ['message', 'actor_type', 'actor_id', 'created_at'], + ['l_message', 'l_actor_type', 'l_actor_id', 'l_created_at'], [ ['log-1 for user-1', Entity\User::ROLE, 1, new \DateTimeImmutable()], ['log-2 for user-2', Entity\User::ROLE, 2, new \DateTimeImmutable()], diff --git a/tests/ORM/Functional/Driver/Common/Integration/Issue356/Entity/LogRecord.php b/tests/ORM/Functional/Driver/Common/Integration/Issue356/Entity/LogRecord.php index 572ac112..14a87925 100644 --- a/tests/ORM/Functional/Driver/Common/Integration/Issue356/Entity/LogRecord.php +++ b/tests/ORM/Functional/Driver/Common/Integration/Issue356/Entity/LogRecord.php @@ -8,7 +8,7 @@ class LogRecord { public const ROLE = 'log'; - public ?int $id = null; + public ?int $lid = null; public string $message; public string $actor_type; public int $actor_id; diff --git a/tests/ORM/Functional/Driver/Common/Integration/Issue356/schema.php b/tests/ORM/Functional/Driver/Common/Integration/Issue356/schema.php index b3e911e3..55a297c8 100644 --- a/tests/ORM/Functional/Driver/Common/Integration/Issue356/schema.php +++ b/tests/ORM/Functional/Driver/Common/Integration/Issue356/schema.php @@ -20,14 +20,14 @@ Schema::SOURCE => Source::class, Schema::DATABASE => 'default', Schema::TABLE => 'log', - Schema::PRIMARY_KEY => ['id'], - Schema::FIND_BY_KEYS => ['id'], + Schema::PRIMARY_KEY => ['lid'], + Schema::FIND_BY_KEYS => ['lid'], Schema::COLUMNS => [ - 'id' => 'id', - 'message' => 'message', - 'actor_id' => 'actor_id', - 'actor_type' => 'actor_type', - 'created_at' => 'created_at', + 'lid' => 'l_id', + 'message' => 'l_message', + 'actor_id' => 'l_actor_id', + 'actor_type' => 'l_actor_type', + 'created_at' => 'l_created_at', ], Schema::RELATIONS => [ 'actor' => [ @@ -46,7 +46,7 @@ ], Schema::SCOPE => null, Schema::TYPECAST => [ - 'id' => 'int', + 'lid' => 'int', 'message' => 'string', 'actor_id' => 'int', 'actor_type' => 'string', @@ -64,9 +64,10 @@ Schema::PRIMARY_KEY => ['id'], Schema::FIND_BY_KEYS => ['id'], Schema::COLUMNS => [ - 'id' => 'id', - 'name' => 'name', - 'created_at' => 'created_at', + 'id' => 'u_id', + 'name' => 'u_name', + 'created_at' => 'u_created_at', + 'active' => 'u_active', ], Schema::RELATIONS => [], Schema::SCOPE => ActiveScope::class, @@ -74,6 +75,7 @@ 'id' => 'int', 'name' => 'string', 'created_at' => 'datetime', + 'active' => 'bool', ], Schema::SCHEMA => [], ],