4

I want to retrieve magento 2 model collection using limit offset, like raw query like this:

$rawQuery = SELECT * FROM test WHERE test_id = '".$test_id."' and status = '1' LIMIT 600 OFFSET ".$offset;

i have tried using this,but not working,

    $collections = $model->getCollection()
    ->addFieldToFilter('test_id',array('eq'=>$test_id))
    ->addFieldToFilter('status',array('eq'=>'1'))
    ->setLimit(600)->setOffset($offset);

2 Answers 2

6

Use the $collection->setPageSize(600) and $collection->setCurPage($offset) methods.

5

You can set limit and offset like this,

$collectionLimit = 600;

$collectionOffset = $offset;

$collections = $model->getCollection()
->addFieldToFilter('test_id',array('eq'=>$test_id))
->addFieldToFilter('status',array('eq'=>'1'));

$collections->getSelect()->limit($collectionLimit, $collectionOffset);
2
  • when i use foreach for $collections, it retrieve empty data Commented Mar 15, 2017 at 3:34
  • is this work with pagination for custom collection? Commented Mar 4, 2019 at 4:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.