We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon model offset

Sorry if this question is not related to Phalcon it self, but i am having a problem fetching the higest ID when using offset.

Lets say i have the data:

1

2

3

4

5

6

7

8

9

Then i need to fetch the data ordered like this:

Page 1:

7

8

9

Page 2:

4

5

6

Page 3:

1

2

3

The problem is that when i fetch the data, when using offset it get's the first 3 rows and lot 3 last rows. Changing DESC dosent help.

My model looks like this:

$conversation = Self::find(array(
        'conditions' => '(id = ?1 OR parent_id = ?1) AND user_id = ?2',
        'bind' => array(1 => $comment_id, 2 => $user_id),
        'limit' => 3,
        'offset' => $offset,
        'order' => 'id DESC'
    ));

    return $conversation;

Hope you can help me solve my problem!

edited Feb '18

Hi @gusler the query looks ok

Could not you reverse the result? with array_reverse(iterator_to_array($conversation))

Sorry but I always use offset/limit and it work fine with any order or conditions but you'll have this result : 9 8 7 - 6 5 4 - 3 2 1 - if you want the reverse just start to parse your result from the end.

How do I pass the result from the end? Right now i am using array_reverse, but then i have to convert my model to array.

edited Feb '18
$conversations = Self::find();
$length = count($conversations);
for($i=($length-1); i>=0; $i--){
    // do something with $conversations[$i]
}