Sorting with CListView access database?

Hi all,

When using CListView with CActiveDataProvider and sorting option on a field. When we click on the field for sorting, will the page access the database again to fetch the datas or it will just use the same data and change the sorting order to display it correctly?

I checked the code of CListView, CBaseListView, CDataProvider and CActiveDataProvider. It seems to me that a new database access is used each time, but that would not really make sense if we are using the same datas…

Does anyone understand the process better than me?

Sorting and searching in lists and grids always cause ajax request to let the server do this process. There is not client-side sorting and filtering implemented by default.

Hi RedGuy,

Thanks for your answer.

I was not necessary talking about Client side, but at least not access the database again for datas we have already. What if we store in the session an array of CActiveRecord and then we filter/sort it according to the user choices to display more quickly, wouldn’t it be much more efficient than using SQL request each time to fetch the datas?

Not necesary… If you have large dataset (lets say: 10 millions of records) - sorting them in database using index and fetching only one page to display (remember that grid and list displays paginated results) would be much faster and less resource-consuming than storing those 10 millions of records in session, serializing/deserialising on every request, and sort probably using uasort method with php function callback… I would stick with database-side sorting and filtering. All you can do is add query-cache to store results if the queries are realy same all the time. If there is a lot of customization allowed (sorting columns, paging, filtering) - caching will only add overhead to store(serialize) results that won’t be used anymore.

I see, I was thinking in my case, the query is complicated (lots of Join) and for not so many records, so storing in a session might be an interesting choice… but yes you’re definitely right if the quantity of records gets bigger… I didn’t take into consideration the paging…

Thanks for enlighting me ;)