@param EntityState $state
@param             $relationsIterator

@return array
359002
private function handleManyManyRelations(EntityState $state, $relationsIterator)
    {
        $object = $state->getObject();

        $manyManyRelations = new \CallbackFilterIterator(
            $relationsIterator,
            function (Relation $relation) {
                return $relation instanceof Relation\ManyToMany;
            }
        );

        $modifiedManyManyRelations = [];

        /** @var Relation\ManyToMany $relation */
        foreach ($manyManyRelations as $relationName => $relation) {
            $relatedEntity = $relation->getEntity();

            $currentForeignKeys = array_map(
                function ($object) use ($relatedEntity) {
                    //TODO should be disallowed? set the foreign key property instead?
                    if (!is_object($object)) {
                        //the foreign key is set directly
                        return $object;
                    }
                    $relatedEntity->save($object);

                    return $relatedEntity->getPrimaryKeyValue($object);
                },
                $relation->get($object)
            );

            $originalForeignKeys = $state->getRelationForeignKeys($relationName);

            //TODO it is possible that a relation is not loaded but the data contains its keys and/or related objects
            //in this case the records may already be in the database - should not insert them blindly
            $modifiedManyManyRelations[ $relationName ] = [
                'deleted'  => array_diff($originalForeignKeys, $currentForeignKeys),
                'inserted' => array_diff($currentForeignKeys, $originalForeignKeys)
            ];

            $state->setRelationForeignKeys($relationName, $currentForeignKeys);
        }

        return $modifiedManyManyRelations;
    }
511442
public function getDescendantStates()
    {
        $states = array();

        foreach ($this->getDescendants('SwatState') as $id => $object) {
            $states[$id] = $object->getState();
        }

        return $states;
    }
744701
protected function getNestedRelation(Model $model, $relationArray)
    {
        $relation = null;
        foreach ($relationArray as $relation) {
            /** @var Relation $relation */
            $relation = $model->newQuery()->getRelation($relation);
            if ($relation) {
                $model = $relation->getRelated();
            }
        }

        return $relation;
    }
1300868
public function findListItem($id = null, array $relations = array())
    {
        return new Accessor\ListItemAccessor($id, $relations, $this->con);
    }
110911
public function getMissingEntities(string $relation): array
    {
        $cachedRelations = $this->getCachedAttribute($relation);

        if (is_null($cachedRelations)) {
            return [];
        }

        $missing = [];

        foreach ($cachedRelations as $hash) {
            if (!$this->getRelatedAggregateFromHash($hash, $relation)) {
                $missing[] = $hash;
            }
        }

        return $missing;
    }
490626
private function getNodeModels($class, $relation_id)
    {
        if ($this->request->id === '0') {
            return is_null($relation_id)
                ? $class::all()->toHierarchy()
                : $class::where('parent_id',$relation_id)->get()->toHierarchy();
        }

        return $class::findOrFail($this->request->id)->descendants()->limitDepth(1)->get();
    }
151392
private function processRelationQueue(Bag $relationQueue)
    {
        foreach ($relationQueue as $source => $links) {
            $entity = $this->query->getContent($source);
            $relations = [];
            foreach ($links as $linkKey) {
                $relation = $this->query->getContent($linkKey);
                $relatedKey = (string) $relation->getContentType();

                $relations[$relatedKey][] = $relation->getId();
            }
            /** @var Collection\Relations $related */
            $related = $this->em->createCollection(Entity\Relations::class);
            $related->setFromPost(['relation' => $relations], $entity);
            $entity->setRelation($related);
            $this->em->save($entity);
        }
    }
740477
protected function reduceRelations(&$resource, array $relationNames)
    {
        $keep = [];
        $keepName = [];
        $totalReduced = 0;
        foreach ($resource->relations as $relation) {

            if (in_array($relation->getName(), $relationNames) || in_array($relation->getName(), $keepName)) {
                $totalReduced++;
                continue;
            }

            $keep[] = $relation;
            $keepName[] = $relation->getName();
        }

        $resource->relations = $keep;

        return $totalReduced;
    }
311434
protected function getRelationsArray(array $relations): array
    {
        $relationsArray = [];
        foreach ($relations as $relation) {
            $key = $relation->getName();

            $relationsArray[$key] = (new self($relation->getJory(), $this->minified))->get();
        }

        return $relationsArray;
    }
464491
protected function loadRelations(ResourceInterface $resource, array $relationshipKeys)
    {
        if ($this->relationLoader !== null && !empty($relationshipKeys)) {
            $this->relationLoader->load($resource, $relationshipKeys);
        }
    }
