Spot\Mapper::collection PHP Method

collection() public method

Create collection from Spot\Query object
public collection ( $cursor, $with = [] )
    public function collection($cursor, $with = [])
    {
        $entityName = $this->entity();
        $results = [];
        $resultsIdentities = [];
        // Ensure PDO only gives key => value pairs, not index-based fields as well
        // Raw PDOStatement objects generally only come from running raw SQL queries or other custom stuff
        if ($cursor instanceof \PDOStatement) {
            $cursor->setFetchMode(\PDO::FETCH_ASSOC);
        }
        // Fetch all results into new entity class
        // @todo Move this to collection class so entities will be lazy-loaded by Collection iteration
        foreach ($cursor as $data) {
            // Do type conversion
            $data = $this->convertToPHPValues($entityName, $data);
            $entity = new $entityName($data);
            $entity->isNew(false);
            $this->prepareEntity($entity);
            // Store in array for Collection
            $results[] = $entity;
            // Store primary key of each unique record in set
            $pk = $this->primaryKey($entity);
            if (!in_array($pk, $resultsIdentities) && !empty($pk)) {
                $resultsIdentities[] = $pk;
            }
        }
        $collectionClass = $this->collectionClass();
        $collection = new $collectionClass($results, $resultsIdentities, $entityName);
        if (empty($with) || count($collection) === 0) {
            return $collection;
        }
        return $this->with($collection, $entityName, $with);
    }