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

Use getDirty instead of trying to find dirty data manually #8

Closed
duellsy opened this issue Apr 3, 2013 · 8 comments
Closed

Use getDirty instead of trying to find dirty data manually #8

duellsy opened this issue Apr 3, 2013 · 8 comments
Assignees
Milestone

Comments

@duellsy
Copy link
Member

duellsy commented Apr 3, 2013

Eloquent offers a method getDirty(), this can be used instead of doing the whole before / after comparison stuff

@ghost ghost assigned duellsy Apr 3, 2013
@duellsy duellsy closed this as completed in 27cc42f Apr 8, 2013
@Antonimo
Copy link

looks like you still trying to do it manually together with the dirty method.
even the core only uses dirty:

$this->setKeysForSaveQuery($query)->update($dirty);

https://github.com/laravel/framework/blob/2680f371eed4b0d6d26818aee0e1eaff79892b96/src/Illuminate/Database/Eloquent/Model.php#L1345

@duellsy
Copy link
Member Author

duellsy commented Feb 23, 2014

Not sure where you're referring to?

The changedRevisionableFields() method uses the getDirty() method, and checks that the updated item is indeed something that the owner wants tracked.

https://github.com/VentureCraft/revisionable/blob/master/src/Venturecraft/Revisionable/Revisionable.php#L164

@Antonimo
Copy link

i'm referring bough to beforesave and changedRevisionableFields()
where you use $this->originalData and $this->updatedData.
I think in beforesave maybe you can do only

$this->dirty = $this->getDirty();

@duellsy
Copy link
Member Author

duellsy commented Feb 24, 2014

Sure, essentially I wanted to clean the data my way before calling getDirty(), but it could just as easily only use the getDirty() method and do a cleanup on the results of that to create the $this->dirty value. Without wanting to be arrogant, I'm taking an 'if it aint broke, don't fix it' approach to this one.

If you want to make a pull request though, go for it and I'll merge it in.

@Antonimo
Copy link

i'm taking the same approach for now =) maybe later if I have time Ill make
a pull request

2014-02-24 23:37 GMT+02:00 Chris Duell notifications@github.com:

Sure, essentially I wanted to clean the data my way before calling
getDirty(), but it could just as easily only use the getDirty() method
and do a cleanup on the results of that to create the $this->dirty value.
Without wanting to be arrogant, I'm taking an 'if it aint broke, don't fix
it' approach to this one.

If you want to make a pull request though, go for it and I'll merge it in.

Reply to this email directly or view it on GitHubhttps://github.com//issues/8#issuecomment-35942182
.

@duellsy
Copy link
Member Author

duellsy commented Feb 25, 2014

Cool. There's also just recently been quite a few updates to the whole section for #36

@mnfgul
Copy link

mnfgul commented Sep 3, 2016

@duellsy Hi,

While making update, getDirty returns empty in my case. Details are as follow;

  • I am using trait in my model
  • Updating the model using;
    MyModel::whereId(1)->first()->update(['field' => 'value']).
  • This was not creating revisions. I digged into code and found that get $this->dirtyData = $this->getDirty(); return empty array.

Can you help about this issue.

Thanks.

@mnfgul
Copy link

mnfgul commented Sep 3, 2016

I found my answer. The reason was that getDirty() does not return changes in case of mass updates.

  • Model::update([]) does not work
  • $model->attribute = 'value'; $model->save(); works.

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

No branches or pull requests

3 participants