Skip to content

Commit b3877c9

Browse files
authored
Allow multiple morphTo statements (#535)
1 parent 6d4014b commit b3877c9

File tree

7 files changed

+74
-20
lines changed

7 files changed

+74
-20
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public function output(Tree $tree, $overwrite = false): array
5656
*/
5757
foreach ($tree->models() as $model) {
5858
$tables['tableNames'][$model->tableName()] = $this->populateStub($stub, $model);
59-
6059
if (!empty($model->pivotTables())) {
6160
foreach ($model->pivotTables() as $pivotSegments) {
6261
$pivotTableName = $this->getPivotTableName($pivotSegments);
@@ -257,9 +256,13 @@ function ($modifier) use ($column) {
257256
$definition .= $column_definition;
258257
}
259258

260-
if ($model->morphTo()) {
261-
$definition .= self::INDENT . sprintf('$table->unsignedBigInteger(\'%s\');', Str::lower($model->morphTo() . '_id')) . PHP_EOL;
262-
$definition .= self::INDENT . sprintf('$table->string(\'%s\');', Str::lower($model->morphTo() . '_type')) . PHP_EOL;
259+
$relationships = $model->relationships();
260+
261+
if (array_key_exists('morphTo', $relationships)) {
262+
foreach ($relationships['morphTo'] as $morphTo) {
263+
$definition .= self::INDENT . sprintf('$table->unsignedBigInteger(\'%s\');', Str::lower($morphTo . '_id')) . PHP_EOL;
264+
$definition .= self::INDENT . sprintf('$table->string(\'%s\');', Str::lower($morphTo . '_type')) . PHP_EOL;
265+
}
263266
}
264267

265268
foreach ($model->indexes() as $index) {

src/Lexers/ModelLexer.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,6 @@ private function buildModel(string $name, array $columns)
158158
foreach ($columns['relationships'] as $type => $relationships) {
159159
foreach (explode(',', $relationships) as $reference) {
160160
$model->addRelationship(self::$relationships[strtolower($type)], trim($reference));
161-
162-
if ($type === 'morphTo') {
163-
$model->setMorphTo(trim($reference));
164-
}
165161
}
166162
}
167163
}

src/Models/Model.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class Model implements BlueprintModel
1212
private $primaryKey = 'id';
1313
private $timestamps = 'timestamps';
1414
private $softDeletes = false;
15-
private $morphTo;
1615
private $columns = [];
1716
private $relationships = [];
1817
private $pivotTables = [];
@@ -189,14 +188,4 @@ public function polymorphicManyToManyTables()
189188
{
190189
return $this->polymorphicManyToManyTables;
191190
}
192-
193-
public function setMorphTo(string $reference)
194-
{
195-
$this->morphTo = $reference;
196-
}
197-
198-
public function morphTo(): ?string
199-
{
200-
return $this->morphTo;
201-
}
202191
}

tests/Feature/Generators/MigrationGeneratorTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,31 @@ public function output_works_with_polymorphic_relationships()
549549
$this->assertEquals(['created' => [$post_migration, $user_migration, $image_migration]], $this->subject->output($tree));
550550
}
551551

552+
/**
553+
* @test
554+
*/
555+
public function output_works_with_multiple_morphto_statements_in_polymorphic_relationship()
556+
{
557+
$this->filesystem->expects('stub')
558+
->with('migration.stub')
559+
->andReturn($this->stub('migration.stub'));
560+
561+
$now = Carbon::now();
562+
Carbon::setTestNow($now);
563+
564+
$image_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_images_table.php');
565+
566+
$this->filesystem->expects('exists')->andReturn(false);
567+
568+
$this->filesystem->expects('put')
569+
->with($image_migration, $this->fixture('migrations/polymorphic_relationships_images_table_multiple_morphto.php'));
570+
571+
$tokens = $this->blueprint->parse($this->fixture('drafts/polymorphic-relationships-multiple-morphto.yaml'));
572+
$tree = $this->blueprint->analyze($tokens);
573+
574+
$this->assertEquals(['created' => [$image_migration]], $this->subject->output($tree));
575+
}
576+
552577
/**
553578
* @test
554579
*/

tests/Feature/Lexers/ModelLexerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ public function it_enables_morphable_and_set_its_reference()
632632

633633
$model = $actual['models']['Model'];
634634
$this->assertEquals('Model', $model->name());
635-
$this->assertEquals('Morphable', $model->morphTo());
635+
$this->assertArrayHasKey('morphTo', $model->relationships());
636636
$this->assertTrue($model->usesTimestamps());
637637

638638
$columns = $model->columns();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
models:
2+
Image:
3+
url: string:400
4+
relationships:
5+
morphTo: Imageable, Tagable
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateImagesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('images', function (Blueprint $table) {
17+
$table->id();
18+
$table->string('url', 400);
19+
$table->unsignedBigInteger('imageable_id');
20+
$table->string('imageable_type');
21+
$table->unsignedBigInteger('tagable_id');
22+
$table->string('tagable_type');
23+
$table->timestamps();
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('images');
35+
}
36+
}

0 commit comments

Comments
 (0)