Skip to content

Commit 8a317dd

Browse files
committed
Merge branch 'disable-model-touching' of https://github.com/tonysm/framework into tonysm-disable-model-touching
2 parents a6d328c + 1d85125 commit 8a317dd

File tree

5 files changed

+602
-3
lines changed

5 files changed

+602
-3
lines changed

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab
124124
*/
125125
protected static $globalScopes = [];
126126

127+
/**
128+
* The list of models classes that should not be affected with touch.
129+
*
130+
* @var array
131+
*/
132+
protected static $ignoreOnTouch = [];
133+
127134
/**
128135
* The name of the "created at" column.
129136
*
@@ -203,6 +210,51 @@ protected static function bootTraits()
203210
}
204211
}
205212

213+
/**
214+
* Disables relationship model touching for the current class during given callback scope.
215+
*
216+
* @param callable $callback
217+
*/
218+
public static function withoutTouching(callable $callback)
219+
{
220+
static::withoutTouchingOn([static::class], $callback);
221+
}
222+
223+
/**
224+
* Disables relationship model touching for the given model classes during given callback scope.
225+
*
226+
* @param array $models
227+
* @param callable $callback
228+
*/
229+
public static function withoutTouchingOn(array $models, callable $callback)
230+
{
231+
static::$ignoreOnTouch = array_values(array_merge(static::$ignoreOnTouch, $models));
232+
233+
try {
234+
call_user_func($callback);
235+
} finally {
236+
static::$ignoreOnTouch = array_values(array_diff(static::$ignoreOnTouch, $models));
237+
}
238+
}
239+
240+
/**
241+
* @param string $class The class name to check. Defaults to current class.
242+
*
243+
* @return bool
244+
*/
245+
public static function isIgnoringTouch($class = null)
246+
{
247+
$class = $class ?: static::class;
248+
249+
foreach (static::$ignoreOnTouch as $ignoredClass) {
250+
if ($class === $ignoredClass || is_subclass_of($class, $ignoredClass)) {
251+
return true;
252+
}
253+
}
254+
255+
return false;
256+
}
257+
206258
/**
207259
* Clear the list of booted models so they will be re-booted.
208260
*

src/Illuminate/Database/Eloquent/Relations/Relation.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,13 @@ public function get($columns = ['*'])
161161
*/
162162
public function touch()
163163
{
164-
$column = $this->getRelated()->getUpdatedAtColumn();
164+
$model = $this->getRelated();
165165

166-
$this->rawUpdate([$column => $this->getRelated()->freshTimestampString()]);
166+
if (! $model::isIgnoringTouch()) {
167+
$column = $model->getUpdatedAtColumn();
168+
169+
$this->rawUpdate([$column => $model->freshTimestampString()]);
170+
}
167171
}
168172

169173
/**

0 commit comments

Comments
 (0)