This repository has been archived by the owner on Sep 20, 2019. It is now read-only.
[3.x] Revert to using laravel-schemaless-attributes for meta_data #194
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In the version 3.x of
laravel-event-projector
thelaravel-schemaless-attributes
dependency was removed.That dependency allowed us to use the following syntax to update an
EloquentStoredEvent
'smeta_data
attribute:This syntax is the one outlined in this package documentation.
With the dependency removal this syntax no longer works and an
ErrorException: Indirect modification of overloaded property
is thrown when trying to set a key in themeta_data
property using the above notation.This error is due that attributes from an Eloquent Model are retrieved due through the
__get
magic method and not accessed directly. This is a PHP limitation as it returns a copy of the value instead of a reference to the underlying array.When using the
laravel-schemaless-attributes
package an instance of theSchemalessAttributes
class was returned which implements theArrayAccess
interface. As objects are returned by reference, the syntax in the snippet above works as expected.One workaround is to use the following syntax:
This will pass a new array to the
__set
magic method on theEloquentStoredEvent
model and it will replace the underlyingmeta_data
attribute.The downside is if you have multiple places (listeners for example) that can modify the
meta_data
property. The user should keep track of the previous value to prevent overriding the whole array and loosing previous changes.I added a test to make sure the documented syntax works. I appreciate if you can review the tests where I have two doubts:
I needed to use a database raw cast to JSON to compare against themeta_data
column in the databaseEloquentStoredEvent::flushEventListeners();
to the end of the test, not sure if that is needed.I will happily make any changes to improve this test
EDIT I removed the MySQL specific syntax due to tests failing with PostgreSQL
Epilogue
The tests already have a test against manipulating an event's meta_data property, why this PR is needed?
Indeed there are already two tests that test against an event's
meta_data
modification, but those tests assert against the newStoredEvent
class which is a wrapper around theEloquentStoredEvent
.In this new class the
meta_data
is a first-party property, so it is not accessed through the__get
magic method, thus the modification through the offset set syntax is allowed.