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

[9.x] Adds handlers for silently discarded and missing attribute violations #44664

Merged
merged 3 commits into from
Oct 20, 2022
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
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ protected function throwMissingAttributeExceptionIfApplicable($key)
if ($this->exists &&
! $this->wasRecentlyCreated &&
static::preventsAccessingMissingAttributes()) {
if (isset(static::$missingAttributeViolationCallback)) {
return call_user_func(static::$missingAttributeViolationCallback, $this, $key);
}

throw new MissingAttributeException($this, $key);
}

Expand Down
98 changes: 72 additions & 26 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,27 @@ abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToSt
*/
protected static $modelsShouldPreventSilentlyDiscardingAttributes = false;

/**
* The callback that is responsible for handling discarded attribute violations.
*
* @var callable|null
*/
protected static $discardedAttributeViolationCallback;

/**
* Indicates if an exception should be thrown when trying to access a missing attribute on a retrieved model.
*
* @var bool
*/
protected static $modelsShouldPreventAccessingMissingAttributes = false;

/**
* The callback that is responsible for handling missing attribute violations.
*
* @var callable|null
*/
protected static $missingAttributeViolationCallback;

/**
* Indicates if broadcasting is currently enabled.
*
Expand Down Expand Up @@ -430,6 +444,17 @@ public static function preventSilentlyDiscardingAttributes($value = true)
static::$modelsShouldPreventSilentlyDiscardingAttributes = $value;
}

/**
* Register a callback that is responsible for handling discarded attribute violations.
*
* @param callable|null $callback
* @return void
*/
public static function handleDiscardedAttributeViolationUsing(?callable $callback)
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey
What scenario users might be required to call handleDiscardedAttributeViolationUsing(null); ?
Same for handleMissingAttributeViolationUsing(null) case.

Copy link
Contributor

Choose a reason for hiding this comment

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

My first thought would be during tests, to either enable or disable it dynamically. Not sure if that was the intended use-case.

{
static::$discardedAttributeViolationCallback = $callback;
}

/**
* Prevent accessing missing attributes on retrieved models.
*
Expand All @@ -441,6 +466,17 @@ public static function preventAccessingMissingAttributes($value = true)
static::$modelsShouldPreventAccessingMissingAttributes = $value;
}

/**
* Register a callback that is responsible for handling lazy loading violations.
*
* @param callable|null $callback
* @return void
*/
public static function handleMissingAttributeViolationUsing(?callable $callback)
{
static::$missingAttributeViolationCallback = $callback;
}

