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

Possible bug in auditSync: Unexpected old/new values #931

Closed
tricki opened this issue May 22, 2024 · 4 comments
Closed

Possible bug in auditSync: Unexpected old/new values #931

tricki opened this issue May 22, 2024 · 4 comments
Labels
duplicate A repeated issue or entry

Comments

@tricki
Copy link
Contributor

tricki commented May 22, 2024

Q A
Bug? yes
New Feature? yes
Framework Laravel
Framework version 10
Package version 13
PHP version 8

Actual Behaviour

The "old" and "new" values for auditSync don't make much sense to me, but because the documentation doesn't list an example I don't know if this is actually a bug or expected behavior.

  1. Object has relation to A, B and C
  2. Sync, removing A and adding D
  3. In the audit: Old = A and New = D
  4. Sync, removing B
  5. In the audit: Old = B and New = []

Expected Behaviour

At step 4, I'd expect Old to be "A, B, C", and New to be "B, C, D". In step 5, I'd expect Old = "B, C, D" and New = "C, D"

Steps to Reproduce

I wrote a simple failing test to illustrate: #930

@erikn69
Copy link
Contributor

erikn69 commented May 22, 2024

#919 (comment)

@willpower232
Copy link
Contributor

  1. Sync, removing A and adding D

I think the problem is here, using sync to append relations is tricky because as pointed out sync will reset the whole relationship.

You would have to do something like

$categories = $model->categories;

// remove D from categories

$categories->push($A);

$model->auditSync('categories', $categories);

See also

@parallels999 parallels999 added the duplicate A repeated issue or entry label May 22, 2024
@tricki tricki changed the title Possible bug in auditSync Possible bug in auditSync: Unexpected old/new values May 23, 2024
@tricki
Copy link
Contributor Author

tricki commented May 23, 2024

OK, then I misunderstood how it's supposed to work (basically old/new becomes removed/added). I think what confused me was this line in the docs:

The audited value is the array version of the relationship return!

Could be rephrased or extended.

Thanks for the quick response. Closing this issue. I've also changed the title hoping it will be easier to discover by anyone having the same problem.

@tricki tricki closed this as completed May 23, 2024
@parallels999
Copy link

Could be rephrased or extended.

I think so, you can open a PR to make it more understandable, English is not my language

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate A repeated issue or entry
Projects
None yet
Development

No branches or pull requests

4 participants