[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Iterate over every object of a given type, without holding all of them in 5 * memory. This is useful for performing database migrations. 6 * 7 * $things = new LiskMigrationIterator(new LiskThing()); 8 * foreach ($things as $thing) { 9 * // do something 10 * } 11 * 12 * NOTE: This only works on objects with a normal `id` column. 13 * 14 * @task storage 15 */ 16 final class LiskMigrationIterator extends PhutilBufferedIterator { 17 18 private $object; 19 private $cursor; 20 private $set; 21 22 public function __construct(LiskDAO $object) { 23 $this->set = new LiskDAOSet(); 24 $this->object = $object->putInSet($this->set); 25 } 26 27 protected function didRewind() { 28 $this->cursor = 0; 29 } 30 31 public function key() { 32 return $this->current()->getID(); 33 } 34 35 protected function loadPage() { 36 $this->set->clearSet(); 37 38 $results = $this->object->loadAllWhere( 39 'id > %d ORDER BY id ASC LIMIT %d', 40 $this->cursor, 41 $this->getPageSize()); 42 if ($results) { 43 $this->cursor = last($results)->getID(); 44 } 45 return $results; 46 } 47 48 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |