-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Add mergeData
method to DataObject
#8814
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think adding new functionality to Magento\Framework\DataObject
is a good idea. We are moving from inheritance-based to interface-based object hierarchy, ideally eliminating all super-abstract classes like blocks and models at some point.
So, just put any additional logic you need in a neat single-responsible class.
As far as I understood the problem you are trying to solve something like $dataObject->setData(\Magento\Framework\Stdlib\ArrayUtils::merge($dataObject->getData(), $value))
would do pretty the same.
Also, there are existing methods \Magento\Framework\Stdlib\ArrayManager::merge
and \Zend\Stdlib\ArrayUtils::merge
which also do perform some array merging.
I can see why you wouldn't want to pollute DataObject with additional functionality. However, I don't think it's really out of scope for that class; it already provides ways to descend into keys that are storing arrays via For example, Is the future plan to eliminate DataObject completely? Should that class not be relied on for creating models at all to ensure future compatibility? If that's the case, what is the "Magento approved" way to create models that contain arbitrary getters and setters? The model system seems to directly rely on DataObject, so I find it surprising that DataObject would be slated for removal. |
This better to be removed actually for better performance, so that you can use such functionality explicitly by some
No, it's new functionality never existed in
Let's wait for the official response ;) |
My mistake, I meant to remain consistent with I'd also argue that if |
Hi @dersam I do not think we can accept this Pull Request in its current state, since |
If this functionality doesn't belong in Closing, thanks for the feedback. |
@dersam Thank you. |
…beta3-develop Sync 2.4 develop with 2.4.7 beta3 develop
It's not uncommon to have two arrays that you wish to merge together recursively, for later descent.
\Magento\Framework\DataObject
provides the retrieval method throughgetData
, where a slash separated path can be used to descend into an array to retrieve a specific item, without having to extract the whole array and do a bunch ofisset
checks.However, there's no equivalent for setting data. If a key already exists in the DataObject with an array value, setting another array will always overwrite the existing data completely.
This pull request proposes adding a new
mergeData
method. This method will perform a recursive merge between the current data in the given key and the provided data. If the key is an array, it will merge against the entire dataset. If the key does not currently exist in the DataObject, it will fall back to the standard behavior ofsetData
.Making this a separate method instead of adding it to
addData
orsetData
ensures that this functionality must be used explicitly, avoiding any backwards compatibility issues.The merge method is from http://php.net/manual/en/function.array-merge-recursive.php#92195. It has been slightly refactored to adhere to the style guide.