Skip to content

Commit

Permalink
Prevent error with BelongsTo and BelongsToMany relations to the same …
Browse files Browse the repository at this point in the history
…model
  • Loading branch information
guidocella committed May 13, 2017
1 parent 0004b25 commit 522585f
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/ModelPopulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/PivotPopulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ protected function getRelatedKeyName()
*/
public function execute(Model $currentParent, array $insertedPKs)
{
if (!isset($insertedPKs[$this->relatedClass])) {
return;
}

$this->updateParentKey($currentParent);

$values = [];
Expand Down
23 changes: 23 additions & 0 deletions tests/Migrations/2026_10_12_000000_create_company_user_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCompanyUserTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('company_user', function (Blueprint $table) {
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->unsignedInteger('company_id');
$table->foreign('company_id')->references('id')->on('companys');
});
}
}
5 changes: 5 additions & 0 deletions tests/Models/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ public function country()
{
return $this->belongsTo(Country::class, 'country_code');
}

public function users()
{
return $this->belongsToMany(User::class);
}
}
5 changes: 5 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public function company()
{
return $this->belongsTo(Company::class);
}

public function companies()
{
return $this->belongsToMany(Company::class);
}
}
10 changes: 10 additions & 0 deletions tests/PivotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 522585f

Please sign in to comment.