kartik-v / yii2-dynagrid

Turbo charge the Yii 2 GridView with personalized columns, page size, and themes.
http://demos.krajee.com/dynagrid
Other
74 stars 66 forks source link

Sort on joined table not working #117

Closed maz0717 closed 8 years ago

maz0717 commented 8 years ago

I was previously using the build in yii2 gridview and have sorting setup on a related table using the following code in my "search()" function:

$dataProvider->sort->attributes['times_ordered'] = [ 'asc' => ['customer_info.times_ordered' => SORT_ASC], 'desc' => ['customer_info.times_ordered' => SORT_DESC], ];

Initial sorting works fine, when I save the sort, and return the the page, I receive the following error:

PHP Notice – yii\base\ErrorException

Undefined index: customer_info.last_order_date

  1. in ..\vendor\yiisoft\yii2\data\Sort.php at line 213

Looking at the data saved in the database, it seems that the database key is being saved instead of the attribute.

maz0717 commented 8 years ago

Tracked down the issue. The problem is that the database field names are being used to store the details and not the actual attribute names. If an attribute name is not the same as the database name, the saved sort will fail when the page is reloaded. I fixed the issue by changing line 1048 in DynaGrid.php from: $model->data = $isValidSort ? $sort->getOrders() : [];

to: $model->data = $isValidSort ? $sort->getAttributeOrders() : [];

maz0717 commented 8 years ago

https://github.com/kartik-v/yii2-dynagrid/pull/119