<p>Not totally sure I understand everything going on in your example code, but I believe it could be a simplified a little:</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = 'affiliates';
$criteria-&gt;limit = null;

$rowCount = $criteria-&gt;total();
$batchSize = 100;
$numBatches = ceil($rowCount / $batchSize);

$criteria-&gt;limit = $batchSize;

for ($i = 0; $i &lt; $numBatches; ++$i) {
    $criteria-&gt;offset = $batchSize * $i;
    $affiliates = $criteria-&gt;find();
    foreach ($affiliates as $affiliate) {
        //process
    }
}
</code></pre>

<p>Tested, and works.</p>

CC BY-SA 3.0
2016-08-23T10:05:17.323Z
2016-08-23T10:10:45.393Z
2016-08-23T10:10:45.393Z
Mats Mikkel Rummelhoff
<p>I need to batch process a set of data. To do this I am using the code below. I am trying to process 100 at a time, but the offset doesn't seem to work. It works the first time, but in the second iteration of the loop it doesn't set the offset properly.</p>

<pre><code>  $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
  $criteria-&gt;section = 'affiliates';
  $criteria-&gt;limit = null;
  $row_count = 6000;
  $batch_size = 100;
  $num_batches = ceil($row_count / $batch_size);
  $offset = 100;

  for ($j=0; $j &lt;= $num_batches; $j++) {
      $query-&gt;setOffset($offset);
      $query-&gt;limit = $batch_size;
      $offset = $offset + $batch_size;        
      $queryResults = $query-&gt;queryAll();
      $affiliates = EntryModel::populateModels($queryResults);    

      foreach ($affiliates as $affiliate) {
        //process
      }
   }
</code></pre>

<plugin-development><yii><query>
Setting query offset
# Setting query offset

(Asked by: username_0 on 2016-08-23)

I need to batch process a set of data. To do this I am using the code below. I am trying to process 100 at a time, but the offset doesn't seem to work. It works the first time, but in the second iteration of the loop it doesn't set the offset properly.
  $criteria = craft()->elements->getCriteria(ElementType::Entry);
  $criteria->section = 'affiliates';
  $criteria->limit = null;
  $row_count = 6000;
  $batch_size = 100;
  $num_batches = ceil($row_count / $batch_size);
  $offset = 100;

  for ($j=0; $j <= $num_batches; $j++) {
      $query->setOffset($offset);
      $query->limit = $batch_size;
      $offset = $offset + $batch_size;        
      $queryResults = $query->queryAll();
      $affiliates = EntryModel::populateModels($queryResults);    

      foreach ($affiliates as $affiliate) {
        //process
      }
   }

**Tags:** plugin-development, yii, query

---

## 1 answers

### Answer by username_1 on 2016-08-23. Score: 1

Not totally sure I understand everything going on in your example code, but I believe it could be a simplified a little:
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'affiliates';
$criteria->limit = null;

$rowCount = $criteria->total();
$batchSize = 100;
$numBatches = ceil($rowCount / $batchSize);

$criteria->limit = $batchSize;

for ($i = 0; $i < $numBatches; ++$i) {
    $criteria->offset = $batchSize * $i;
    $affiliates = $criteria->find();
    foreach ($affiliates as $affiliate) {
        //process
    }
}

Tested, and works.


---

