Is it possible to re-use a Kohana ORM query for the row count?

六眼飞鱼酱① 提交于 2019-12-05 00:32:03

问题


So I have my query as so...

$records = ORM::factory('category');

Add a WHERE clause as so...

$records = $records->where('categoryid', 'LIKE', 'aa');

Grab a count for pagination as so...

$count = $records->count_all();

And my where clause gets cleared away as so...

SELECT `categories`.* FROM `categories` LIMIT 20 OFFSET 0

With this line commented out

//$count = $records->count_all();

My SQL looks just fine...

SELECT `categories`.* FROM `categories` WHERE `categoryid` LIKE 'aa' LIMIT 20 OFFSET 0

Is it possible to use a single query the way I'm trying to or do I have to make two duplicate identical queries? One for the count, and one for the actual results...

Thanks!


回答1:


Use special reset(FALSE) call:

$records = $records->where('categoryid', 'LIKE', 'aa');
$records->reset(FALSE); // !!!!
$count = $records->count_all();
$categories = $records->find_all();


来源:https://stackoverflow.com/questions/6615367/is-it-possible-to-re-use-a-kohana-orm-query-for-the-row-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!