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

fix having stale properties after refreshing a model #74

Merged
merged 6 commits into from
Nov 10, 2023
Merged

fix having stale properties after refreshing a model #74

merged 6 commits into from
Nov 10, 2023

Conversation

layerok
Copy link
Contributor

@layerok layerok commented Nov 7, 2023

Lift doesn't refill model attributes after refreshing a model.
In this PR I have fixed it and added a test.

@layerok
Copy link
Contributor Author

layerok commented Nov 8, 2023

Actually I didn't fully fixed the issue. I only fixed refreshement of fields that have default value in the database e.g. ->default(999). But refreshemnt for all other fields is broken. See example below

Schema::create('category_refresheds', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->integer('sort')->default(9999);
    $table->timestamps();
});

class CategoryRefreshed extends Model
{
    use Lift;

    #[PrimaryKey]
    public int $id;

    #[Fillable]
    public string $title;

    #[Fillable]
    public int $sort;
}

($discount = new Category(['title' => 'test'))->save();
$discount->title = 'updated';
$discount->save()->refresh();
echo $discount->title; // outputs 'test'. Not correct!

$discount->sort; // outputs 999. Correct!

@layerok
Copy link
Contributor Author

layerok commented Nov 9, 2023

fixed it and updated the test

@layerok
Copy link
Contributor Author

layerok commented Nov 9, 2023

Added a better fix that overwrites setRawAttributes method instead of refresh method

Copy link
Owner

@WendellAdriel WendellAdriel left a comment

Choose a reason for hiding this comment

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

Nice catch and fix here, thank you so much! 🔥 💪

@WendellAdriel WendellAdriel merged commit e13cf15 into WendellAdriel:main Nov 10, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants