Skip to content

Instantly share code, notes, and snippets.

@chardcastle
Created October 20, 2014 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chardcastle/f3b5eedef975de8f28a3 to your computer and use it in GitHub Desktop.
Save chardcastle/f3b5eedef975de8f28a3 to your computer and use it in GitHub Desktop.
Traverse array in batch YII Framework
<?php
/**
* Prototype method for traversing large records in batches
* using the YII framework.
*
* Completed stepper method for restoration rollback solution.
*
* @todo Include this in main restoartion rollback solution
* @return [type] [description]
*/
public function actionInspect()
{
$offset = Controller::getGlobal ("offsetTitle");
$batchSize = 50;
if (empty($offset) || $offset == "finished")
{
// Value has not been set or doesn't exist
Controller::setGlobal("offsetTitle", 0);
$offset = Controller::getGlobal ("offsetTitle");
}
$targetRestorationUsers = Yii::app()->params['users'];
$users = array_slice($targetRestorationUsers, $offset, $batchSize);
if (empty($users))
{
Controller::setGlobal("offsetTitle", "finished");
echo "Finished";
yii::app()->end();
}
echo "Traversing from {$offset} to {$batchSize}" . "<br/>";
foreach ($users as $user)
{
echo $user["first_name"] . "<br/>";
}
$offset += $batchSize;
Controller::setGlobal("offsetTitle", $offset);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment