Read model(s)

@param string $model  Model
@param array  $ids    Array of model id's
@param array  $fields Index array of fields to fetch, an empty array fetches all fields

@return array An array of models
483177
public function read($model, $ids, $fields = array())
	{
		$params = $this->buildParams(array(
			$model,
			'read',
			$ids,
			$fields
		));

		$response = $this->getClient('object')->call('execute', $params);

		return $response;
	}
611940
public function getReadRequest($ids)
    {
        $cacheDataAdapter = $this->cacheDataAdapter; 
        $primaryKey = $this->primaryKey;
        return new StorageDataRequest(
            [$this->sourceDataAdapter, $this->cacheDataAdapter, $ids],
            function (DataAdapterInterface $sourceData, DataAdapterInterface $cacheData, $ids) {
                $request = $cacheData->getReadRequest($ids);
                $request->send();
                
                $cacheReadResult = $request->fetch();
                
                $cacheReadSuccess = count($cacheReadResult) == count($ids);
                if (!$cacheReadSuccess) {
                    $request = $sourceData->getReadRequest($ids);
                    $request->send();
                }
            
                return [$request, $cacheReadSuccess];
            },
            function (StorageDataRequest $request, $cacheReadSuccess) use ($cacheDataAdapter, $primaryKey) {
                $result = $request->fetch();
                if ($result && !$cacheReadSuccess) {
                    $cacheDataAdapter->getBatchInsertRequest($result)->send();
                }
                
                return $result;
            }
        );
    }
609169
public function readCache(Model $model, $keys)
    {
        return Cache::read($this->cacheKey($model, $keys), $this->settings[$model->alias]['cacheConfig']);
    }
94611
public static function getOne($id, $fields = null)
    {
        $table = self::getTableNameFromClass();

        if ($fields != null && is_array($fields)) {
            $query = "SELECT ". implode(', ', $fields) ." FROM $table WHERE id = $id";
        } else {
            $query = "SELECT * FROM $table WHERE id = $id";
        }

        $res = [];

        $results = Connection::db()->query($query);
        $results->setFetchMode(PDO::FETCH_ASSOC);

        while ($row = $results->fetch()) {
            array_push($res, $row);
        }

        if (!empty($res)) {
            return $res;
        } else {
            throw new PotatoException('There is no user with id '. $id);
        }
    }
666773
public function map($results, $model)
    {
        if (count($results['ids']) === 0) {
            return Collection::make();
        }

        $keys = collect($results['ids'])->values()->all();
        $fieldsWheres = array_keys($this->builder->wheres);
        $models = $model->whereIn(
            $model->getQualifiedKeyName(),
            $keys
        )->get()->keyBy($model->getKeyName());

        return collect($results['ids'])->map(function ($hit) use ($models) {
            return $models->has($hit) ? $models[$hit] : null;
        })->filter(function ($model) use ($fieldsWheres) {
            return ! is_null($model) && array_reduce($fieldsWheres, function ($carry, $item) use ($model) {
                return $carry && $model[$item] == $this->builder->wheres[$item];
            }, true);
            ;
        });
    }
198403
public function modelsFromIds($ids)
    {
        // We need a Model in order to save this relationship so we try
        // to whereIn the given id(s) through the related model.
        return $this->related->whereIn($this->related->getKeyName(), (array) $ids)->get();
    }
391006
public function getReadRequest($ids)
    {
        $self = $this;
        $request = new StorageDataRequest(
            [$ids],
            function ($ids) use ($self) {
                $results = [];
                foreach ($ids as $id) {
                    $pointer = $self->getPointer($id);
                    if ($pointer) {
                        $record = json_decode(stream_get_contents($pointer), true);
                        if (isset($record[self::F_DATA])) {
                            $results[$id] = $record[self::F_DATA] + [$this->primaryKey => $id];
                        }
                        $self->closePointer($pointer);
                    } else {
                        $results[$id] = null;
                    }
                }
                
                return $results;
            }
        );
    
        return $request;
    }
702424
protected function getQueryResults(array $models) {
        if (empty($this->relations)) {
            return static::getRelationQuery($models)->all();
        }
        $data = static::getRelationQuery($models)->appendSelect($this->getRelationFields())->all();
        if (empty($data)) {
            return $data;
        }
        foreach ($this->relations as $relation) {
            if (empty($relation->getKey())) {
                return $relation->getResults($data);
            }
            $data = $relation->getResults($data);
        }
        return $data;
    }
