Created
September 22, 2017 12:35
-
-
Save loburets/0b6a259cebe7f6a05a82d7d1244c4c49 to your computer and use it in GitHub Desktop.
Different chunks for DB navigation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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