3 namespace DataAccess\DataMapper
;
5 use DataAccess\DataMapper\Helpers\AbstractPopulationHelper
;
9 class LazyLoadedEntities
implements Iterator
14 private $_rowIndex = 0;
15 private $_loadedEntitiesIndex = 0;
16 private $_loadedEntities = array();
19 public function __construct(array $rows, $entityName, $maps, $db)
22 $this->_entityName
= $entityName;
27 public function current()
29 return $this->_loadedEntities
[$this->_loadedEntitiesIndex
];
34 return $this->_loadedEntitiesIndex
;
37 public function next()
39 $keys = array_keys($this->_loadedEntities
);
40 $pos = array_search($this->_loadedEntitiesIndex
, $keys);
41 if(isset($keys[$pos +
1]))
43 $this->_loadedEntitiesIndex
= $keys[$pos+
1];
45 $this->mapEntities(); //sets the loaded entites index
49 public function rewind()
55 public function valid()
57 //next will always load more entities when it runs out, therefore if
58 //we have an empty loadEntities array, it means there were no more and we are done.
59 if($this->_loadedEntities
)
67 //unsets the current entities array, and maps in the next 10
68 private function mapEntities()
73 unset($this->_loadedEentities
);
74 $this->_loadedEntities
= array();
76 $entityName = $this->_entityName
;
77 for($i = $this->_rowIndex
; $i<$this->_rowIndex+
$numToMap; $i++
)
79 if(isset($this->_rows
[$i]))
81 $row = $this->_rows
[$i];
82 $className = $this->_maps
[$entityName]['class']; //the entity to instantiate and return
83 $constructors = AbstractPopulationHelper
::getConstrutorArray($this->_maps
, $entityName, $row, $this->_db
);
85 if(count($constructors) == 0)
87 $class = new $className;
89 $r = new ReflectionClass($className);
90 $class = $r->newInstanceArgs($constructors);
93 $class->setId($row['id']);
94 $this->_loadedEntities
[$row['id']] = $class;
96 if($tick == 0) $this->_loadedEntitiesIndex
= $row['id'];
101 $this->_rowIndex +
= $numToMap;