From 8b727b4cf8cfc3551609536086e940b5f964be23 Mon Sep 17 00:00:00 2001 From: Maxime Fabre Date: Sun, 4 Sep 2016 10:01:22 +0200 Subject: [PATCH] Add ability to disable touching of parent when toggling relation --- .../Eloquent/Relations/BelongsToMany.php | 5 +++-- .../DatabaseEloquentBelongsToManyTest.php | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index 1811e0309628..f24b0556d354 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -801,9 +801,10 @@ public function createMany(array $records, array $joinings = []) * Each existing model is detached, and non existing ones are attached. * * @param mixed $ids + * @param bool $touch * @return array */ - public function toggle($ids) + public function toggle($ids, $touch = true) { $changes = [ 'attached' => [], 'detached' => [], @@ -849,7 +850,7 @@ public function toggle($ids) $changes['attached'] = array_keys($attach); } - if (count($changes['attached']) || count($changes['detached'])) { + if ($touch && (count($changes['attached']) || count($changes['detached']))) { $this->touchIfTouching(); } diff --git a/tests/Database/DatabaseEloquentBelongsToManyTest.php b/tests/Database/DatabaseEloquentBelongsToManyTest.php index 491852b3ef45..0ec041b10da9 100755 --- a/tests/Database/DatabaseEloquentBelongsToManyTest.php +++ b/tests/Database/DatabaseEloquentBelongsToManyTest.php @@ -582,6 +582,23 @@ public function toggleMethodListProvider() ]; } + public function testToggleMethodCanLeaveRelatedTimestampsIntact() + { + $relation = $this->getMockBuilder('Illuminate\Database\Eloquent\Relations\BelongsToMany')->setMethods(['attach', 'detach'])->setConstructorArgs($this->getRelationArguments())->getMock(); + $query = m::mock('stdClass'); + $query->shouldReceive('from')->once()->with('user_role')->andReturn($query); + $query->shouldReceive('where')->once()->with('user_id', 1)->andReturn($query); + $relation->getQuery()->shouldReceive('getQuery')->andReturn($mockQueryBuilder = m::mock('StdClass')); + $mockQueryBuilder->shouldReceive('newQuery')->once()->andReturn($query); + $query->shouldReceive('pluck')->once()->with('role_id')->andReturn(new BaseCollection([1, 2, 3])); + $relation->expects($this->once())->method('attach')->with($this->equalTo(['x' => []]), $this->equalTo([]), $this->equalTo(false)); + $relation->expects($this->once())->method('detach')->with($this->equalTo([2, 3])); + $relation->getRelated()->shouldNotReceive('touches'); + $relation->getParent()->shouldNotReceive('touches'); + + $this->assertEquals(['attached' => ['x'], 'detached' => [2, 3]], $relation->toggle([2, 3, 'x'], false)); + } + public function testToggleMethodTogglesIntermediateTableWithGivenArrayAndAttributes() { $relation = $this->getMockBuilder('Illuminate\Database\Eloquent\Relations\BelongsToMany')->setMethods(['attach', 'detach', 'touchIfTouching', 'updateExistingPivot'])->setConstructorArgs($this->getRelationArguments())->getMock();