Search the parent of a model in hierarchical mode.

@param ModelInterface $model The model to search the parent of.

@return ModelInterface|null

@throws DcGeneralInvalidArgumentException When a root model has been passed.
14676
private function searchParentOfInHierarchical(ModelInterface $model)
    {
        if ($this->isRootModel($model)) {
            throw new DcGeneralInvalidArgumentException('Invalid condition, root models can not have parents!');
        }
        // Start from the root data provider and walk through the whole tree.
        // To speed up, some conditions have an inverse filter - we should use them!
        $config = $this->rootProvider->getEmptyConfig()->setFilter($this->rootCondition->getFilterArray());

        return $this->searchParentOfIn($model, $this->rootProvider->fetchAll($config));
    }
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);
    }
1003968
public function children( $model )
	{
		$query = $this->model->newQuery();

		$query->where( $this->model->getQualifiedParentColumnName(), '=', $model->getKey() );

		// We must also add the scoped column values to the query to compute valid
		// left and right indexes.
		foreach ( $this->scopedAttributes( $model ) as $fld => $value )
			$query->where( $this->qualify( $fld ), '=', $value );

		$query->orderBy( $this->model->getQualifiedLeftColumnName() );
		$query->orderBy( $this->model->getQualifiedRightColumnName() );
		$query->orderBy( $this->model->getQualifiedKeyName() );

		return $query->get();
	}
997232
private function generateToggleUrl(ModelInterface $model)
    {
        $toggleUrlEvent = new AddToUrlEvent(
            'ptg=' . $model->getId() . '&amp;provider=' . $model->getProviderName()
        );
        $this->getEnvironment()->getEventDispatcher()->dispatch(ContaoEvents::BACKEND_ADD_TO_URL, $toggleUrlEvent);

        return System::getContainer()->get('router')->generate(
            'cca_dc_general_tree_toggle',
            $this->getQueryParameterFromUrl($toggleUrlEvent->getUrl()),
            UrlGenerator::ABSOLUTE_URL
        );
    }
365983
protected function fetchDefinition(ModelInterface $model, $relation)
    {
        if ($model->hasRelation($relation)) {
            return $model->relation($relation);
        }

        throw new RelationException(sprintf('Unable to resolve relation "%s" not found in model "%s"', $relation, $model->entity()));
    }
372940
public function findModelInstanceByIdOrAlias(string $table, $idOrAlias, array $options = [])
    {
        if (!($modelClass = $this->framework->getAdapter(Model::class)->getClassFromTable($table))) {
            return null;
        }

        /* @var Model $adapter */
        if (null === ($adapter = $this->framework->getAdapter($modelClass))) {
            return null;
        }

        if ($this->container->get('huh.utils.dca')->isDcMultilingual($table)) {
            $table = 't1';
        }

        $options = array_merge(
            [
                'limit' => 1,
                'column' => !is_numeric($idOrAlias) ? ["$table.alias=?"] : ["$table.id=?"],
                'value' => $idOrAlias,
                'return' => 'Model',
            ],
            $options
        );

        return $adapter->findByIdOrAlias($idOrAlias, $options);
    }
483149
protected function _changeParent(Model $model, $id = null, $parentId = null) {
		$parentField = $this->_getBehaviorConfig($model, 'parent');
		if (empty($id) || ($parentField === null)) {
			return false;
		}

		$model->recursive = -1;
		$treeItem = $model->read(null, $id);
		if (empty($treeItem)) {
			return false;
		}

		$treeItem[$model->alias][$parentField] = $parentId;
		$result = (bool)$model->save($treeItem);

		return $result;
	}
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;
    }
2726
protected function renderModel(ModelInterface $model, EnvironmentInterface $environment)
    {
        $event = new FormatModelLabelEvent($environment, $model);
        $environment->getEventDispatcher()->dispatch(
            DcGeneralEvents::FORMAT_MODEL_LABEL,
            $event
        );

        $model->setMeta($model::LABEL_VALUE, $event->getLabel());
    }
887057
public function create(ModelInterface $model)
    {
        $user = $this->container->make(UserSingularity::class);

        $user
            ->setModel($model)
            ->boot();

        return $user;
    }
464222
public function create(ModelInterface $model)
    {
        $comment = $this->container->make(CommentSingularity::class);

        $comment
            ->setModel($model)
            ->boot();

        return $comment;
    }
