From 763dce2975d0e3b9ff479f543264a7b621e50a15 Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Thu, 30 Jul 2020 12:13:57 +0300 Subject: [PATCH] - [bugfix] fixes typo in eager loading --- CHANGELOG.md | 4 + src/Select/JoinableLoader.php | 2 +- tests/ORM/Driver/MySQL/Eager2Test.php | 17 ++ .../MySQL/ManyToManyPromiseEagerLoadTest.php | 17 ++ tests/ORM/Driver/Postgres/Eager2Test.php | 17 ++ .../ManyToManyPromiseEagerLoadTest.php | 17 ++ tests/ORM/Driver/SQLServer/Eager2Test.php | 17 ++ .../ManyToManyPromiseEagerLoadTest.php | 17 ++ tests/ORM/Driver/SQLite/Eager2Test.php | 17 ++ .../SQLite/ManyToManyPromiseEagerLoadTest.php | 17 ++ tests/ORM/Eager2Test.php | 164 ++++++++++++++++++ 11 files changed, 305 insertions(+), 1 deletion(-) create mode 100644 tests/ORM/Driver/MySQL/Eager2Test.php create mode 100644 tests/ORM/Driver/MySQL/ManyToManyPromiseEagerLoadTest.php create mode 100644 tests/ORM/Driver/Postgres/Eager2Test.php create mode 100644 tests/ORM/Driver/Postgres/ManyToManyPromiseEagerLoadTest.php create mode 100644 tests/ORM/Driver/SQLServer/Eager2Test.php create mode 100644 tests/ORM/Driver/SQLServer/ManyToManyPromiseEagerLoadTest.php create mode 100644 tests/ORM/Driver/SQLite/Eager2Test.php create mode 100644 tests/ORM/Driver/SQLite/ManyToManyPromiseEagerLoadTest.php create mode 100644 tests/ORM/Eager2Test.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b45666d3..e0fdabd78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +v1.2.12 (30.07.2020) +-------------------- +- [bugfix] fixes typo in eager loading + v1.2.11 (22.07.2020) -------------------- - [bugfix] incorrect update sequence for nullable numeric values diff --git a/src/Select/JoinableLoader.php b/src/Select/JoinableLoader.php index b9a5af3e8..c29633d54 100644 --- a/src/Select/JoinableLoader.php +++ b/src/Select/JoinableLoader.php @@ -132,7 +132,7 @@ public function withContext(LoaderInterface $parent, array $options = []): Loade $loader->setConstrain($this->getSource()->getConstrain()); } - if ($this->isLoaded()) { + if ($loader->isLoaded()) { foreach ($loader->getEagerRelations() as $relation) { $loader->loadRelation($relation, [], false, true); } diff --git a/tests/ORM/Driver/MySQL/Eager2Test.php b/tests/ORM/Driver/MySQL/Eager2Test.php new file mode 100644 index 000000000..9aa71c9a9 --- /dev/null +++ b/tests/ORM/Driver/MySQL/Eager2Test.php @@ -0,0 +1,17 @@ +makeTable('user', [ + 'id' => 'primary', + 'email' => 'string', + 'balance' => 'float' + ]); + + $this->makeTable('profile', [ + 'id' => 'primary', + 'user_id' => 'integer,nullable', + 'image' => 'string' + ]); + + $this->makeTable('nested', [ + 'id' => 'primary', + 'profile_id' => 'integer', + 'label' => 'string' + ]); + + $this->makeFK('profile', 'user_id', 'user', 'id'); + $this->makeFK('nested', 'profile_id', 'profile', 'id'); + + $this->getDatabase()->table('user')->insertMultiple( + ['email', 'balance'], + [ + ['hello@world.com', 100], + ['another@world.com', 200], + ] + ); + + $this->getDatabase()->table('profile')->insertMultiple( + ['user_id', 'image'], + [ + [1, 'image.png'], + ] + ); + + + $this->getDatabase()->table('nested')->insertMultiple( + ['profile_id', 'label'], + [ + [1, 'nested-label'], + ] + ); + + $this->orm = $this->withSchema(new Schema([ + User::class => [ + Schema::ROLE => 'user', + Schema::MAPPER => Mapper::class, + Schema::DATABASE => 'default', + Schema::TABLE => 'user', + Schema::PRIMARY_KEY => 'id', + Schema::COLUMNS => ['id', 'email', 'balance'], + Schema::SCHEMA => [], + Schema::RELATIONS => [ + 'profile' => [ + Relation::TYPE => Relation::HAS_ONE, + Relation::TARGET => Profile::class, + Relation::LOAD => Relation::LOAD_PROMISE, + Relation::SCHEMA => [ + Relation::CASCADE => true, + Relation::INNER_KEY => 'id', + Relation::OUTER_KEY => 'user_id', + ], + ] + ] + ], + Profile::class => [ + Schema::ROLE => 'profile', + Schema::MAPPER => Mapper::class, + Schema::DATABASE => 'default', + Schema::TABLE => 'profile', + Schema::PRIMARY_KEY => 'id', + Schema::COLUMNS => ['id', 'user_id', 'image'], + Schema::SCHEMA => [], + Schema::RELATIONS => [ + 'nested' => [ + Relation::TYPE => Relation::HAS_ONE, + Relation::TARGET => Nested::class, + Relation::LOAD => Relation::LOAD_EAGER, + Relation::SCHEMA => [ + Relation::CASCADE => true, + Relation::INNER_KEY => 'id', + Relation::OUTER_KEY => 'profile_id', + ], + ] + ] + ], + Nested::class => [ + Schema::ROLE => 'nested', + Schema::MAPPER => Mapper::class, + Schema::DATABASE => 'default', + Schema::TABLE => 'nested', + Schema::PRIMARY_KEY => 'id', + Schema::COLUMNS => ['id', 'profile_id', 'label'], + Schema::SCHEMA => [], + Schema::RELATIONS => [] + ] + ])); + } + + public function testFetchEager(): void + { + $selector = new Select($this->orm, User::class); + + $this->assertEquals([ + [ + 'id' => 1, + 'email' => 'hello@world.com', + 'balance' => 100.0, + ], + [ + 'id' => 2, + 'email' => 'another@world.com', + 'balance' => 200.0, + ], + ], $selector->fetchData()); + } + + public function testFetchEager2(): void + { + $selector = new Select($this->orm, User::class); + $selector->where('profile.id', '>', 0); + + $this->assertCount(3, $selector->buildQuery()->getColumns()); + + $this->assertEquals([ + [ + 'id' => 1, + 'email' => 'hello@world.com', + 'balance' => 100.0, + ], + ], $selector->fetchData()); + } +}