Skip to content

Commit

Permalink
BUGFIX: If an array $attributes exists in the model, its data is writ…
Browse files Browse the repository at this point in the history
…ten to the table when the repository is updated (#751)

Details in test:
https://github.com/akmktz/l5repository_test_bug
See readme
  • Loading branch information
akmktz authored Mar 6, 2021
1 parent 4a7a642 commit ad55bf2
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Prettus/Repository/Eloquent/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -670,10 +670,13 @@ public function update(array $attributes, $id)
// we should pass data that has been casts by the model
// to make sure data type are same because validator may need to use
// this data to compare with data that fetch from database.
$model = $this->model->newInstance();
$model->setRawAttributes([]);
$model->setAppends([]);
if ($this->versionCompare($this->app->version(), "5.2.*", ">")) {
$attributes = $this->model->newInstance()->forceFill($attributes)->makeVisible($this->model->getHidden())->toArray();
$attributes = $model->forceFill($attributes)->makeVisible($this->model->getHidden())->toArray();
} else {
$model = $this->model->newInstance()->forceFill($attributes);
$model->forceFill($attributes);
$model->makeVisible($this->model->getHidden());
$attributes = $model->toArray();
}
Expand Down

1 comment on commit ad55bf2

@ronfuller
Copy link

Choose a reason for hiding this comment

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

It turns out that this problem exists on the create() method as well.
Since the model is filled with the attributes on the create and then the toArray() is called, any appends attributes will be returned.
Attempting to save the model will generate an error from the database because the appends attributes don't have columns in the model table
public function create(array $attributes)
{
if (!is_null($this->validator)) {
// we should pass data that has been casts by the model
// to make sure data type are same because validator may need to use
// this data to compare with data that fetch from database.
if ($this->versionCompare($this->app->version(), "5.2.*", ">")) {
$attributes = $this->model->newInstance()->forceFill($attributes)->makeVisible($this->model->getHidden())->toArray();

Please sign in to comment.