selectParams = $selectParams; $this->entityType = $entityType; $this->entityManager = $entityManager; } protected function getQuery() { return $this->entityManager->getQuery(); } protected function getPdo() { return $this->entityManager->getPdo(); } protected function getEntityFactory() { return $this->entityManager->getEntityFactory(); } public function setSelectParams(array $selectParams) { $this->selectParams = $selectParams; } public function setQuery(?string $sql) { $this->sql = $sql; } public function executeQuery() { if ($this->sql) { $sql = $this->sql; } else { $sql = $this->getQuery()->createSelectQuery($this->entityType, $this->selectParams); } $sth = $this->getPdo()->prepare($sql); $sth->execute(); $this->sth = $sth; } public function getIterator() { return (function () { if (isset($this->sth)) { $this->sth->execute(); } while ($row = $this->fetchRow()) { $entity = $this->getEntityFactory()->create($this->entityType); $entity->set($row); $entity->setAsFetched(); $this->prepareEntity($entity); yield $entity; } })(); } protected function fetchRow() { if (!$this->sth) { $this->executeQuery(); } return $this->sth->fetch(\PDO::FETCH_ASSOC); } protected function prepareEntity(Entity $entity) { } public function toArray($itemsAsObjects = false) { $arr = []; foreach ($this as $entity) { if ($itemsAsObjects) { $item = $entity->getValueMap(); } else { $item = $entity->toArray(); } $arr[] = $item; } return $arr; } public function getValueMapList() { return $this->toArray(true); } public function setAsFetched() { $this->isFetched = true; } public function setAsNotFetched() { $this->isFetched = false; } public function isFetched() { return $this->isFetched; } public function getEntityType() { return $this->entityType; } }