[4.1][Bug] field and filter names being changed by Backpack
See original GitHub issueBug report
What I did
Upgraded from v3.6 to v4.1 by following instructions in documentation.
What I expected to happen
Expected behavior similar to v3.6 where field and filter names matched the ‘name’ attribute of the field/filter definition.
What happened
(1) Field names are being array-ified based on the ‘entity’ attribute (I think?). The following example creates a field named series[line][manufacturer_id]
instead of a field named manufacturer_id
. Because the field is not named manufacturer_id
, the dependent ajax filter returns 0 results.
[
'tab' => 'Basics',
'name' => 'manufacturer_id',
'label' => 'Manufacturer',
'type' => 'select2',
'entity' => 'series.line.manufacturer',
'attribute' => 'name',
'options' => (function ($query) {
return $query->orderBy('name')->get();
}),
],
(2) Filter names are being camelCased based on the ‘name’ attribute. The following example creates a filter named manufacturerId
instead of a filter named manufacturer_id
. The downstream effect is similar to the renamed field example when dependent filters are used.
$this->crud->addFilter(
[
'name' => 'manufacturer_id',
'type' => 'select2',
'label'=> 'Manufacturer',
],
function() {
return \App\Catalog\Models\Manufacturer::orderBy('name')->get()
->pluck('name', 'id')->toArray();
},
function ($value) { // if the filter is active
if (! is_null($value)) {
$this->crud->addClause('whereHas', 'series', function ($series) use ($value) {
return $series->whereHas('line', function ($line) use ($value) {
return $line->where('manufacturer_id', $value);
});
});
}
}
);
What I’ve already tried to fix it
I’ve read all of the issues and pull/merge requests I can find about these two similar changes. They appear to have been done on purpose, but I can not find an explanation.
Backpack, Laravel, PHP, DB version
When I run php artisan backpack:version
the output is:
### PHP VERSION:
PHP 7.3.5-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: May 3 2019 10:00:24) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.5, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.3.5-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies
### LARAVEL VERSION:
v7.21.0@3ccdb116524de408fdc00715b6f06a1031ddace9
### BACKPACK VERSION:
4.1.15@6c751de946a9c8511dd32eb7bfa3ca6a568849f5
Issue Analytics
- State:
- Created 3 years ago
- Comments:20 (7 by maintainers)
Top GitHub Comments
Hey @trip-somers
Glad you found it.
My notes from friday:
I was also getting into the last possible scenarios.
Don’t mind the time I spent debugging this, I found more stuff that we can optimize, and built a complete relation panel to test further issues if needed, it’s not a waste of time.
From this issue I think there is not much more to do here, filter names are going to be addressed in 4.2 as a BC, and I think #3116 has the fix for the issue of BelongsTo beeing renamed.
Thanks again for submitting the issue, if you found some more let us know so we can properly address it.
FYI: you can use unrelated ajax fields. We should update our docs accordingly, sorry for misdirecting you.
It works just like removing model and adding
entity => 'article'
, the difference is that if entity is set, we infer the model from the relation and some other attributes.Best, Pedro
I finally found it. I built a custom field called
select2_from_ajax_plain
that is designed to work with non-relationship. I extended it fromselect2_from_ajax
but failed to change the field init function name. The result was that my newselect2_from_ajax_plain
field was overwriting the init function for theselect2_from_ajax
fields on the page.So this is back down to just the BelongsTo/HasOne mistake. My apologies if you spent much time trying to recreate this. I’m a unicorn.