Get the first edge relationship between two models.

@param  \Sgpatil\Orientdb\Eloquent\Model  $parentModel
@param  \Sgpatil\Orientdb\Eloquent\Model  $relatedModel
@param  string $direction
@return \Sgpatil\Orientdb\Eloquent\Edges\Edge[In|Out]|null
175498
public function first(Model $parentModel, Model $relatedModel, $type, $direction = 'any')
    {
        // First we get the first relationship instance between the two models
        // absed on the given direction.
        $relation = $this->firstRelation($parentModel, $relatedModel, $type, $direction);

        // Let's stop here if there is no relationship between them.
        if (is_null($relation)) return null;

        // Now we can return the determined edge out of the relation and direction.
        return $this->edgeFromRelationWithDirection($relation, $parentModel, $relatedModel, $direction);
    }
750110
public static function findChildren($model, $parentId) {
        return $children = $model::find()->where(['parent_id' => $parentId])->orderBy('position')->all();
    }
1185875
public function getParent()
    {
        if ($this->parentRelation === null && $this->isParentRelationResolved === false) {
            $this->isParentRelationResolved = true;

            try {
                $source = $this->getObject();
                if ($source instanceof AttachmentContainerInterface) {
                    $model = $this->modelFactory()->create(self::class)->loadFrom('attachment_id', $source->id());
                    if ($model->id()) {
                        $this->parentRelation = $model;
                    }
                }
            } catch (Exception $e) {
                throw new LogicException(sprintf(
                    'Could not load the parent relationship: %s',
                    $e->getMessage()
                ), 0, $e);
            }
        }

        return $this->parentRelation;
    }
64296
public static function resolveRelation($model, $relModel)
    {
        if (is_string($relModel) &&
            BaseOrm::RECURSIVE_RELATIONSHIP_CONSTANT == $relModel) {
            return self::resolveRelation($model, $model);
        } elseif ($relModel instanceof Model) {
            return $relModel->getMeta()->getNSModelName();
        }

        return $relModel;
    }
1263271
protected function getRelatedModelsWithImages($parents, $relation)
    {
        $models = [];

        foreach ($parents as $parent) {
            // Get the related object
            $related = $parent->$relation;

            // The related object is a model (like in a belongsTo setup), add it to
            // the array
            if (is_a($related, Model::class)) {
                $models[] = $related;
            }

            // Otherwise, if the relation returns a collection (like a hasMany), add
            // all the related models to the growing array.
            elseif (is_a($related, Collection::class)) {
                $models = array_merge($models, $related->all());
            }
        }

        return $models;
    }
32850
public static function hydrateRecursive(Model $model, array $items, Relation $parentRelation = null)
    {
        $instance = $model;

        $items = array_map(function ($item) use ($instance, $parentRelation) {
            // Convert all null relations into empty arrays
            $item = $item ?: [];
            
            return static::newFromBuilderRecursive($instance, $item, $parentRelation);
        }, $items);

        return $instance->newCollection($items);
    }
1253469
protected function doSetSportId(Object $model, $relatedId) {
		if ($model->getSportId() !== $relatedId) {
			$model->setSportId($relatedId);

			return true;
		}

		return false;
	}
580467
public function populate($relatedModel, $ruleKey = null) {
        // Sanity check: does the model have a reference to the
        // given model in the first place?
        // This will throw an exception if not.
        $relatedModel = $relatedModel instanceof Garp_Model_Db ? get_class($relatedModel) : $relatedModel;
        $relatedModel = (substr($relatedModel, 0, 6) !== 'Model_' ? 'Model_' : '') . $relatedModel;
        $ref = $this->_model->getReference($relatedModel, $ruleKey);
        $locales = Garp_I18n::getLocales();
        foreach ($locales as $locale) {
            $factory = new Garp_I18n_ModelFactory($locale);
            $localizedModel = $factory->getModel($relatedModel);
            $localizedModelName = get_class($localizedModel);
            $cleanLocalizedName = $localizedModel->getNameWithoutNamespace();
            $this->_model->addReference(
                $cleanLocalizedName,
                $ref[Zend_Db_Table_Abstract::COLUMNS],
                $localizedModelName,
                $ref[Zend_Db_Table_Abstract::REF_COLUMNS]
            );
        }
        return $this;
    }
