Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Fix nullable MorphTo and $touches #25438

Merged
merged 1 commit into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
{
}