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

BugFix Trait AsSource #1988

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ivanomatteo
Copy link

@ivanomatteo ivanomatteo commented Dec 1, 2021

The trait is part of the model class, so can access also to private and protected members.
For example if we use this trait on a model with a nullable database field called "visible"

Arr::get($this->toArray(), $field) ---> is null
Arr::get($this->getRelations(), $field) ---> is null
$this->$field ---> refear to "protected $visible = []"

with this commit the problem is solved, and anso bring a performance improvement:
->toArray() force the execution of all casts and accessors, but only one filed is requested.

The trait is part of the model class, so can access also to private and protected members.

For example if we use this trait on a model with a nullable database field called "visible" 

Arr::get($this->toArray(), $field) ---> is null
Arr::get($this->getRelations(), $field) ---> is null
$this->$field ---> refear to "protected $visible = []"

with this commit the problem is solved, and anso bring a performance improvement:
->toArray() force the execution of all casts and accessors, but only one filed is requested.
return Arr::get($this->toArray(), $field)
?? Arr::get($this->getRelations(), $field)
?? $this->$field;
if ($this->isRelation($field) && !$this->relationLoaded($field)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines will break the usual behavior that users are accustomed to. For example with access to private properties to be prohibited, it was enough to change only
the call to $this->$field to$this->getAttribute($field)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, do you mean when passing $field === null?

in that case just prepend:

if($field === null){
return $this->toArray();
}

It’s unnecessary serialize all fields to get just one.

Also public class property can override database fields with the current code.
But using getAttribute() it will not happen anymore:
Is the right behavior?
Or do you think it’s a breaking change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, for solution #1989 it is enough to change the call to getAttribute.
Other changes do not affect issue #1989. Plus, they break current tests (and existing user code.)

public function testGetSimpleAttribute(): void
{
$this->assertEquals(8, $this->model->getContent('id'));
$this->assertEquals('Alexandr Chernyaev', $this->model->getContent('name'));
$this->assertEquals('red', $this->model->getContent('color'));
}
public function testGetArrayAttribute(): void
{
$this->assertIsArray($this->model->getContent('options.country'));
$this->assertContains('Russia', $this->model->getContent('options.country'));
$this->assertIsBool($this->model->getContent('options.skills.php'));
$this->assertTrue($this->model->getContent('options.skills.php'));
}
public function testGetRelation(): void
{
$this->assertIsInt($this->model->getContent('many.three'));
$this->assertEquals(84, $this->model->getContent('many.three'));
$this->assertContains('one', $this->model->getContent('many'));
$this->assertContains('two', $this->model->getContent('many'));
$this->assertEquals('one', $this->model->getContent('many')[0]);
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, it must support dot notation.

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

Successfully merging this pull request may close these issues.

None yet

2 participants