Recursively determine all child ids of the passed model.

@param ModelInterface $model The model to fetch the ids from.

@return string[]
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);
    }
864220
protected static function convertModelToFormRepresentation(
        ContainerInterface $container,
        ModelInterface $model
    ): ?array {
        /** @var JsonSchemasInterface $schemas */
        $schemas = $container->get(JsonSchemasInterface::class);
        /** @var SchemaInterface $schema */
        $schema = $schemas->getSchema($model);
        assert($schema instanceof SchemaInterface);

        $mappings = $schema::getMappings();

        $formData = [];
        if (array_key_exists(SchemaInterface::SCHEMA_ATTRIBUTES, $mappings) === true) {
            foreach ($mappings[SchemaInterface::SCHEMA_ATTRIBUTES] as $jsonAttrName => $modelAttrName) {
                $formData[$jsonAttrName] = $model->{$modelAttrName} ?? null;
            }
        }
        if (array_key_exists(SchemaInterface::SCHEMA_RELATIONSHIPS, $mappings) === true) {
            /** @var ModelSchemaInfoInterface $modelSchemeInfo */
            $modelSchemeInfo = $container->get(ModelSchemaInfoInterface::class);
            $modelClass      = get_class($model);
            foreach ($mappings[SchemaInterface::SCHEMA_RELATIONSHIPS] as $jsonRelName => $modelRelName) {
                $relationshipType = $modelSchemeInfo->getRelationshipType($modelClass, $modelRelName);
                if ($relationshipType === RelationshipTypes::BELONGS_TO) {
                    $fkName                 = $modelSchemeInfo->getForeignKey($modelClass, $modelRelName);
                    $formData[$jsonRelName] = $model->{$fkName} ?? null;
                }
            }
        }

        return $formData;
    }
957595
public static function compositeIDs($models)
    {
        if (!is_array($models) && $models instanceof static) {
            return $models->getID();
        }
        $ids = [];
        foreach ($models as $model) {
            if ($model instanceof static) {
                $ids[] = $model->getID();
            }
        }
        return $ids;
    }
1346725
protected function getNestedFormFieldKeys(ModelFormLayoutNodeInterface $node = null): array
    {
        if (null === $node) {
            $children = $this->layout();
        } else {
            $children = $node->children();
        }

        $fieldKeys = [];

        foreach ($children as $key => $value) {

            if ($value instanceof ModelFormLayoutNodeInterface) {
                $fieldKeys = array_merge($fieldKeys, $this->getNestedFormFieldKeys($value));
                continue;
            }

            if (is_string($value)) {
                $fieldKeys[] = $value;
            }
        }

        return $fieldKeys;
    }
860460
protected function _getOptionsFromModel($model)
    {
        $model = TableRegistry::get($model);

        if (method_exists($model, 'postType')) {
            $result = $model->postType();
            if ($result) {
                $result['table'] = $model->table();
            }
            return $result;
        }
        if (property_exists($model, 'postType')) {
            $result = $model->postType;
            if ($result) {
                $result['table'] = $model->table();
            }
            return $result;
        }
        return [];
    }
651622
public function dropTable(ModelInterface $model)
    {
        $i = 0;

        $model->setConnection($this->connection);

        $builder = $this->getQueryBuilder();

//        $this->connection->executeUpdate($adapter->sqlCheckIntegrity(false, 'public', $model->tableName()));

        $schemaManager = $this->connection->getSchemaManager();
        foreach ($model->getMeta()->getManyToManyFields() as $field) {
            if (null === $field->through) {
                $fieldTable = TableNameResolver::getTableName($field->getTableName());
                if ($this->hasTable($fieldTable)) {
                    $schemaManager->dropTable($builder->getQuotedName($fieldTable));
                    $i += 1;
                }
            }
        }

        if ($this->hasTable($model->tableName())) {
            $schemaManager->dropTable($model->tableName());
            $i += 1;
        }

//        $this->connection->executeUpdate($adapter->sqlCheckIntegrity(true, 'public', $model->tableName()));

        return $i;
    }
