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

Fix retrieval of property to work for Astrotomic/translatable #883

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Tofandel
Copy link
Contributor

@Tofandel Tofandel commented Oct 10, 2024

As seen in #870 the move to getAttributeValue bypasses the overriden method https://github.com/Astrotomic/laravel-translatable/edit/main/src/Translatable/Translatable.php#L143

This 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

@Tofandel Tofandel marked this pull request as draft October 10, 2024 13:59
@Tofandel Tofandel marked this pull request as ready for review October 10, 2024 14:27
@Tofandel
Copy link
Contributor Author

Tofandel commented Oct 10, 2024

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);
Copy link
Contributor Author

@Tofandel Tofandel Oct 20, 2024

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

Copy link
Contributor Author

@Tofandel Tofandel Oct 20, 2024

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);
Copy link
Contributor Author

@Tofandel Tofandel Oct 20, 2024

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

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

Successfully merging this pull request may close these issues.

1 participant