198493
public static function getRelationGridColumns($model, $fields, $relationName, $relation)
    {
        $columns = parent::getRelationGridColumns($model, $fields, $relationName, $relation);
        $controller = Yii::$app->crudModelsMap[$model::className()];
        $actionColumn = new \yii\grid\ActionColumn();

        if (!isset($columns[0]) || !isset($columns[0]['class']) || $columns[0]['class'] !== 'yii\grid\ActionColumn') {
            array_unshift($columns, [
                'class'         => 'yii\grid\ActionColumn',
                'headerOptions' => ['class' => 'column-action'],
                'controller'    => $controller,
                'template'      => '',
                'buttons'       => [],
            ]);
        }

        $columns[0]['template'] = '{update} {unlink}';
        $columns[0]['urlCreator'] = function ($action, $model, $key, $index) use ($controller, $relation) {
            $params = is_array($key) ? $key : ['id' => (string)$key];
            if ($action === 'update') {
                $params['hide'] = implode(',', array_keys($relation->link));
            }
            $params[0] = $controller . '/' . $action;

            return Url::toRoute($params);
        };
        $columns[0]['buttons']['update'] = function ($url, $model, $key) use ($actionColumn, $relationName) {
            /** @var \netis\crud\db\ActiveRecord $model */
            if (!Yii::$app->user->can($model::className() . '.update', ['model' => $model])) {
                return null;
            }

            $options = array_merge([
                'title'         => Yii::t('app', 'Update'),
                'aria-label'    => Yii::t('app', 'Update'),
                'data-pjax'     => '0',
                // required for editing in a modal
                'data-toggle'   => 'modal',
                'data-target'   => '#relationModal',
                'data-relation' => $relationName,
                'data-title'    => $model->getCrudLabel('update'),
                'data-pjax-url' => $url,
                'class'         => 'btn btn-default btn-xs',
            ], $actionColumn->buttonOptions);
            return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, $options);
        };

        /** @var \yii\db\ActiveQuery $relation */
        $remove = false;
        $modelClass = $relation->modelClass;
        foreach (array_keys($relation->link) as $foreignKey) {
            $remove = $remove || !$modelClass::getTableSchema()->getColumn($foreignKey)->allowNull;
        }

        $columns[0]['buttons']['unlink'] = function ($url, $model, $key) use ($actionColumn, $remove) {
            /** @var \netis\crud\db\ActiveRecord $model */
            if (!Yii::$app->user->can($model::className() . ($remove ? '.delete' : '.update'), ['model' => $model])) {
                return null;
            }

            $options = array_merge([
                'title'      => Yii::t('app', 'Unlink'),
                'aria-label' => Yii::t('app', 'Unlink'),
                //'data-confirm' => Yii::t('app', 'Are you sure you want to unlink this item?'),
                'data-pjax'  => '0',
                'class'      => 'remove btn btn-default btn-xs',
            ], $actionColumn->buttonOptions);
            return Html::a('<span class="glyphicon glyphicon-remove"></span>', $url, $options);
        };
        return $columns;
    }
153716
public function addState(\Haven\PersonaBundle\Entity\State $states)
    {
        $this->states[] = $states;

        return $this;
    }
687863
protected function arrayHandler(ParsingState $state, $arrayText)
    {
        /** @var $arrayNode ArrayNode */
        $arrayNode = $this->objectManager->get(\TYPO3\Fluid\Core\Parser\SyntaxTree\ArrayNode::class, $this->recursiveArrayHandler($arrayText));
        $state->getNodeFromStack()->addChildNode($arrayNode);
    }
645976
protected function getRelations($relations)
    {
        $existingNames = [];
        $finalRelations = [];
        foreach ($relations as $relation) {
            $newRelation = ForeignRelationship::fromString($relation);
            if (is_null($newRelation)) {
                continue;
            }

            if (!empty($newRelation->name) && in_array($newRelation->name, $existingNames)) {
                continue;
            }

            $finalRelations[] = $newRelation;
            $existingNames[] = $newRelation->name;
        }

        return $finalRelations;
    }
799680
public function getObjects($state = null)
    {
        if (!isset($state)) {
            return $this->objects;
        }

        $objectList = [];
        foreach ($this->objects as $id => $object) {
            if ($this->objectStates[$id] == $state) {
                $objectList[$id] = $object;
            }
        }

        return $objectList;
    }
232703
public function readUsers(
        $index,
        iterable $relationshipFilters = null,
        iterable $relationshipSorts = null
    ): PaginatedDataInterface {
        $this->authorize(RoleRules::ACTION_VIEW_ROLE_USERS, Schema::TYPE, $index);

        return $this->readRelationshipInt($index, Model::REL_USERS, $relationshipFilters, $relationshipSorts);
    }
353738
protected function getRelationInputs($inputs = [])
    {
        $relations = [];

        foreach ($inputs as $column => $value) {
            if (method_exists($this->model, $column)) {
                $relation = call_user_func([$this->model, $column]);

                if ($relation instanceof Relation) {
                    $relations[$column] = $value;
                }
            }
        }

        return $relations;
    }