1058161
public function belongsToMany($related, $table = null, $foreignKey = null, $otherKey = null, $relation = null)
    {
        // If no relationship name was passed, we will pull backtraces to get the
        // name of the calling function. We will use that function name as the
        // title of this relation since that is a great convention to apply.
        if (is_null($relation)) {
            $relation = $this->getBelongsToManyCaller();
        }

        $foreignKey = $foreignKey ?: $this->getCmsReferenceKeyForRelation($relation);
        $otherKey   = $otherKey   ?: $this->getCmsReferenceKeyForRelation($relation, true);

        if (is_null($table)) {
            $table = $this->getCmsJoiningTable();
        }

        $fieldId = $this->getCmsReferenceFieldId($relation);
        if (empty($fieldId)) {
            throw new InvalidArgumentException("No 'field' id configured for relation/reference: '{$relation}'!");
        }

        // originally, it was possible to just get the parent's BelongsToMany instance, but
        // to avoid problems when saving on the relationship, we need a specialized extension
        // of the relation class for the CMS.
        //$belongsToMany = parent::belongsToMany($related, $table, $foreignKey, $otherKey);

        /** @var Model $instance */
        $instance = new $related;

        $query = $instance->newQuery();

        $belongsToMany = new BelongsToMany($query, $this, $table, $foreignKey, $otherKey, $relation, $fieldId);

        // add constraints
        $belongsToMany->wherePivot(config('pxlcms.relations.references.keys.field', 'from_field_id'), $fieldId);

        return $belongsToMany;
    }
264206
protected function OneToOne($related, $model, $local, $remote)
    {
        $relationVal = $this->oneRelationValue($related, $model, $local);
        $relationColumn = $this->oneRelationColumn($related, $model, $remote);
        //
        return $this->all($related, $relationColumn, $relationVal);
    }
1048576
public function parentModel()
    {
        if ($this->value
            && ($relation = $this->guessRelation())
            && ($model = $this->getModel())
            && method_exists($model, $relation)) {
            return $model->$relation;
        }
    }
473552
public function hasMany($related, $parentKey = null, $localKey = null)
    {
        $parentKey = $parentKey ?: $this->getParentKey();
        $localKey = $localKey ?: $this->getKeyName();

        $instance = new $related;

        return new Relations\Relation($instance, $this, $parentKey, $localKey);
    }
1107969
protected function getType($model, $related, $local, $remote)
    {
        $modelObject = new $model();
        $remoteObject = new $related();
        //
        $tablemodel = $this->getTable($modelObject);
        $tableremote = $this->getTable($remoteObject);
        //
        if (is_null($local) && is_null($remote)) {
            $model = $tablemodel.'_id';
            $remote = $tableremote.'_id';
        } else {
            $model = $remote;
        }
        //
        if (in_array(strtolower($model), $remoteObject->_columns)) {
            $this->relation = OneToOneRelation;
        }
        if (in_array(strtolower($remote), $modelObject->_columns)) {
            $this->relation = OneToManyRelation;
        }
        //
        return $this->relation;
    }
216305
public function initRelation()
    {
        switch ($this->direction)
        {
            case 'in':
                // Make them nodes
                $this->start = $this->asNode($this->related);
                $this->end   = $this->asNode($this->parent);
                // Setup relationship
                $this->relation = $this->makeRelationship($this->type, $this->relation, $this->parent, $this->attributes);
            break;

            case 'out':
                // Make them nodes
                $this->start = $this->asNode($this->parent);
                $this->end   = $this->asNode($this->related);
                // Setup relationship
                $this->relation = $this->makeRelationship($this->type, $this->parent, $this->related, $this->attributes);
            break;

            default:
                throw new NoEdgeDirectionException;
            break;
        }
    }
1150561
public function withHasMany(array $data, $relatedModel, $relation_name){
        $this->withHasMany['relation'] = $relation_name;
        foreach($data as $k=>$v){
            $this->withHasMany['data'][] = new $relatedModel($v);
        }
        return $this;
    }
164064
protected function siblings($direction = '', $parentId = false, $order = 'asc')
    {
        $parentId = ($parentId === false ? $this->parent_id : $parentId);

        /**
         * @var QueryBuilder $query
         */
        $query = $this->where($this->getParentIdColumn(), '=', $parentId);

        $column = $this->getPositionColumn();

        switch ($direction) {
            case static::QUERY_ALL:
                $query->where($column, '<>', $this->position)->orderBy($column, $order);
                break;

            case static::QUERY_PREV_ALL:
                $query->where($column, '<', $this->position)->orderBy($column, $order);
                break;

            case static::QUERY_PREV_ONE:
                $query->where($column, '=', $this->position - 1);
                break;

            case static::QUERY_NEXT_ALL:
                $query->where($column, '>', $this->position)->orderBy($column, $order);
                break;

            case static::QUERY_NEXT_ONE:
                $query->where($column, '=', $this->position + 1);
                break;

            case static::QUERY_NEIGHBORS:
                $query->whereIn($column, [$this->position - 1, $this->position + 1]);
                break;

            case static::QUERY_LAST:
                $query->orderBy($column, 'desc');
                break;
        }

        if (is_int($direction)) {
            $query->where($column, '=', $direction);
        } else if (is_array($direction)) {
            $query->buildWherePosition($this->getPositionColumn(), $direction);
        }

        return $query;
    }
711479
public function morphMany(Model $relatedModel, $morphName)
    {
        if ($relatedModel->isIgnored()) {
            return;
        }

        $this->relations->add(
            new Relations\MorphMany($this, $relatedModel, $morphName)
        );
    }