252426
public function getNormalizedIdentifier($model)
    {
        $identifier = $this->baseModelManager->getNormalizedIdentifier($model);

        if ($identifier === null) {
            $identifierName = is_array($this->getModelIdentifier(get_class($model))) ? current($this->getModelIdentifier(get_class($model))) : $this->getModelIdentifier(get_class($model));

            return $model->{'get'.ucfirst($identifierName)}();
        }

        return $identifier;
    }
1075635
public function addModel(ModelInterface $model): ModelCollectionInterface
    {
        $this->propertyReflection->setValue($model, $this->foreignId);

        $this->models[$model->getId()] = $model;

        return $this;
    }
974880
protected function parseParentChildConditions(ContainerInterface $container)
    {
        if ($container->hasDefinition(ModelRelationshipDefinitionInterface::NAME)) {
            $definition = $container->getDefinition(ModelRelationshipDefinitionInterface::NAME);
        } else {
            $definition = new DefaultModelRelationshipDefinition();
        }

        // If mode is 5, we need to define tree view.
        if (5 === $this->getFromDca('list/sorting/mode')) {
            $rootProvider = $this->getRootProviderName($container);

            if (null === ($relationship = $definition->getRootCondition())) {
                $relationship = new RootCondition();
                $relationship
                    ->setSourceName($rootProvider);
                $definition->setRootCondition($relationship);

                $builder = FilterBuilder::fromArrayForRoot()->getFilter();
            } else {
                $builder = FilterBuilder::fromArrayForRoot($relationship->getFilterArray())->getFilter();
            }

            $relationship
                ->setSetters(
                    \array_merge_recursive(
                        [['property' => 'pid', 'value' => '0']]
                    ),
                    $relationship->getSetters()
                );

            $builder->andPropertyEquals('pid', '0');

            $relationship
                ->setFilterArray($builder->getAllAsArray());

            if (null === ($relationship = $definition->getChildCondition($rootProvider, $rootProvider))) {
                $relationship = new ParentChildCondition();
                $relationship
                    ->setSourceName($rootProvider)
                    ->setDestinationName($rootProvider);
                $definition->addChildCondition($relationship);

                $builder = FilterBuilder::fromArray()->getFilter();
            } else {
                $builder = FilterBuilder::fromArray($relationship->getFilterArray())->getFilter();
            }

            $relationship
                ->setSetters(
                    \array_merge_recursive(
                        [['to_field' => 'pid', 'from_field' => 'id']]
                    ),
                    $relationship->getSetters()
                );

            $builder->andRemotePropertyEquals('pid', 'id');
            $relationship
                ->setFilterArray($builder->getAllAsArray());

            $container->setDefinition(ModelRelationshipDefinitionInterface::NAME, $definition);
        }

        // If ptable defined and no root setter we need to add (Contao default id=>pid mapping).
        if (null !== $this->getFromDca('config/ptable')) {
            $rootProvider = $this->getRootProviderName($container);

            if (null === ($relationship = $definition->getRootCondition())) {
                $relationship = new RootCondition();
                $relationship
                    ->setSourceName($rootProvider);

                $definition->setRootCondition($relationship);
            }

            if (!$relationship->getSetters()) {
                $relationship
                    ->setSetters(
                        \array_merge_recursive(
                            [['property' => 'pid', 'value' => '0']]
                        ),
                        $relationship->getSetters()
                    );
            }

            $container->setDefinition(ModelRelationshipDefinitionInterface::NAME, $definition);
        }
    }
1010789
public function searchParentOfIn(ModelInterface $model, CollectionInterface $models)
    {
        // @codingStandardsIgnoreStart
        @\trigger_error(
            'Use \ContaoCommunityAlliance\DcGeneral\Controller\ModelCollector::searchParentOfIn().',
            E_USER_DEPRECATED
        );
        // @codingStandardsIgnoreEnd

        return $this->modelCollector->searchParentOfIn($model, $models);
    }
542890
public function getParentClass()
    {
        $parentClass = $this->getBehaviorContent('parentClass');
        return null === $parentClass ? ($this->getTable()->getBaseQueryClass() != "" ? $this->getTable()->getBaseQueryClass() : 'ModelCriteria') : $parentClass;
    }
934587
public function getByAncestor(AncestorCriteriaInterface $ancestor)
    {
        $this->model = $ancestor->apply($this->model, $this);
        $results = $this->model->get();
        $this->resetModel();

        return $this->parserResult($results);
    }