663574
public function createTable(ModelInterface $model)
    {
        $i = 0;

        $model->setConnection($this->connection);

        $schemaManager = $this->connection->getSchemaManager();
        $builder = $this->getQueryBuilder();
        $tableName = TableNameResolver::getTableName($model->tableName());

        $columns = [];
        $indexes = [];

        foreach ($model->getMeta()->getFields() as $name => $field) {
            $field->setModel($model);

            if ($field instanceof ManyToManyField) {
                $field->setConnection($this->connection);

                /* @var $field \Mindy\Orm\Fields\ManyToManyField */
                if (null === $field->through) {
                    $fieldTableName = TableNameResolver::getTableName($field->getTableName());
                    if (false === $this->hasTable($fieldTableName)) {
                        $fieldTable = new Table(
                            $fieldTableName,
                            $field->getColumns()
                        );
                        $schemaManager->createTable($fieldTable);
                        $i += 1;
                    }
                }
            } else {
                $column = $field->getColumn();
                if (empty($column)) {
                    continue;
                }

                $columns[] = $column;
                $indexes = array_merge($indexes, $field->getSqlIndexes());
            }
        }

        if (false === $this->hasTable($tableName)) {
            $table = new Table($tableName, $columns, []);
            $table->setPrimaryKey($model->getPrimaryKeyName(true), 'primary');
            $schemaManager->createTable($table);

            $i += 1;
        }

        return $i;
    }
1081153
protected function renderModel(ModelInterface $model, EnvironmentInterface $environment)
    {
        $event = new ParentViewChildRecordEvent($environment, $model);
        $environment->getEventDispatcher()->dispatch($event::NAME, $event);

        if (null !== $event->getHtml()) {
            $information = [
                [
                    'colspan' => 1,
                    'class'   => 'tl_file_list col_1',
                    'content' => $event->getHtml()
                ]
            ];
            $model->setMeta($model::LABEL_VALUE, $information);
            return;
        }

        parent::renderModel($model, $environment);
    }
109113
public function findPalette(ModelInterface $model = null, PropertyValueBag $input = null)
    {
        $matches = [];

        // Determinate the matching count for each palette.
        foreach ($this->palettes as $palette) {
            $condition = $palette->getCondition();

            if ($condition) {
                $count = $condition->getMatchCount($model, $input);

                if (false !== $count) {
                    $matches[$count][] = $palette;
                }
            }
        }

        // Sort by count.
        \ksort($matches);

        // Get palettes with highest matching count.
        $palettes = \array_pop($matches);

        if (1 !== \count($palettes)) {
            throw new DcGeneralInvalidArgumentException(\sprintf('%d matching palettes found.', \count($palettes)));
        }

        return $palettes[0];
    }
938315
protected function properties(Model $model)
    {
        // Process property annotations
        $annotations = '';

        foreach ($model->getProperties() as $name => $hint) {
            $annotations .= $this->class->annotation('property', "$hint \$$name");
        }

        if ($model->hasRelations()) {
            // Add separation between model properties and model relations
            $annotations .= "\n * ";
        }

        foreach ($model->getRelations() as $name => $relation) {
            // TODO: Handle collisions, perhaps rename the relation.
            if ($model->hasProperty($name)) {
                continue;
            }
            $annotations .= $this->class->annotation('property', $relation->hint()." \$$name");
        }

        return $annotations;
    }
419627
protected function _includeResource(ModelEntityInterface $entity)
    {
        $cache  = $entity->getIdentifier()->name.'-'.$entity->getHandle();

        if (!isset($this->_included_resources[$cache])) {
            $this->_included_resources[$cache] = $this->_createResource($entity, array('relationships' => false));
        }

        $resource = $this->_included_resources[$cache];

        return array(
            'data' => array(
                'type' => $resource['type'],
                'id'   => $resource['id']
            )
        );
    }
615914
public function retrieve(Model $model, $source)
    {
        return [
            'from' => parent::retrieve($model, $this->getAttributeFrom()),
            'to'   => parent::retrieve($model, $this->getAttributeTo()),
        ];
    }
750110
public static function findChildren($model, $parentId) {
        return $children = $model::find()->where(['parent_id' => $parentId])->orderBy('position')->all();
    }
582157
protected function getCaptureTo(ModelInterface $block)
    {
        $captureTo = $block->captureTo();
        if ($parent = $block->getOption('parent')) {
            $captureTo = explode(self::CAPTURE_TO_DELIMITER, $captureTo);
            return [
                $parent,
                end($captureTo)
            ];
        }
        if (false !== strpos($captureTo, self::CAPTURE_TO_DELIMITER)) {
            return explode(self::CAPTURE_TO_DELIMITER, $captureTo);
        }
        return [
            self::BLOCK_ID_ROOT,
            $captureTo
        ];
    }
1016022
private function getId($model)
    {
        // Check to see if we can get an id via our defined methods
        foreach (self::$methods as $method) {
            if (method_exists($model, $method) && is_callable([$model, $method])) {
                return $model->$method();
            }
        }

        // Check to see if we can get an id via our defined properties
        foreach (self::$properties as $property) {
            if (isset($model->$property)) {
                return $model->$property;
            }
        }
    }
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]);
    }
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;
    }