Using CSqlDataProvider with CListView

Hi guys,

to my understanding I need to provide the number of results to the CSqlDataProvider to get pagination to work with CListView. My problem is that I have a pretty complex and "heavy" query I do not want to run twice. Does anyone have a nice way to do this without running the query twice? Or any other good advise?

Cheers!:slight_smile:

Ole

u might want to modify your query instead to shorten it or limit it… query cost is there…

No way:

The pagination has to know how many page items to display and you have to assign the ‘totalItemCount’.

Maybe you have a simple query to get this value. Then you can override the ‘getTotalItemCount’ of the CSqlDataProvider or set ‘totalItemCount’ to this value.

Another solution - but very, very ‘heavy’ - and I wouldn’t do this:

You can fetch all records at once and create a CArrayDataProvider with the results from $dataProvider->getData();

But then this is a ‘fake’ pagination where always all items are fetched instead of only the items of a single page.

Thanks for your answers.

Since this is a part of a "search engine" on my site, I cannot shorten the query. So the only real solution I could find is to run it twice, and set the count from the first round. I am not sure how grave the consequenses will be regarding load and execution time in production.

Wish there was a way around this though.

I have the same problem.

I think you must run the query twice, but not on each page and do the COUNT query without all the fields e.g. SELECT COUNT(id) FROM table.

You can ask if the search button is entered




if(isset($_GET['searchButton']

SELECT COUNT(id) FROM table WHERE...



In my table with a total number of 3000 rows the query time is 0.5 to 0.6 ms.

I think that is ok.