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

Sorting with Callback #193

Open
shyammtp opened this issue Sep 13, 2018 · 3 comments
Open

Sorting with Callback #193

shyammtp opened this issue Sep 13, 2018 · 3 comments

Comments

@shyammtp
Copy link

I have a issue with Eloquent Model with multiple table join and its column collected in a alias name.
In the Query:

$query = Activity::from('activity_log as a');  
        $query->leftJoin('users as u','u.id','=','a.causer_id')
        ->select('a.id','a.log_name','a.description','u.name','a.created_at as created_at','a.properties'); 
(new FieldConfig)
                        ->setName('created_at')
                        ->setLabel('Date')
                        ->setCallback(function ($val, EloquentDataRow $row) {   
                            return $val;
                        })->setSortable(true)
                        ->setSorting(Grid::SORT_DESC)

I can't able to do the sortable on the created_at column, because it wont take alias a.created_at on the fieldname. I hope i can do this if i have Sortable Callback available on the library.

Please help me. How to solve this case?

@wdog
Copy link
Contributor

wdog commented Sep 14, 2018

Can you post the error? I have tons of aliases and they work.

@shyammtp
Copy link
Author

shyammtp commented Sep 14, 2018

Here it is,

BTW, I am using SQL Database
If i set as like this

(new FieldConfig)
                        ->setName('created_at')
                        ->setLabel('Date')
                        ->setCallback(function ($val, EloquentDataRow $row) {   
                            return $val;
                        }) 
                        ->setSortable(true)
                        ->setSorting(Grid::SORT_DESC)

it throws:
SQLSTATE[HY000]: General error: 20018 Ambiguous column name 'created_at'. [20018] (severity 16) [(null)] (SQL: select * from (select [a].[id], [a].[log_name], [a].[description], [u].[name], [a].[created_at], [a].[properties], row_number() over (order by [created_at] desc) as row_num from [activity_log] as [a] left join [dimasuser] as [u] on [u].[id] = [a].[causer_id]) as temp_table where row_num between 51 and 100 order by row_num)

But if i set like this:

(new FieldConfig)
                        ->setName('a.created_at')
                        ->setLabel('Date')
                        ->setCallback(function ($val, EloquentDataRow $row) {   
                            return $val; 
                        }) 
                        ->setSortable(true)
                        ->setSorting(Grid::SORT_DESC)

I cant able to get the value for created_at.

I have fixed this issue by overriding the Sorter.php class and added SortingCallback

(new FieldConfig)
                        ->setName('created_at')
                        ->setLabel('Date')
                        ->setCallback(function ($val, EloquentDataRow $row) {   
                            return $val;
                        })
                        ->setSortableCallback(function($provider, $sort) { 
                            $provider->orderBy('a.created_at',$sort[1]); 
                        }) 
                        ->setSortable(true)
                        ->setSorting(Grid::SORT_DESC)

Thanks, Please let me know if you have any suggestion on this.

@wdog
Copy link
Contributor

wdog commented Sep 17, 2018

the query in the first post is different from the second one. I contains dimasuser table and a select * from ( select ... ) and more...

can you post/check the query?

I think that if you define only 1 time the field created_at in the select there would be no problem.

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

No branches or pull requests

2 participants