From 522585fb20a52420fa04a5d35994dca8b2d72d6a Mon Sep 17 00:00:00 2001 From: guidocella Date: Sat, 13 May 2017 17:11:13 +0200 Subject: [PATCH] Prevent error with BelongsTo and BelongsToMany relations to the same model --- CHANGELOG.md | 4 ++++ src/ModelPopulator.php | 4 ++++ src/PivotPopulator.php | 4 ++++ ...10_12_000000_create_company_user_table.php | 23 +++++++++++++++++++ ... 2027_10_12_000000_create_clubs_table.php} | 0 ...8_10_12_000000_create_club_user_table.php} | 0 tests/Models/Company.php | 5 ++++ tests/Models/User.php | 5 ++++ tests/PivotTest.php | 10 ++++++++ 9 files changed, 55 insertions(+) create mode 100644 tests/Migrations/2026_10_12_000000_create_company_user_table.php rename tests/Migrations/{2026_10_12_000000_create_clubs_table.php => 2027_10_12_000000_create_clubs_table.php} (100%) rename tests/Migrations/{2027_10_12_000000_create_club_user_table.php => 2028_10_12_000000_create_club_user_table.php} (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16f986d..75c8cfa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## v2.0.2 (2017-05-13) + +- Prevented error with BelongsTo and BelongsToMany relations to the same model + ## v2.0.1 (2017-04-18) - Prevented previously added models from being recreated diff --git a/src/ModelPopulator.php b/src/ModelPopulator.php index 130495d..e9217e8 100644 --- a/src/ModelPopulator.php +++ b/src/ModelPopulator.php @@ -516,6 +516,10 @@ public function getInsertRecords(array $insertedPKs) $foreignKeys = []; foreach ($this->pivotPopulators as $relatedClass => $pivotPopulator) { + if (!isset($insertedPKs[$relatedClass])) { + continue; + } + list($table, $pivotRecordsOfOneRelation, $foreignKey) = $pivotPopulator->getInsertRecords( $this->model, $insertedPKs diff --git a/src/PivotPopulator.php b/src/PivotPopulator.php index c9b5971..30d185b 100644 --- a/src/PivotPopulator.php +++ b/src/PivotPopulator.php @@ -177,6 +177,10 @@ protected function getRelatedKeyName() */ public function execute(Model $currentParent, array $insertedPKs) { + if (!isset($insertedPKs[$this->relatedClass])) { + return; + } + $this->updateParentKey($currentParent); $values = []; diff --git a/tests/Migrations/2026_10_12_000000_create_company_user_table.php b/tests/Migrations/2026_10_12_000000_create_company_user_table.php new file mode 100644 index 0000000..dd0e963 --- /dev/null +++ b/tests/Migrations/2026_10_12_000000_create_company_user_table.php @@ -0,0 +1,23 @@ +unsignedInteger('user_id'); + $table->foreign('user_id')->references('id')->on('users'); + $table->unsignedInteger('company_id'); + $table->foreign('company_id')->references('id')->on('companys'); + }); + } +} diff --git a/tests/Migrations/2026_10_12_000000_create_clubs_table.php b/tests/Migrations/2027_10_12_000000_create_clubs_table.php similarity index 100% rename from tests/Migrations/2026_10_12_000000_create_clubs_table.php rename to tests/Migrations/2027_10_12_000000_create_clubs_table.php diff --git a/tests/Migrations/2027_10_12_000000_create_club_user_table.php b/tests/Migrations/2028_10_12_000000_create_club_user_table.php similarity index 100% rename from tests/Migrations/2027_10_12_000000_create_club_user_table.php rename to tests/Migrations/2028_10_12_000000_create_club_user_table.php diff --git a/tests/Models/Company.php b/tests/Models/Company.php index b233618..9388dff 100644 --- a/tests/Models/Company.php +++ b/tests/Models/Company.php @@ -10,4 +10,9 @@ public function country() { return $this->belongsTo(Country::class, 'country_code'); } + + public function users() + { + return $this->belongsToMany(User::class); + } } diff --git a/tests/Models/User.php b/tests/Models/User.php index 6e2ac8f..79816ec 100644 --- a/tests/Models/User.php +++ b/tests/Models/User.php @@ -22,4 +22,9 @@ public function company() { return $this->belongsTo(Company::class); } + + public function companies() + { + return $this->belongsToMany(Company::class); + } } diff --git a/tests/PivotTest.php b/tests/PivotTest.php index 726d5eb..05360cc 100644 --- a/tests/PivotTest.php +++ b/tests/PivotTest.php @@ -103,6 +103,16 @@ public function testCustomPivotAttributes() $this->assertSame('2000-01-11', $user->roles[0]->pivot->expires_at); } + public function testBelongsToAndBelongsToManyOfSameModel_withExecute() + { + $this->populator->create(User::class); + } + + public function testBelongsToAndBelongsToManyOfSameModel_withSeed() + { + $this->populator->add(User::class)->seed(); + } + public function testSeedRunsOneInsertPer500PivotRows() { $this->app['db']->enableQueryLog();