entityManager = $entityManager; } protected function getQueryComposer() : QueryComposer { return $this->entityManager->getQueryComposer(); } protected function getEntityFactory() : EntityFactory { return $this->entityManager->getEntityFactory(); } protected function setSql(string $sql) : void { $this->sql = $sql; } protected function getPDO() : PDO { return $this->entityManager->getPDO(); } protected function executeQuery() : void { $sql = $this->getSql(); $sth = $this->getPDO()->prepare($sql); $sth->execute(); $this->sth = $sth; } protected function getSql() : string { if (!$this->sql) { $this->sql = $this->getQueryComposer()->compose($this->getQuery()); } return $this->sql; } protected function getQuery() : SelectQuery { return $this->query; } 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); $this->entityList[] = $entity; yield $entity; } })(); } protected function executeQueryIfNotExecuted() : void { if (!$this->sth) { $this->executeQuery(); } } protected function fetchRow() { $this->executeQueryIfNotExecuted(); return $this->sth->fetch(PDO::FETCH_ASSOC); } public function count() : int { $this->executeQueryIfNotExecuted(); $rowCount = $this->sth->rowCount(); // MySQL may not return a row count for select queries. if ($rowCount) { return $rowCount; } return count($this->getValueMapList()); } protected function prepareEntity(Entity $entity) : void { } /** * @deprecated */ public function toArray(bool $itemsAsObjects = false) : array { $arr = []; foreach ($this as $entity) { if ($itemsAsObjects) { $item = $entity->getValueMap(); } else { $item = $entity->toArray(); } $arr[] = $item; } return $arr; } public function getValueMapList() : array { return $this->toArray(true); } /** * Whether Is fetched from DB. SthCollection is always fetched. */ public function isFetched() : bool { return true; } /** * Get an entity type. */ public function getEntityType() : string { return $this->entityType; } public static function fromQuery(SelectQuery $query, EntityManager $entityManager) : self { $obj = new self($entityManager); $obj->entityType = $query->getFrom(); $obj->query = $query; return $obj; } public static function fromSql(string $entityType, string $sql, EntityManager $entityManager) : self { $obj = new self($entityManager); $obj->entityType = $entityType; $obj->sql = $sql; return $obj; } }