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] Disable model relationship touching #23469

Merged
merged 18 commits into from
Jun 20, 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
52 changes: 52 additions & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab
*/
protected static $globalScopes = [];

/**
* The list of models classes that should not be affected with touch.
*
* @var array
*/
protected static $ignoreOnTouch = [];

/**
* The name of the "created at" column.
*
Expand Down Expand Up @@ -197,6 +204,51 @@ protected static function bootTraits()
}
}

/**
* Disables relationship model touching for the current class during given callback scope.
*
* @param callable $callback
*/
public static function withoutTouching(callable $callback)
{
static::withoutTouchingOn([static::class], $callback);
}

/**
* Disables relationship model touching for the given model classes during given callback scope.
*
* @param array $models
* @param callable $callback
*/
public static function withoutTouchingOn(array $models, callable $callback)
{
static::$ignoreOnTouch = array_values(array_merge(static::$ignoreOnTouch, $models));

try {
call_user_func($callback);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is call_user_func($callback) better than $callback() ?

} finally {
static::$ignoreOnTouch = array_values(array_diff(static::$ignoreOnTouch, $models));
}
}

/**
* @param string $class The class name to check. Defaults to current class.
*
* @return bool
*/
public static function isIgnoringTouch($class = null)
{
$class = $class ?: static::class;

foreach (static::$ignoreOnTouch as $ignoredClass) {
if ($class === $ignoredClass || is_subclass_of($class, $ignoredClass)) {
return true;
}
}

return false;
}

/**
* Clear the list of booted models so they will be re-booted.
*
Expand Down
8 changes: 6 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,13 @@ public function get($columns = ['*'])
*/
public function touch()
{
$column = $this->getRelated()->getUpdatedAtColumn();
$model = $this->getRelated();

$this->rawUpdate([$column => $this->getRelated()->freshTimestampString()]);
if (! $model::isIgnoringTouch()) {
$column = $model->getUpdatedAtColumn();

$this->rawUpdate([$column => $model->freshTimestampString()]);
}
}

/**
Expand Down
Loading