1140309
public function setModel($model, $ids = [])
    {
        $this->model = $model;
        $this->ids   = array_wrap($ids);

        $this->message = "No query results for model [{$model}]";

        if (count($this->ids) > 0) {
            $this->message .= ' ' . implode(', ', $this->ids);
        } else {
            $this->message .= '.';
        }

        return $this;
    }
553255
public function unlink($model, $ids)
	{
		$params = $this->buildParams(array(
			$model,
			'unlink',
			$ids
		));

		return $this->getClient('object')->call('execute', $params);
	}
402053
public function findById(array $ids)
    {
        $ids = $this->idsToMongo($ids);

        $documents = array();
        $remaining = array();
        foreach ($ids as $id) {
            if ($this->identityMap->has($id)) {
                $documents[(string) $id] = $this->identityMap->get($id);
            } else {
                $remaining[] = $id;
            }
        }

        if (count($documents) == count($ids)) {
            return $documents;
        }

        return array_merge(
            $documents,
            $this->createQuery(array('_id' => array('$in' => $remaining)))->all()
        );
    }
428956
public function findManyByIds(array $ids)
    {
        $models = $this->model->whereIn($this->model->getKeyName(), array_unique($ids))
            ->get();

        return $models;
    }
236863
public function load($ids)
    {
        settype($ids, 'array');

        $types = $this->getTypes();
        $libraries = $this->getList();

        foreach ($ids as $key => $id) {

            if ($this->isLoaded($id)) {
                unset($ids[$key]);
                continue;
            }

            if (empty($libraries[$id]['type'])) {
                unset($ids[$key]);
                continue;
            }

            if (empty($types[$libraries[$id]['type']]['load'])) {
                unset($ids[$key]);
            }
        }

        $sorted = $this->graph->sort($ids, $libraries);

        if (empty($sorted)) {
            return false;
        }

        foreach ($this->prepareFiles($sorted, $libraries) as $file) {
            require_once $file;
        }

        $this->loaded = array_merge($this->loaded, $sorted);
        return true;
    }
873396
private function addModelToArraySimpleMode($models, $model)
    {
        $apply_to_models = array();
        
        if (count($models) > 0) {
            foreach ($models as $item) {
                $object = $model->find($item);
                if ($object != null) {
                    $apply_to_models[] = $object;
                }
            }
        }
        
        return $apply_to_models;
    }
779156
public function getResults($models) {
        $is_one = !is_array($models) || !is_array(reset($models));
        if ($is_one) {
            $models = [$models];
        }
        $results = $this->getQueryResults($models);
        if (empty($results) || !$is_one) {
            return $results;
        }
        return $this->type == self::TYPE_ONE ? reset($results) : $results;
    }
1153271
public static function get_many(array $ids) {
        global $DB;
        $coursecatrecordcache = cache::make('core', 'coursecatrecords');
        $categories = $coursecatrecordcache->get_many($ids);
        $toload = array();
        foreach ($categories as $id => $result) {
            if ($result === false) {
                $toload[] = $id;
            }
        }
        if (!empty($toload)) {
            list($where, $params) = $DB->get_in_or_equal($toload, SQL_PARAMS_NAMED);
            $records = self::get_records('cc.id '.$where, $params);
            $toset = array();
            foreach ($records as $record) {
                $categories[$record->id] = new self($record);
                $toset[$record->id] = $categories[$record->id];
            }
            $coursecatrecordcache->set_many($toset);
        }
        return $categories;
    }
1304293
public function importFields($model, $fields = null)
    {
        $this->model = $model;

        if ($fields === false) {
            return;
        }

        if (!$fields) {
            if ($model->only_fields) {
                $fields = $model->only_fields;
            } else {
                $fields = [];
                // get all field-elements
                foreach ($model->elements as $field => $f_object) {
                    if ($f_object instanceof \atk4\data\Field) {
                        $fields[] = $f_object;
                    }
                }
            }
        }

        if (!is_array($fields)) {
            $fields = [$fields];
        }

        // import fields one by one
        foreach ($fields as $field) {
            $this->importField($field);
        }

        return $this;
    }