0

That happens when I try to play around with DB::select instead of ORM. The query is returned as an object, but the error appears.

Code:

$bd_userdata -> offset($pagination -> offset) -> limit($pagination -> items_per_page) -> find_all() -> as_array();

Error:

ErrorException [ Fatal Error ]: Call to undefined method Database_MySQL_Result::offset()

Does it mean I have to count rows, before I send them to the offset in pagination?


When I try $query->count_all() I get the error message:

Undefined property: Database_Query_Builder_Select::$count_all

I tried count($query) but instead I got:

No tables used [ SELECT * LIMIT 4 OFFSET 0 ]


Here is the solution:

$results = DB::select('*')
->from('users')
->where('id', '=', 1)
->limit($pagination->items_per_page)
->offset($pagination->offset)->execute();

And a counter:

$count = $results->count_all();

I was doing it before, the other way around. That is why it did not work.

4

1 回答 1

2

如您所见,execute()返回Database_Result对象,它没有 QBuilder 的功能。您必须在调用之前应用所有条件(where、limit、offset 等)execute

这是一个带有分页的简单示例:

// dont forget to apply reset(FALSE)!
$query = DB::select()->from('users')->where('username', '=', 'test')->reset(FALSE);
// counting rows
$row_count = $query->count_all();
// create pagination
$pagination = Pagination::factory(array(
        'items_per_page'    =>  4,
        'total_items'       =>  $row_count,
    ));
// select rows using pagination's limit&offset 
$users = $query->offset($pagination->offset)->limit($pagination->items_per_page)->execute();
于 2013-05-11T06:48:35.470 回答