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.5] dont use global scope while touching parent timestamp #21604

Merged
merged 2 commits into from
Oct 10, 2017
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
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function touch()
*/
public function rawUpdate(array $attributes = [])
{
return $this->query->update($attributes);
return $this->query->withoutGlobalScopes()->update($attributes);
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/Database/DatabaseEloquentRelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function testTouchMethodUpdatesRelatedTimestamps()
$builder->shouldReceive('getModel')->andReturn($related = m::mock(\stdClass::class));
$builder->shouldReceive('whereNotNull');
$builder->shouldReceive('where');
$builder->shouldReceive('withoutGlobalScopes')->andReturn($builder);
$relation = new HasOne($builder, $parent, 'foreign_key', 'id');
$related->shouldReceive('getTable')->andReturn('table');
$related->shouldReceive('getUpdatedAtColumn')->andReturn('updated_at');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Illuminate\Tests\Integration\Database\EloquentTouchParentWithGlobalScopeTest;

use Illuminate\Support\Carbon;
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Eloquent\Model;

/**
* @group integration
*/
class EloquentTouchParentWithGlobalScopeTest extends TestCase
{
protected function getEnvironmentSetUp($app)
{
$app['config']->set('app.debug', 'true');

$app['config']->set('database.default', 'testbench');

$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}

public function setUp()
{
parent::setUp();

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

Schema::create('comments', function ($table) {
$table->increments('id');
$table->integer('post_id');
$table->string('title');
$table->timestamps();
});

Carbon::setTestNow(null);
}

/**
* @test
*/
public function basic_create_and_retrieve()
{
$post = Post::create(['title' => str_random(), 'updated_at' => '2016-10-10 10:10:10']);

$this->assertEquals('2016-10-10', $post->fresh()->updated_at->toDateString());

$post->comments()->create(['title' => str_random()]);

$this->assertNotEquals('2016-10-10', $post->fresh()->updated_at->toDateString());
}
}

class Post extends Model
{
public $table = 'posts';
public $timestamps = true;
protected $guarded = ['id'];

public function comments()
{
return $this->hasMany(Comment::class, 'post_id');
}

public static function boot()
{
parent::boot();

static::addGlobalScope('age', function ($builder) {
$builder->join('comments', 'comments.post_id', '=', 'posts.id');
});
}
}

class Comment extends Model
{
public $table = 'comments';
public $timestamps = true;
protected $guarded = ['id'];
protected $touches = ['post'];

public function post()
{
return $this->belongsTo(Post::class, 'post_id');
}
}