/**
* Execute a callback without broadcasting any model events for all model types.
*
Expand Down Expand Up @@ -481,20 +517,30 @@ public function fill(array $attributes)
if ($this->isFillable($key)) {
$this->setAttribute($key, $value);
} elseif ($totallyGuarded || static::preventsSilentlyDiscardingAttributes()) {
throw new MassAssignmentException(sprintf(
'Add [%s] to fillable property to allow mass assignment on [%s].',
$key, get_class($this)
));
if (isset(static::$discardedAttributeViolationCallback)) {
call_user_func(static::$discardedAttributeViolationCallback, $this, [$key]);
} else {
throw new MassAssignmentException(sprintf(
'Add [%s] to fillable property to allow mass assignment on [%s].',
$key, get_class($this)
));
}
}
}

if (count($attributes) !== count($fillable) &&
static::preventsSilentlyDiscardingAttributes()) {
throw new MassAssignmentException(sprintf(
'Add fillable property [%s] to allow mass assignment on [%s].',
implode(', ', array_diff(array_keys($attributes), array_keys($fillable))),
get_class($this)
));
$keys = array_diff(array_keys($attributes), array_keys($fillable));

if (isset(static::$discardedAttributeViolationCallback)) {
call_user_func(static::$discardedAttributeViolationCallback, $this, $keys);
} else {
throw new MassAssignmentException(sprintf(
'Add fillable property [%s] to allow mass assignment on [%s].',
implode(', ', $keys),
get_class($this)
));
}
}

return $this;
Expand Down Expand Up @@ -1025,7 +1071,7 @@ public function push()
// us to recurse into all of these nested relations for the model instance.
foreach ($this->relations as $models) {
$models = $models instanceof Collection
? $models->all() : [$models];
? $models->all() : [$models];

foreach (array_filter($models) as $model) {
if (! $model->push()) {
Expand Down Expand Up @@ -1072,7 +1118,7 @@ public function save(array $options = [])
// clause to only update this model. Otherwise, we'll just insert them.
if ($this->exists) {
$saved = $this->isDirty() ?
$this->performUpdate($query) : true;
$this->performUpdate($query) : true;
}

// If the model is brand new, we'll insert it into our database and set the
Expand Down Expand Up @@ -1474,8 +1520,8 @@ public function registerGlobalScopes($builder)
public function newQueryWithoutScopes()
{
return $this->newModelQuery()
->with($this->with)
->withCount($this->withCount);
->with($this->with)
->withCount($this->withCount);
}

/**
Expand All @@ -1498,8 +1544,8 @@ public function newQueryWithoutScope($scope)
public function newQueryForRestoration($ids)
{
return is_array($ids)
? $this->newQueryWithoutScopes()->whereIn($this->getQualifiedKeyName(), $ids)
: $this->newQueryWithoutScopes()->whereKey($ids);
? $this->newQueryWithoutScopes()->whereIn($this->getQualifiedKeyName(), $ids)
: $this->newQueryWithoutScopes()->whereKey($ids);
}

/**
Expand Down Expand Up @@ -1547,7 +1593,7 @@ public function newCollection(array $models = [])
public function newPivot(self $parent, array $attributes, $table, $exists, $using = null)
{
return $using ? $using::fromRawAttributes($parent, $attributes, $table, $exists)
: Pivot::fromAttributes($parent, $attributes, $table, $exists);
: Pivot::fromAttributes($parent, $attributes, $table, $exists);
}

/**
Expand Down Expand Up @@ -1625,9 +1671,9 @@ public function fresh($with = [])
}

return $this->setKeysForSelectQuery($this->newQueryWithoutScopes())
->useWritePdo()
->with(is_string($with) ? func_get_args() : $with)
->first();
->useWritePdo()
->with(is_string($with) ? func_get_args() : $with)
->first();
}

/**
Expand Down Expand Up @@ -1705,9 +1751,9 @@ public function replicateQuietly(array $except = null)
public function is($model)
{
return ! is_null($model) &&
$this->getKey() === $model->getKey() &&
$this->getTable() === $model->getTable() &&
$this->getConnectionName() === $model->getConnectionName();
$this->getKey() === $model->getKey() &&
$this->getTable() === $model->getTable() &&
$this->getConnectionName() === $model->getConnectionName();
}

/**
Expand Down Expand Up @@ -2050,8 +2096,8 @@ protected function resolveChildRouteBindingQuery($childType, $value, $field)
}

return $relationship instanceof Model
? $relationship->resolveRouteBindingQuery($relationship, $value, $field)
: $relationship->getRelated()->resolveRouteBindingQuery($relationship, $value, $field);
? $relationship->resolveRouteBindingQuery($relationship, $value, $field)
: $relationship->getRelated()->resolveRouteBindingQuery($relationship, $value, $field);
}

/**
Expand Down Expand Up @@ -2295,8 +2341,8 @@ public static function __callStatic($method, $parameters)
public function __toString()
{
return $this->escapeWhenCastingToString
? e($this->toJson())
: $this->toJson();
? e($this->toJson())
: $this->toJson();
}

/**
Expand Down
53 changes: 53 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,32 @@ public function testGuarded()
Model::preventSilentlyDiscardingAttributes(false);
}

public function testUsesOverriddenHandlerWhenDiscardingAttributes()
{
EloquentModelStub::setConnectionResolver($resolver = m::mock(Resolver::class));
$resolver->shouldReceive('connection')->andReturn($connection = m::mock(stdClass::class));
$connection->shouldReceive('getSchemaBuilder->getColumnListing')->andReturn(['name', 'age', 'foo']);

Model::preventSilentlyDiscardingAttributes();

$callbackModel = null;
$callbackKeys = null;
Model::handleDiscardedAttributeViolationUsing(function ($model, $keys) use (&$callbackModel, &$callbackKeys) {
$callbackModel = $model;
$callbackKeys = $keys;
});

$model = new EloquentModelStub;
$model->guard(['name', 'age']);
$model->fill(['Foo' => 'bar']);

$this->assertInstanceOf(EloquentModelStub::class, $callbackModel);
$this->assertEquals(['Foo'], $callbackKeys);

Model::preventSilentlyDiscardingAttributes(false);
Model::handleDiscardedAttributeViolationUsing(null);
}

public function testFillableOverridesGuarded()
{
Model::preventSilentlyDiscardingAttributes(false);
Expand Down Expand Up @@ -2338,6 +2364,33 @@ public function testThrowsWhenAccessingMissingAttributes()
}
}

public function testUsesOverriddenHandlerWhenAccessingMissingAttributes()
{
$originalMode = Model::preventsAccessingMissingAttributes();
Model::preventAccessingMissingAttributes();

$callbackModel = null;
$callbackKey = null;

Model::handleMissingAttributeViolationUsing(function ($model, $key) use (&$callbackModel, &$callbackKey) {
$callbackModel = $model;
$callbackKey = $key;
});

$model = new EloquentModelStub(['id' => 1]);
$model->exists = true;

$this->assertEquals(1, $model->id);

$model->this_attribute_does_not_exist;

$this->assertInstanceOf(EloquentModelStub::class, $callbackModel);
$this->assertEquals('this_attribute_does_not_exist', $callbackKey);

Model::preventAccessingMissingAttributes($originalMode);
Model::handleMissingAttributeViolationUsing(null);
}

public function testDoesntThrowWhenAccessingMissingAttributesOnModelThatIsNotSaved()
{
$originalMode = Model::preventsAccessingMissingAttributes();
Expand Down