Skip to content

Instantly share code, notes, and snippets.

@loburets
Created September 22, 2017 12:35
Show Gist options
  • Select an option

  • Save loburets/0b6a259cebe7f6a05a82d7d1244c4c49 to your computer and use it in GitHub Desktop.

Select an option

Save loburets/0b6a259cebe7f6a05a82d7d1244c4c49 to your computer and use it in GitHub Desktop.
Different chunks for DB navigation
<?php
/**
* Special implementation of chunk
* Used in case when records' count depends on dynamic condition and decreases during performing to zero
* In this way default chunk of query builder can't work correctly
* @param $query
* @param $callback
*/
private function chunkForDecreasesCollection($query, $callback)
{
while ($query->count() > 0) {
$entities = $query->take($this->chunkSize)->get();
foreach ($entities as $entity) {
$callback($entity);
}
}
}
/**
* Custom implementation of chunk
* @param $query
* @param $callback
*/
private function chunk($query, $callback)
{
$step = $this->chunkSize;
$count = $query->count();
for ($i = 0; $i < $count; $i += $step) {
$entities = $query->skip($i)->take($step)->get();
foreach ($entities as $entity) {
$callback($entity);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment