Reads the complete meta-data for certain model

@param string|\ManaPHP\Db\ModelInterface $model

@return array
16189
protected function _readMetaData($model)
    {
        $modelName = is_string($model) ? $model : get_class($model);

        if (!isset($this->_metadata[$modelName])) {
            $data = $this->read($modelName);
            if ($data !== false) {
                $this->_metadata[$modelName] = $data;
            } else {
                $modelInstance = is_string($model) ? new $model : $model;

                $data = $this->_di->getShared($modelInstance->getDb(true))->getMetadata($modelInstance->getSource(true));

                $this->_metadata[$modelName] = $data;
                $this->write($modelName, $data);
            }
        }

        return $this->_metadata[$modelName];
    }
1121221
protected function findMetaHeaders(ResponseInterface $response)
    {
        $headerNames = array_keys($response->getHeaders());
        $metaType = $this->objectMetaType();

        return array_filter($headerNames, function ($header) use ($metaType) {
            return strpos($header, 'X-'.$metaType.'-Meta') !== false;
        });
    }
1325249
protected function storeVersion(ModelInterface $model)
    {
        $modelId                 = $model->getId();
        $environment             = $this->getEnvironment();
        $definition              = $this->getDataDefinition();
        $dataProvider            = $environment->getDataProvider($model->getProviderName());
        $dataProviderDefinition  = $definition->getDataProviderDefinition();
        $dataProviderInformation = $dataProviderDefinition->getInformation($model->getProviderName());

        if (!$dataProviderInformation->isVersioningEnabled()) {
            return;
        }

        // Compare version and current record.
        $currentVersion = $dataProvider->getActiveVersion($modelId);
        if (!$currentVersion
            || !$dataProvider->sameModels($model, $dataProvider->getVersion($modelId, $currentVersion))
        ) {
            $user = BackendUser::getInstance();

            $dataProvider->saveVersion($model, $user->username);
        }
    }
934157
public static function getLookup(RepositoryInterface $table) : array
    {
        $moduleName = App::shortName(get_class($table), 'Model/Table', 'Table');

        $config = new ModuleConfig(ConfigType::MODULE(), $moduleName);
        $parsed = $config->parse();

        if (! property_exists($parsed, 'table')) {
            return [];
        }

        if (! property_exists($parsed->table, 'lookup_fields')) {
            return [];
        }

        return $parsed->table->lookup_fields;
    }
848929
protected function getModelPermissions($model)
    {
        $tree = $this->treeBuilder->getTree();
        $psrClass = '';
        if (is_string($model)) {
            $psrClass = $model;
        } elseif (is_object($model)) {
            $psrClass = get_class($model);
        }

        if (!empty($tree['models']) && array_key_exists($psrClass, $tree['models'])) {
            return $tree['models'][$psrClass];
        }

        return [];
    }
582715
public function getRelationships($model): iterable
    {
        assert($model instanceof ModelInterface);

        foreach ($this->getRelationshipsMapping() as $jsonRelName => [$modelRelName, $belongsToFkName, $reverseType]) {
            // if model has relationship data then use it
            if ($this->hasProperty($model, $modelRelName) === true) {
                yield $jsonRelName => $this->createRelationshipRepresentationFromData(
                    $model,
                    $modelRelName,
                    $jsonRelName
                );
                continue;
            }

            // if relationship is `belongs-to` and has that ID we can add relationship as identifier
            if ($belongsToFkName !== null && $this->hasProperty($model, $belongsToFkName) === true) {
                $reverseIndex = $model->{$belongsToFkName};
                $identifier   = $reverseIndex === null ?
                    null : new Identifier((string)$reverseIndex, $reverseType, false, null);

                yield $jsonRelName => [
                    static::RELATIONSHIP_DATA       => $identifier,
                    static::RELATIONSHIP_LINKS_SELF => $this->isAddSelfLinkInRelationshipWithData($jsonRelName),
                ];
                continue;
            }

            // if we are here it's nothing left but show relationship as a link
            yield $jsonRelName => [static::RELATIONSHIP_LINKS_SELF => true];
        }
    }
1005617
public function getMetaData(ModelInterface $model, DiInterface $di)
    {
        /** @var Reflection $reflection */
        $reflection = $di->getAnnotations()->get($model);
        $properties = $reflection->getPropertiesAnnotations();
        if (!$properties) {
            throw new \Exception('There are no properties defined on the class');
        }

        $tableProperties = $reflection->getClassAnnotations();

        if ($tableProperties && $tableProperties->has('Source')) {
            $source = $tableProperties->get('Source')->getArguments()[0];
        } else {
            throw new \Exception('Model ' . get_class($model) . ' has no Source defined');
        }

        /** @var \Phalcon\Db\Adapter $database */
        $database = $di->get($source);
        $dbType = ucfirst($database->getType());
        $indexes = $this->getIndexes($reflection);

        return call_user_func_array(['\Phalcon\Annotations\DbStrategies\\'.$dbType, 'getMetaData'], [$reflection, $properties, $indexes]);
    }
346675
private function getChildIds(ModelInterface $model)
    {
        if (null === ($childCollections = $model->getMeta($model::CHILD_COLLECTIONS))) {
            return [];
        }

        $ids = [ModelId::fromModel($model)->getSerialized()];

        $childIds = [];
        foreach ($childCollections as $collection) {
            foreach ($collection as $child) {
                $childIds[] = $this->getChildIds($child);
            }
        }

        return \array_merge($ids, ...$childIds);
    }
203622
protected function showDatabaseFields(Model $model)
    {
        $this->title("Table {$model->getTable()} :");

        if ( ! $this->db->isConnected()) {
            $this->warn("Not Connected to databse, please check your connection config\r");
        } else {
            $fields = $this->db->getFields($model->getTable());
            $headers = ['name', 'type', 'null', 'length', 'unsigned', 'autoincrement', 'primary_key', 'foreign_key'];

            $this->table($headers, $fields->toArray());
        }
    }
107959
protected function _generateTableColumns($model)
    {
        $model = TableRegistry::get($model);
        $columns = ConnectionManager::get('default')->schemaCollection()->describe($model->table())->columns();

        $result = [];
        $counter = 0;
        $max = 2;

        if (in_array('id', $columns)) {
            $result['id'] = [];
            unset($columns['id']);
        }

        foreach ($columns as $column) {
            if ($counter < $max) {
                $result[$column] = [];
                unset($columns[$column]);
                $counter++;
            }
        }

        if (in_array('created', $columns)) {
            $result['created'] = [];
        }

        return $result;
    }
716572
public function getReadConnection(\Phalcon\Mvc\ModelInterface $model)
    {
        $di = $this->getDI();
        if ((true === $di->get('config')->get('db.use_slave')) && $di->has('dbRead')) {
            return $di->getShared('dbRead');
        }

        return parent::getReadConnection($model);
    }
357056
public function createModelData(Model $model)
    {
        $data = [];

        if ($model->getId()) {
            $data['id'] = $model->getId();
        }
        if ($model->getName()) {
            $data['name'] = $model->getName();
        }
        if ($model->getConcepts()) {
            $data['output_info']['data'] = [];
            $data['output_info']['data'] = $this->addModelConcepts($data['output_info']['data'], $model->getConcepts());
        }
        $data['output_info']['output_config'] = $model->getOutputConfig();

        return $data;
    }
477892
public function retrieve(Model $model, $source)
    {
        if ($this->isTranslated()) {
            return $model->translations->mapWithKeys(function (Model $model) {
                /** @var Taggable $model */
                return [ $model->{config('translatable.locale_key', 'locale')} => $model->tagNames() ];
            });
        }

        return $model->tagNames();
    }
919139
protected function getRelatedModels(Model $model)
    {
        // Store the class name
        $modelClass = get_class($model);

        // Check if we already know the related models for this model
        if (!isset($this->relatedModels[$modelClass]))
        {
            $relatedModels = [];

            $methods = with(new \ReflectionClass($model))->getMethods();

            // Iterate all class methods
            foreach ($methods as $method)
            {
                // Check if this method returns an Eloquent relation
                if ($method->class == $modelClass &&
                    preg_match('/@return\s+\\\\Illuminate\\\\Database\\\\Eloquent\\\\Relations/', $method->getDocComment())
                )
                {
                    // Get the method name, so that we can call it on the model
                    $relationMethod = $method->name;

                    // Find the relation
                    $relation = $model->$relationMethod();

                    if ($relation instanceof Relation)
                    {
                        // Find the related model
                        $related = $relation->getRelated();

                        // Store the method to help build the inverse path
                        $this->relationClassMethods[$modelClass][get_class($related)] = $relationMethod;

                        $relatedModels[] = ['model' => $related, 'method' => $method];
                    }
                }
            }

            // Cache related models for this model
            $this->relatedModels[$modelClass] = $relatedModels;

            // Return the related models
            return $relatedModels;
        } else
        {
            // Return from cache
            return $this->relatedModels[$modelClass];
        }
    }
291346
protected function scopedAttributes( $model )
	{
		$keys = $this->model->getScopedColumns();

		if ( count( $keys ) == 0 )
			return [];

		$values = array_map( function ( $column ) use ( $model )
		{
			return $model->getAttribute( $column );
		}, $keys );

		return array_combine( $keys, $values );
	}
1323938
protected function serializeModel($model)
    {
        if ($this->request->getIsHead()) {
            return null;
        }

        [$fields, $expand] = $this->getRequestedFields();
        return $model->toArray($fields, $expand);
    }
722814
public function readCache(Model $model, $keys) {
        $key = $this->cacheKey($model, $keys);

        if (isset($this->_cached[$key])) {
            $results = $this->_cached[$key];
        } else {
            $this->_cached[$key] = $results = Cache::read($key, $this->settings[$model->alias]['cacheConfig']);
        }

        return $results;
    }