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

BaseActiveRecord::isAttributeDirty breaking change >= 2.0.46 #19743

Closed
everskies opened this issue Jan 20, 2023 · 3 comments
Closed

BaseActiveRecord::isAttributeDirty breaking change >= 2.0.46 #19743

everskies opened this issue Jan 20, 2023 · 3 comments
Assignees
Labels
Milestone

Comments

@everskies
Copy link

everskies commented Jan 20, 2023

I've noticed that #19272 modified the way that ActiveRecord array attributes are compared when marking them as dirty.

The newest version sorts the old and new array values recursively before comparing for equality.

This makes perfect sense for associative arrays, however the change didn't take lists into account.

I assume that we should first check if the array is not associative by using BaseArrayHelper::isIndexed or similar, and sort only if it's associative (not indexed). Otherwise the above mentioned PR is a breaking change and should be noted

What steps will reproduce the problem?

Changing the order of array elements for an ActiveRecord property doesn't mark the attribute as dirty.

$someNewRecord->data = [1, 2];
$someNewRecord->save();

$someNewRecord->data = [2, 1];
$someNewRecord->save();

What is the expected result?

The data attribute should should be [2, 1]

What do you get instead?

The data attribute is [1, 2] (Didn't get committed to DB)

Additional info

Q A
Yii version 2.0.47
PHP version 8.2
Operating system Unix
@bizley
Copy link
Member

bizley commented Jan 20, 2023

Is the array stored as a JSON in the DB? IIRC the order of the elements there is arbitrary and you cannot rely on it anyway.

@everskies
Copy link
Author

everskies commented Jan 20, 2023

Is the array stored as a JSON in the DB? IIRC the order of the elements there is arbitrary and you cannot rely on it anyway.

We're using a MySQL back-end for ActiveRecord. From my understanding, the order of items in an JSON array is preserved, but key => value pairs can be arbitrarily sorted and should not be relied on (hence the linked PR is perfect for that)

@samdark samdark added the type:bug Bug label Jan 20, 2023
@samdark samdark added this to the 2.0.48 milestone Jan 20, 2023
@everskies everskies changed the title BaseArrayHelper::isAttributeDirty breaking change >= 2.0.46 BaseActiveRecord::isAttributeDirty breaking change >= 2.0.46 Jan 24, 2023
samdark added a commit that referenced this issue May 21, 2023
@samdark samdark self-assigned this May 21, 2023
@kyliemsauter
Copy link

I think there's a still a bug with this, isValueDifferent() is sorting the arrays if they aren't associative when it's supposed to be sorting them when they ARE associative.

if (is_array($newValue) && is_array($oldValue) && !ArrayHelper::isAssociative($oldValue)) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants