Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BatchProvider.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Framework\DB\Adapter\AdapterInterface;
9 
17 {
21  public function getBatches(AdapterInterface $adapter, $tableName, $linkField, $batchSize)
22  {
23  $maxLinkFieldValue = $adapter->fetchOne(
24  $adapter->select()->from(
25  ['entity' => $tableName],
26  [
27  'max_value' => new \Zend_Db_Expr('MAX(entity.' . $linkField . ')')
28  ]
29  )
30  );
31 
33  $truncatedBatchSize = $maxLinkFieldValue % $batchSize;
35  $fullBatchCount = ($maxLinkFieldValue - $truncatedBatchSize) / $batchSize;
36 
37  for ($batchIndex = 0; $batchIndex < $fullBatchCount; $batchIndex ++) {
38  yield ['from' => $batchIndex * $batchSize + 1, 'to' => ($batchIndex + 1) * $batchSize];
39  }
40  // return the last batch if it has smaller size
41  if ($truncatedBatchSize > 0) {
42  yield ['from' => $fullBatchCount * $batchSize + 1, 'to' => $maxLinkFieldValue];
43  }
44  }
45 
49  public function getBatchIds(
50  \Magento\Framework\DB\Adapter\AdapterInterface $connection,
51  \Magento\Framework\DB\Select $select,
52  array $batch
53  ) {
54  $betweenCondition = sprintf(
55  '(%s BETWEEN %s AND %s)',
56  'entity_id',
57  $connection->quote($batch['from']),
58  $connection->quote($batch['to'])
59  );
60 
61  $ids = $connection->fetchCol($select->where($betweenCondition));
62  return array_map('intval', $ids);
63  }
64 }
$tableName
Definition: trigger.php:13
getBatches(AdapterInterface $adapter, $tableName, $linkField, $batchSize)
$adapter
Definition: webapi_user.php:16
getBatchIds(\Magento\Framework\DB\Adapter\AdapterInterface $connection, \Magento\Framework\DB\Select $select, array $batch)
$connection
Definition: bulk.php:13