pt0
pt0

Reputation: 175

Kohana 3.2 Call to undefined method Database_MySQL_Result::offset()

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.

Upvotes: 0

Views: 1597

Answers (1)

biakaveron
biakaveron

Reputation: 5483

As you can see, execute() returns Database_Result object, which has no QBuilder's functionality. You must apply all conditions (where, limit, offset etc) before calling execute.

Here is a simple example with pagination:

// 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();

Upvotes: 2

Related Questions