Skip to content

Commit

Permalink
Fix nullable MorphTo and $touches
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Sep 4, 2018
1 parent 83bd575 commit a8c5056
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,21 @@ public function dissociate()
return $this->parent->setRelation($this->relation, null);
}

/**
* Touch all of the related models for the relationship.
*
* @return void
*/
public function touch()
{
// If there is no related model, we'll just return to prevent an invalid query.
if (is_null($this->ownerKey)) {
return;
}

parent::touch();
}

/**
* Remove all or passed registered global scopes.
*
Expand Down
67 changes: 67 additions & 0 deletions tests/Integration/Database/EloquentMorphToTouchesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Illuminate\Tests\Integration\Database\EloquentMorphToTouchesTest;

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Tests\Integration\Database\DatabaseTestCase;

/**
* @group integration
*/
class EloquentMorphToTouchesTest extends DatabaseTestCase
{
public function setUp()
{
parent::setUp();

Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});

Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->nullableMorphs('commentable');
});

Post::create();
}

public function test_not_null()
{
$comment = (new Comment)->commentable()->associate(Post::first());

\DB::enableQueryLog();

$comment->save();

$this->assertCount(2, \DB::getQueryLog());
}

public function test_null()
{
\DB::enableQueryLog();

Comment::create();

$this->assertCount(1, \DB::getQueryLog());
}
}

class Comment extends Model
{
public $timestamps = false;

protected $touches = ['commentable'];

public function commentable()
{
return $this->morphTo();
}
}

class Post extends Model
{
}

0 comments on commit a8c5056

Please sign in to comment.