-
-
Notifications
You must be signed in to change notification settings - Fork 629
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
base: master
Are you sure you want to change the base?
BugFix Trait AsSource #1988
Conversation
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)) { |
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.
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)
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.
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?
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 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.)
platform/tests/Unit/Screen/SourceTest.php
Lines 67 to 91 in 39d631a
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]); | |
} |
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.
Oh, I see, it must support dot notation.
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.