Apply the root condition of the current data definition to the given model.

@param ModelInterface $model The model to be used as root.

@return void

@throws DcGeneralRuntimeException When no root condition is defined.
556350
public function setRoot(ModelInterface $model)
    {
        if (BasicDefinitionInterface::MODE_HIERARCHICAL !== $this->mode) {
            return;
        }

        $condition = $this->relationships->getRootCondition();
        if (!$condition instanceof RootConditionInterface) {
            throw new DcGeneralRuntimeException('No root condition defined');
        }

        $condition->applyTo($model);
    }
26228
public function apply($builder, QueryInterface $query): void
    {
        // Apply joins
        foreach ($query->getJoins() as $join) {
            if ($join->isInner()) {
                $builder->innerJoin($join->getFromAlias(), $join->getTo(), $join->getToAlias(), $join->getCondition());
            } else {
                $builder->leftJoin($join->getFromAlias(), $join->getTo(), $join->getToAlias(), $join->getCondition());
            }
        }

        // Apply rules
        foreach ($query->getRules() as $rule) {
            $builder->andWhere($rule->getRule());
        }

        // Apply parameters
        foreach ($query->getParameters() as $parameter) {
            $builder->setParameter($parameter->getName(), $parameter->getValue(), $parameter->getType());
        }

        // Apply orders
        foreach ($query->getOrders() as $order) {
            $builder->addOrderBy($order->getField(), $order->getDirection());
        }
    }
647253
public function set(ModelInterface $model, $alias = null)
    {
        $hash = spl_object_hash($model);

        $this->collection[$hash] = &$model;

        if ($alias !== null) {
            $this->byAlias[$model->alias($alias)] = &$this->collection[$hash];
        }

        $this->byEntity[$this->normalizeNamespace($model->entity())] = &$this->collection[$hash];

        return $this;
    }
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];
    }
1197340
public function setCondition($condition)
    {
        if ((!$condition instanceof PaletteConditionInterface)
            && (!$condition instanceof PropertyConditionInterface)
        ) {
            throw new DcGeneralInvalidArgumentException(
                'The condition is invalid. ' .
                ' Only use PaletteConditionInterface or PropertyConditionInterface.'
            );
        }

        $this->condition = $condition;

        return $this;
    }
33253
public function setModel(ModelInterface $model)
    {
        $this->model = $model;
        $this->configureWorkflow($this->model->getWorkflowName());
    }
933426
protected function execute(CommandInterface $command)
    {
        $runner = $this->getCommandRunner();

        $this->lastReturnValue = $runner->run($command);

        $this->checkReturnValue($runner);
    }
229766
protected function setModelAttributes($model)
    {
        $this->setModelClass(Inflector::getClassNameFromNamespace($model));

        if (!property_exists($model, 'tableName') || is_null($this->tableName)) {
            $this->setTableName(Inflector::tabilize($this->getModelClass()));
        }

        if (!property_exists($model, 'database') || is_null($this->database)) {
            $this->setDatabase($this->queryBuilder()->getDefaultConnection());
        } else {
            $this->setDatabase($this->database);
        }

        if (is_null($this->getDatabase())) {
            throw new \InvalidArgumentException(
                'Please specify database name in your model. '.get_called_class()
            );
        }

        $this->setPrimarykey();
    }
850627
public function save(Domain $model): void
    {
        if (!$model->save()) {
            throw new \RuntimeException($this->i18n->t('setrun/sys', 'Saving error'));
        }
    }
1245463
public function actionModel($model = null)
    {
        if (!$model) {
            $model = $this->prompt('Namespaced path to the ngrest model (e.g. "app\models\Users"):');
        }
        $object = Yii::createObject(['class' => $model]);
        
        if (!$object instanceof NgRestModelInterface) {
            throw new Exception("Model must be instance of NgRestModelInterface.");
        }
        
        $reflector = new \ReflectionClass($model);
        $fileName = $reflector->getFileName();
        $path = dirname($fileName);
        $apiEndpoint = $object->ngrestApiEndpoint();
        $i18n = !empty($object->i18n);
        $data = [
            'path' =>  $path,
            'fileName' => basename($fileName),
            'content' => $this->generateModelContent(
                $reflector->getNamespaceName(),
                $reflector->getShortName(),
                $apiEndpoint,
                Yii::$app->db->getTableSchema($object->tableName(), true),
                $i18n
             ),
        ];
        
        $this->generateFile($data);
    }
316419
protected function fetchModel(EnvironmentInterface $environment, ModelIdInterface $modelId)
    {
        $dataProvider = $environment->getDataProvider($modelId->getDataProviderName());
        $model        = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($modelId->getId()));

        if (!$model || !$model->getId()) {
            throw new DcGeneralRuntimeException(
                'Could not load model with id ' . $modelId->getSerialized()
            );
        }

        return $model;
    }
798740
private function isRootModel(ModelInterface $model)
    {
        return (null !== $this->rootCondition) && $this->rootCondition->matches($model);
    }
178618
protected function addPaletteCondition(PaletteConditionInterface $condition)
    {
        if (!$this->palette) {
            throw new DcGeneralRuntimeException('Palette is missing, please create a palette first');
        }

        $event = new AddConditionEvent($condition, $this->palette, $this);
        $this->dispatchEvent($event);
        $condition = $event->getCondition();

        $previousCondition = $this->palette->getCondition();

        if (!$previousCondition) {
            $this->palette->setCondition($condition);
        } elseif ($previousCondition instanceof PropertyConditionChain) {
            $previousCondition->addCondition($condition);
        } else {
            $chain = $this->paletteConditionChainClass->newInstance();
            $chain->addCondition($previousCondition);
            $chain->addCondition($condition);
            $this->palette->setCondition($chain);
        }
    }
34363
protected function showActionInternal(ModelInterface $entity) {
        $outputObject = $this->getOutputObject();
        $outputObject->{$entity->getEntityName()} = $this->generateEntityAction($entity);
        $this->setOutputObject($outputObject);
    }
1151786
protected function execute(InputInterface $input, OutputInterface $output)
    {
        try
        {
            if (method_exists($this, 'start'))
            {
                $this->start();
            }
            else
            {
                throw new \RuntimeException('Command is not set correctly.');
            }
        }
        catch (\Exception $e)
        {
            $this->error($e->getMessage());
        }
    }
350916
public function apply(Builder $builder, Model $model) {
    $subclassTypes = array_keys($model->getSingleTableTypeMap());

    if (!empty($subclassTypes) ) {
      $builder->whereIn($model->getQualifiedSingleTableTypeColumn(), $subclassTypes);
    }
  }
667120
protected function setDataFromModel(ModelInterface $model): void
    {
        $this->container->set('model', $model);
        
        foreach (get_class_methods($model) as $method) {
            if (0 !== strpos($method, 'get')) {
                continue;
            }
            
            $this->data[strtolower(substr($method, 3))] = $model->$method();
        }
    }