livewire-powergrid icon indicating copy to clipboard operation
livewire-powergrid copied to clipboard

search problem when joining multiple tables

Open qhanm opened this issue 5 years ago • 21 comments

Describe the bug image

image

when i search with field "fb.name" how to declare in function columns

Laravel version (exactly):

  • 8.65

Alpine version:

  • "power-components/livewire-powergrid": "^1.4"

Thank for help me

qhanm avatar Nov 15 '21 02:11 qhanm

In my case I have two tables human_resources and centres, where one HR belongs to one centre, so I need to search on centre name which comes from centres table, so I used below modification to make it work:

return HumanResource::query()
                                ->join(DB::raw('
                                    (SELECT id as centre_id, name as centre_name, alpha_code as centre_alpha_code, parent_id
                                    FROM centres) as centre
                                '), function ($join) {
                                    $join->on('human_resources.center_id', '=', 'centre.centre_id');
                                })
                                ->select('human_resources.*','centre.centre_name');

And then I add below line in columns function

Column::add()->title('Centre')->field('centre_name')->makeInputText()

nausif111 avatar Nov 15 '21 08:11 nausif111

See also #111 Not resolved although @luanfreitasdev believes it is.

snapey avatar Nov 15 '21 10:11 snapey

These are different problems, #111 says solve the sortable problem while this one is about advanced search with joins.

Maybe you'll need to put a $table variable inside the searchable so you don't get lost.

luanfreitasdev avatar Nov 15 '21 14:11 luanfreitasdev

@luanfreitasdev Its a problem because you reverted the previous table prefix. There is still a problem with sorting also, so for now, I abandoned using the project

snapey avatar Nov 15 '21 14:11 snapey

Prefix doesn't cause problem with join ?

luanfreitasdev avatar Nov 15 '21 15:11 luanfreitasdev

I uploaded some changes directly to main, which works with a prefix with the table name. The problem with the previous prefix is that in the back state when sortable is clicked, it would no longer recognize it, and the state started like this:

$table = 'dishes'; I clicked on sortable ('categories.name')

The component was understand like this: $table = 'categories.name' (but categories is not the main one in the component).

So to solve this, I removed the round trip data from the component when the table name is the same, allowing no more table not found.

Prefix resolves table duplication.

This change does not fix advanced filters, only component initialization and ordering

Remember that this project is not funded but open-source. Everyone can collaborate with him to make him better in every way.,

luanfreitasdev avatar Nov 15 '21 15:11 luanfreitasdev

I just upload advanced search support with join in inputText, dataPicker and boolean filter.

Example that worked (both in searches and sorting):

Column::add()
                 ->title(__('Dish'))
                 ->field('dishes.name')
                 ->searchable()
                 ->editOnClick($canEdit)
                 ->makeInputText('name')
                 ->sortable('dishes.name'),

Column::add()
                 ->title(__('Created Categories'))
                 ->sortable('categories.created_at')
                 ->field('created_at_formatted')
                 ->makeInputDatePicker('categories.created_at'),

luanfreitasdev avatar Nov 15 '21 18:11 luanfreitasdev

I'm waiting for confirmation if it worked @snapey , @qhanm

luanfreitasdev avatar Nov 15 '21 18:11 luanfreitasdev

Hi, I made some more improvements and I just uploaded improvements.

luanfreitasdev avatar Nov 16 '21 01:11 luanfreitasdev

it doesn't work, when i search by "fb.name as name" field the sql sentence is rendered incorrect, the correct query should be "fb.name like %test%" image @luanfreitasdev

qhanm avatar Nov 16 '21 01:11 qhanm

Did you just install the last commit from main ?

luanfreitasdev avatar Nov 16 '21 02:11 luanfreitasdev

field('name') has to be field('fb.name') to work

luanfreitasdev avatar Nov 16 '21 02:11 luanfreitasdev

yes i just installed the latest livewire-powergrid

qhanm avatar Nov 16 '21 02:11 qhanm

dev-main ? You also have to fix the field('fb.name')

luanfreitasdev avatar Nov 16 '21 02:11 luanfreitasdev

There's no way your code is with the dev-main, as my last commit prefixes the column.

You need to install the package by passing the dev-main

luanfreitasdev avatar Nov 16 '21 02:11 luanfreitasdev

image I think it's because in the query count total record the field name is not found due to "select count(*) aggre"

qhanm avatar Nov 16 '21 02:11 qhanm

You need to do two things;

  1. composer require power-compoments/livewire-powergrid dev-main
  2. Change the field('name') method to field('fb.name')

That done. The name column will become fb.name

luanfreitasdev avatar Nov 16 '21 02:11 luanfreitasdev

oh sorry, since i haven't cleared cache composer, i just updated to the latest and it worked. thanks for your help My problem has been resolved so I'm closing this discussion Thanks.

qhanm avatar Nov 16 '21 02:11 qhanm

You need to do two things;

  1. composer require power-compoments/livewire-powergrid dev-main
  2. Change the field('name') method to field('fb.name')

That done. The name column will become fb.name

qhanm avatar Nov 16 '21 02:11 qhanm

Thanks

luanfreitasdev avatar Nov 16 '21 03:11 luanfreitasdev