-
-
Notifications
You must be signed in to change notification settings - Fork 215
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
Fix retrieval of property to work for Astrotomic/translatable #883
base: main
Are you sure you want to change the base?
Conversation
Ended up giving up on the idea of using strict mode, because it has a lot of conditions to throw (model exists and was not recently created) and a callback that can change the behavior so this wouldn't have been a reliable method |
if ($this->hasModelAttribute($name)) { | ||
return $this->properties[$name] = $this->model->getAttributeValue($name); | ||
} | ||
|
||
$camelName = Str::camel($name); |
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 think we should get rid of this in the automatic camel, snake casing in the next major, after looking back at it, we should not do this manually for relations and rely on the content mapping of the lib https://spatie.be/docs/laravel-data/v4/as-a-data-transfer-object/model-to-data-object#content-mapping-property-names
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.
Here is the commit for my proposal on how it should be handled in the next major 380942b
Attributes are still snake cased if necessary but not relations
if (! isset($this->attributesProperty)) { | ||
$this->attributesProperty = new ReflectionProperty($this->model, 'attributes'); | ||
$this->attributesProperty->setAccessible(true); |
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.
This line does nothing because: https://www.php.net/manual/en/reflectionproperty.setaccessible.php
Note: As of PHP 8.1.0, calling this method has no effect; all properties are accessible by default.
And we require php 8.1 in composer.json
As seen in #870 the move to
getAttributeValue
bypasses the overriden method https://github.com/Astrotomic/laravel-translatable/edit/main/src/Translatable/Translatable.php#L143This PR goes back to using
getAttribute
but after already checking if it's a relation (because getAttribute loads the relation otherwise, which we don't want), like this both spatie/translatable and astrotomic/translatable will work with this package