From 728f11f81520b036f46bb62abbf6fc2b1a14cf31 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Tue, 10 Oct 2017 17:27:39 +0200 Subject: [PATCH 1/2] dont use global scope while touching parent timestamp --- .../Database/Eloquent/Relations/Relation.php | 2 +- .../Database/DatabaseEloquentRelationTest.php | 1 + ...EloquentTouchParentWithGlobalScopeTest.php | 98 +++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 tests/Integration/Database/EloquentTouchParentWithGlobalScopeTest.php diff --git a/src/Illuminate/Database/Eloquent/Relations/Relation.php b/src/Illuminate/Database/Eloquent/Relations/Relation.php index a35bea035028..d5de492325f2 100755 --- a/src/Illuminate/Database/Eloquent/Relations/Relation.php +++ b/src/Illuminate/Database/Eloquent/Relations/Relation.php @@ -174,7 +174,7 @@ public function touch() */ public function rawUpdate(array $attributes = []) { - return $this->query->update($attributes); + return $this->query->withoutGlobalScopes()->update($attributes); } /** diff --git a/tests/Database/DatabaseEloquentRelationTest.php b/tests/Database/DatabaseEloquentRelationTest.php index e69bbdfabaa8..52e83d46ca31 100755 --- a/tests/Database/DatabaseEloquentRelationTest.php +++ b/tests/Database/DatabaseEloquentRelationTest.php @@ -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'); diff --git a/tests/Integration/Database/EloquentTouchParentWithGlobalScopeTest.php b/tests/Integration/Database/EloquentTouchParentWithGlobalScopeTest.php new file mode 100644 index 000000000000..d0c756ef4c2f --- /dev/null +++ b/tests/Integration/Database/EloquentTouchParentWithGlobalScopeTest.php @@ -0,0 +1,98 @@ +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'); + } + + 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'); + } +} From 65e62e175cc79058351f04090f1c835258afd07b Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Tue, 10 Oct 2017 17:29:03 +0200 Subject: [PATCH 2/2] fix style --- .../Database/EloquentTouchParentWithGlobalScopeTest.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/Integration/Database/EloquentTouchParentWithGlobalScopeTest.php b/tests/Integration/Database/EloquentTouchParentWithGlobalScopeTest.php index d0c756ef4c2f..b571d5836e26 100644 --- a/tests/Integration/Database/EloquentTouchParentWithGlobalScopeTest.php +++ b/tests/Integration/Database/EloquentTouchParentWithGlobalScopeTest.php @@ -4,11 +4,8 @@ use Illuminate\Support\Carbon; use Orchestra\Testbench\TestCase; -use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\Collection; -use Illuminate\Database\Eloquent\Relations\Pivot; /** * @group integration @@ -74,7 +71,7 @@ public function comments() return $this->hasMany(Comment::class, 'post_id'); } - static function boot() + public static function boot() { parent::boot();