Unexpected behaviour with getSubfieldsValues in update trait
See original GitHub issueThere appears to be an issue with function getSubfieldsValues in the update trait. Specifically the part that handles fields that have a dot in the name
Let assume we have a model called Address with attribute street_number having an integer value of 1 so
$name = "address.street_number"
Right after this block of code is executed
https://github.com/Laravel-Backpack/CRUD/blob/bdf1349c88f8f621f568c97f6cad16e26f788b62/src/app/Library/CrudPanel/Traits/Update.php#L260-L263
The iterator has the following value
$iterator = 1
Is there a reason why the code assumes the attribute value would be a string if it is not a model instance ? https://github.com/Laravel-Backpack/CRUD/blob/bdf1349c88f8f621f568c97f6cad16e26f788b62/src/app/Library/CrudPanel/Traits/Update.php#L258-L259
Given that iterator now contains a number, as you can expect
!is_string($iterator = 1) // true
&&
!is_null($iterator = 1) // true
$this->getModelWithFakes($iterator = 1)->getAttributes()
So when getModelWithFakes is called it results in an exception when class_uses_recursive is called using a value that is not a model https://github.com/Laravel-Backpack/CRUD/blob/bdf1349c88f8f621f568c97f6cad16e26f788b62/src/app/Library/CrudPanel/Traits/Update.php#L195-L202
Solution: If the idea is to call getModelWithFakes only in the case when $iterator is a model then perhaps the code could be rewritten as
$iterator instanceof \Illuminate\Database\Eloquent\Model ? $this->getModelWithFakes($iterator)->getAttributes() : $iterator
Issue Analytics
- State:
- Created a year ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
Hello @fournodes
Thanks for the report. Unfortunately I was not able to reproduce it, maybe I am using the wrong usecase.
From my understanding some of your
subfields
aredot.notation
fields. We have the same example in our demo: https://demo.backpackforlaravel.comIn
Monsters & Stuff
menu, you will find theStory
for example, this Story HasMany Monsters, and those Monsters have some fields inside that usedot.notation
as for exampleaddress.street
.Can you please provide me a more detailed implementation of your usecase so I can attempt to reproduce it ?
Cheers
Merged. Thanks guys! Will be available starting Monday when we release a new patch version.
In regards to the
is_a()
vsinstanceof
debated… I’ve merged it withis_a()
. Not because I think it’s more readable (I think they’re both readable) but because… the performance improvement is so small that it can be approximated to zero. Let’s keep the context in mind - we’re running on a “fat” framework here, Laravel itself usesis_a
throughout its code, which gets run upon every request. In my opinion, one more call will not do any harm, on less call will not do any good. Let’s not let this distract us from squashing bugs, it really doesn’t matter 💪Thanks again, cheers!