Skip to content
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

Feature server side order and search for belongsto field #5185

Conversation

MrCrayon
Copy link
Collaborator

@MrCrayon MrCrayon commented Jan 5, 2021

Enable order and search server side for belongsto relationship fields.

For the search I am applying a global scope, that's the only things that will not require to reimplement pagination manually, for that reason I had to move search checks because globalScope needs to be added as first thing or will not work.

Added a couple of tests, both using a scope and sofdelete work but we should probably add tests for those too.

Closes #2661 and closes #3595

Supersede #5120

@codecov
Copy link

codecov bot commented Jan 5, 2021

Codecov Report

Merging #5185 (3ff9f9d) into 1.4 (8b030a3) will increase coverage by 0.36%.
The diff coverage is 95.23%.

Impacted file tree graph

@@             Coverage Diff              @@
##                1.4    #5185      +/-   ##
============================================
+ Coverage     63.21%   63.58%   +0.36%     
- Complexity     1383     1384       +1     
============================================
  Files           194      194              
  Lines          4081     4081              
============================================
+ Hits           2580     2595      +15     
+ Misses         1501     1486      -15     
Impacted Files Coverage Δ Complexity Δ
src/Http/Controllers/VoyagerBaseController.php 60.18% <95.23%> (+2.49%) 148.00 <2.00> (+5.00)
src/Models/DataType.php 61.06% <0.00%> (+0.76%) 56.00% <0.00%> (ø%)
src/Http/Controllers/VoyagerController.php 38.00% <0.00%> (+4.07%) 12.00% <0.00%> (-4.00%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8b030a3...6793780. Read the comment docs.

@MrCrayon
Copy link
Collaborator Author

MrCrayon commented Jan 7, 2021

I have checked your PR on my local environment and sorting was without problem. Searching in via title was also fine but when I tried to search by parent I didn't get anything - just a blank white page

@Stehos thanks again for checking.
I saw you posted on Slack about some unrelated problem, was that connected or search still does not work?
Can you check your logs to see if there is any error?

Copy link

@Stehos Stehos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MrCrayon I checked this and both things seems to be working for me without issues. Searching sorting, too.

@Stehos
Copy link

Stehos commented Jan 9, 2021

I have checked your PR on my local environment and sorting was without problem. Searching in via title was also fine but when I tried to search by parent I didn't get anything - just a blank white page

@Stehos thanks again for checking.
I saw you posted on Slack about some unrelated problem, was that connected or search still does not work?
Can you check your logs to see if there is any error?

Sorry, I didn't see your comment. The discussion on the slack was not related to this thing. I also approved your last commit and I am not sure but I think this was causing my issues two days ago. Thank you a lot.

@emptynick emptynick merged commit f34cc95 into thedevdojo:1.4 Jan 28, 2021
@MrCrayon MrCrayon deleted the feature-server-side-order-and-search-for-belongsto-field branch February 11, 2021 03:48
@muriloferanti
Copy link

muriloferanti commented Jun 8, 2022

This work for me

            // Update
            if ($search->value != '' && $search->key && $search->filter) {
                $search_filter = ($search->filter == 'equals') ? '=' : 'LIKE';
                $search_value = ($search->filter == 'equals') ? $search->value : '%'.$search->value.'%';

                $isRelation = false;
                $relationshipIds = [];
                $sortableColumns = $this->getSortableColumns($dataType->browseRows);

                $oldchampson = '';

                $searchField = $dataType->name.'.'.$search->key;

                foreach ($dataType->browseRows as $key => $row) {
                    if ($row->type == 'relationship' && isset($row->details) && $row->details->type == 'belongsTo'){
                        if($search->key == $row->field){
                            $relationshipIds = DB::table($row->details->table)
                                ->select($row->details->key, $row->details->label)
                                ->where($row->details->label, $search_filter, $search_value)
                                ->pluck($row->details->key)
                                ->toArray();
                            $oldchampson = $row->details->column;
                            $isRelation = true;
                        }
                    }
                }
                if($isRelation) {
                    $query->whereIn($oldchampson,$relationshipIds);
                } else {
                    $query->where($searchField, $search_filter, $search_value);
                }
            }
            // Update

@theqdev
Copy link

theqdev commented May 1, 2024

Using @muriloferanti's solution works well, as it fixes belongsTo type of relations search filters, while also preserving search fields types when searching/paginating (eg: {String} Username - {Int} User Id).

On top of this, I've used the following snippet to completely disable hasOne type of relations, for which I don't have a fix yet, the search still failing for them (but I'm not relying on them too much either now).

        if ($dataType->server_side) {
            $searchNames = $dataType->browseRows->filter(function ($formfield) {
                if($formfield->type === 'relationship') {
                    if($formfield->details->type === 'belongsTo'){
                        return true;
                    }
                    else{
                        return false;
                    }
                }
                return true;
            })->mapWithKeys(function ($row) {
                return [$row['field'] => $row->getTranslatedAttribute('display_name')];
            });
        }

_Fix similar to the one attempted in here @ #5312 . _

If we could get a fix for the hasOne type of relationships search in a similar manner, I think we could sketch a PR that would close a bunch if not all existing items related to server-side search - I will re-visit this one as well when I get the chance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BelongsTo ServerSide Search Sort by relationship column
5 participants