question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[4.1][Bug] field and filter names being changed by Backpack

See original GitHub issue

Bug 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:closed
  • Created 3 years ago
  • Comments:20 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
pxpmcommented, Aug 17, 2020

Hey @trip-somers

Glad you found it.

My notes from friday:

....
- could not reproduce wrong sent of value using api or fetch operation. 
- could be environment specific ? Ask for dev. environment.
- ask if user had overwritten any of the field files.

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.

 $this->crud->addField([
            'name' => 'select2_from_ajax',
            'type' => 'select2_from_ajax',
            'data_source' => backpack_url('features/fetch/article'),
            'dependencies' => ['number'],
            'include_all_form_fields' => true,
            'placeholder' => 'select smth',
            'minimum_input_length' => 0,
            'method' => 'POST',
            //'entity' => 'article',
            'model' => 'Backpack\NewsCRUD\app\Models\Article',
            'attribute' => 'title',
            'tab' => 'Relations',
            'label' => 'select2 from ajax depends on category'
        ]);

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

1reaction
trip-somerscommented, Aug 16, 2020

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 from select2_from_ajax but failed to change the field init function name. The result was that my new select2_from_ajax_plain field was overwriting the init function for the select2_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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using textual bug reports to predict the fault category of ...
Since exhaustive examination of all issues is deemed infeasible, we need to filter and preselect issues for manual examination. We therefore performed a...
Read more >
How About Bug-Triggering Paths? - Yulei Sui
File “libswscale/swscale.c”, a pointer “filter” is pointed to a ... information and then transformed into a compact vector. ... 4.1 Bug-triggering paths.
Read more >
DOWNLOAD Sample Pages - Teacher Created Materials
people know if your plans change. • Check local travel conditions before you depart and again at time of travel. • Learn about...
Read more >
On the use of automatic classification for bug triaging in industry
and it is a condition of accessing publications that users recognise and abide by the legal requirements associated with these rights.
Read more >
Bug Triaging with High Confidence Predictions
Correctly assigning bugs to the right developer or team, i.e., bug triaging, is a costly activity. A concerted effort at Ericsson has been...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found