Determine if the relationship is nested.

@param  string $name
@param  string $relation
@return bool
325878
protected function isNested( $name, $relation )
	{
		$dots = Str::contains( $name, '.' );

		return $dots && Str::startsWith( $name, $relation . '.' );
	}
771690
public function hasRole($name)
    {
        if (!$this->relationLoaded('role')) {
            $this->load('role');
        }

        return in_array($this->role->name, (is_array($name) ? $name : [$name]));
    }
390533
private function isRelationForeignKeyNullable(Relation $relation)
    {
        $reflectionObj = new \ReflectionObject($relation);
        if (!$reflectionObj->hasProperty('foreignKey')) {
            return false;
        }
        $fkProp = $reflectionObj->getProperty('foreignKey');
        $fkProp->setAccessible(true);

        return isset($this->nullableColumns[$fkProp->getValue($relation)]);
    }
1060616
protected function findRelationByName($relationName)
    {
        foreach ($this->relations as $relation) {
            if ($relation->name() == $relationName || $relation->entity() == $relationName) {
                return $relation;
            }
        }

        return null;
    }
1303949
protected function checkRelationExistence($name)
    {
        $getter = 'get' . ucfirst($name);
        $class = get_class($this->owner);

        if (method_exists($this->owner, $getter)) {
            /** @var ActiveQuery $value */
            $value = $this->owner->$getter();

            if (!($value instanceof ActiveQueryInterface)) {
                throw new InvalidConfigException("Value of relation '$getter' not valid");
            }

            $this->models[$name]['multiple'] = $value->multiple;
        } else {
            throw new InvalidConfigException("Relation '$class::$getter' for attribute '$name' does not exists");
        }
    }
939263
private function checkBelongsToRelation($entity, $relation)
    {
        $varObjName = $relation->varObjectName();
        $varIdName  = $relation->varIdName();

        $currentRelation  = $this->getValue($entity, $varObjName);
        $originalRelation = $this->getValue($this->entityRegistry->getOriginal($entity), $varObjName);

        if (empty($currentRelation)) {
            // Current entity has no relation
            if (!empty($originalRelation)) {
                // Original had a relation, so relation was removed
                $this->setValue($entity, $varIdName, null);

                if ($relation->isRequired()) {
                    $this->scheduleForDeletion($entity);
                }
            }

            return;
        }

        // Current entity has a relation
        $currentRelationHash = $this->entityRegistry->getObjectHashId($currentRelation);

        if (empty($originalRelation)) {
            // Original had no relation, so relation was newly created
            $this->scheduleRelationForInsertion($currentRelation, $entity, $varIdName);

            return;
        }

        $originalRelationHash = $this->entityRegistry->getObjectHashId($originalRelation);

        if ($currentRelationHash != $originalRelationHash) {
            // Relation was exchanged
            $this->scheduleRelationForInsertion($currentRelation, $entity, $varIdName);
            $this->setValue($entity, $varIdName, null);
        }
    }
872593
public function getRelation( $name )
	{
		// We want to run a relationship query without any constrains so that we will
		// not have to remove these where clauses manually which gets really hacky
		// and is error prone while we remove the developer's own where clauses.
		$relation = Relation::noConstraints( function () use ( $name )
		{
			return $this->getModel()->$name();
		} );

		$nested = $this->nestedRelations( $name );

		// If there are nested relationships set on the query, we will put those onto
		// the query instances so that they can be handled after this relationship
		// is loaded. In this way they will all trickle down as they are loaded.
		if ( count( $nested ) > 0 )
		{
			$relation->getQuery()->with( $nested );
		}

		return $relation;
	}
1030198
protected function checkRelationName($sRelationName) : bool
    {
        $sCheckedRelationName = $this->getRelationName();
        if (empty($sCheckedRelationName)) {
            return true;
        }

        if (is_array($sCheckedRelationName) && in_array($sRelationName, $sCheckedRelationName)) {
            return true;
        }

        $bResult = $sRelationName == $sCheckedRelationName;

        return $bResult;
    }
716070
def _nested_relations(self, relation):
        
        nested = {}

        for name, constraints in self._eager_load.items():
            if self._is_nested(name, relation):
                nested[name[len(relation + ".") :]] = constraints

        return nested
1370235
public function __isset($name) {
		return isset($this->_updated[$name]) || isset($this->_relationships[$name]);
	}
1316641
protected function isMemberOf($member, $relation)
    {
        if (!$member || !$member->exists()) {
            return false;
        }

        if ($relation instanceof UnsavedRelationList) {
            return in_array($member->ID, $relation->getIDList());
        }

        return $relation->byID($member->ID) !== null;
    }
455454
public static function areRelated(...$relations)
	{
		try {
			$max_key = count($relations) - 1;

			foreach ($relations as $key => $current) {
				if (!is_array($current)) {
					$previous = null;

					if ($key > 0) {
						$previous = $relations[ $key - 1 ];
						$previous = is_array($previous)
							? $previous[ 0 ] : $previous;
					}

					$basename = strtolower(class_basename($current));
					$method   = Str::plural($basename);

					if (!is_null($previous)) {
						if (!method_exists($previous, $method)) {
							$method = Str::singular($basename);
						}

						if (!method_exists($previous, $method)) {
							throw new \Exception('UNABLE_TO_FIND_RELATIONSHIP');
						}
					}

					$relations[ $key ] = [ $current, $method ];
				}

				if (!($relations[ $key ][ 0 ] instanceof Model)) {
					throw new \InvalidArgumentException('INVALID_MODEL');
				}
			}

			foreach ($relations as $key => $current) {
				if ($key !== $max_key) {
					$model    = $current[ 0 ];
					$relation = $relations[ $key + 1 ];

					$model->{$relation[ 1 ]}()->findOrFail($relation[ 0 ]->id);
				}
			}

			return true;
		} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
			return false;
		}
	}
484716
protected function parseSingleRelationship(string $relation): bool
    {
        if (!$value = $this->parseForCommonValues($relation)) {
            return true;
        }

        if ($value instanceof Collection || is_array($value) || $value instanceof CollectionProxy) {
            throw new MappingException("Entity's attribute $relation should not be array, or collection");
        }

        if ($value instanceof LazyLoadingInterface && !$value->isProxyInitialized()) {
            $this->relationships[$relation] = [];

            return true;
        }

        // If the attribute is a loaded proxy, swap it for its
        // loaded entity.
        if ($value instanceof LazyLoadingInterface && $value->isProxyInitialized()) {
            $value = $value->getWrappedValueHolderValue();
        }

        if ($this->isParentOrRoot($value)) {
            $this->relationships[$relation] = [];

            return true;
        }

        // At this point, we can assume the attribute is an Entity instance
        // so we'll treat it as such.
        $subAggregate = $this->createSubAggregate($value, $relation);

        // Even if it's a single entity, we'll store it as an array
        // just for consistency with other relationships
        $this->relationships[$relation] = [$subAggregate];

        // We always need to check a loaded relation is in sync
        // with its local key
        $this->needSync[] = $relation;

        return true;
    }
789311
public function getRelation($relation)
    {
        $query = Relation::noConstraints(function() use ($relation)
        {
            return $this->getModel()->$relation();
        });

        $nested = $this->nestedRelations($relation);

        if (count($nested) > 0) {
            $query->getQuery()->with($nested);
        }

        return $query;
    }
14468
public function getRelation($name) {
        // We want to run a relationship query without any constrains so that we will
        // not have to remove these where clauses manually which gets really hacky
        // and error prone. We don't want constraints because we add eager ones.
        $relation = Relation::noConstraints(function () use ($name) {
            try {
                return $this->getModel()->{$name}();
            } catch (\Exception $e) {
                throw $e;//RelationNotFoundException::make($this->getModel(), $name);
            }
        });

        $nested = $this->relationsNestedUnder($name);

        // If there are nested relationships set on the query, we will put those onto
        // the query instances so that they can be handled after this relationship
        // is loaded. In this way they will all trickle down as they are loaded.
        if (count($nested) > 0) {
            $relation->getQuery()->with($nested);
        }

        return $relation;
    }
960573
public function getRelationSavingModels($relation)
    {
        $result = isset($this->savingModels[$relation])
            ? $this->savingModels[$relation]
            : $this->owner->$relation;
        return is_array($result) && (!$this->owner->getRelation($relation)->multiple)
            ? reset($result) : $result;
    }
967865
public function setRelated($name, $values)
    {
        $relation = $this->owner->getRelation($name, false);
        if ($relation === null) {
            return false;
        }
        $class = $relation->modelClass;
        $multiple = $relation->multiple;
        $link = $relation->link;
        $uniqueKeys = array_flip($class::primaryKey());
        foreach (array_keys($link) as $from) {
            unset($uniqueKeys[$from]);
        }
        $uniqueKeys = array_keys($uniqueKeys);
        if (isset($this->_original_relations[$name])) {
            $children = $this->_original_relations[$name];
        } else {
            $this->_original_relations[$name] = $children = $this->owner->$name;
        }

        if ($multiple) {
            $newChildren = [];
            foreach ($values as $index => $value) {
                // get from current relation
                // if has child with same primary key, use this
                /* @var $newChild \yii\db\ActiveRecord */
                $newChild = null;
                if (empty($relation->indexBy)) {
                    foreach ($children as $i => $child) {
                        if ($this->isEqual($child, $value, $uniqueKeys)) {
                            if ($value instanceof $class) {
                                $newChild = $value;
                                $newChild->isNewRecord = $child->isNewRecord;
                                $newChild->oldAttributes = $child->oldAttributes;
                            } else {
                                $newChild = $child;
                            }
                            unset($children[$i]);
                            break;
                        }
                    }
                } elseif (isset($children[$index])) {
                    $child = $children[$index];
                    if ($value instanceof $class) {
                        $newChild = $value;
                        $newChild->isNewRecord = $child->isNewRecord;
                        $newChild->oldAttributes = $child->oldAttributes;
                    } else {
                        $newChild = $child;
                    }
                    unset($children[$index]);
                }
                if ($newChild === null) {
                    $newChild = $value instanceof $class ? $value : new $class;
                }
                if (isset($this->relatedScenarios[$name])) {
                    $newChild->scenario = $this->relatedScenarios[$name];
                }
                if (!$value instanceof $class) {
                    $newChild->load($value, '');
                }
                foreach ($link as $from => $to) {
                    $newChild->$from = $this->owner->$to;
                }
                $newChildren[$index] = $newChild;
            }
            $this->_old_relations[$name] = $children;
            $this->owner->populateRelation($name, $newChildren);
            $this->_process_relation[$name] = true;
        } else {
            $newChild = null;
            if ($children === null) {
                if ($values !== null) {
                    $newChild = $values instanceof $class ? $values : new $class;
                    $this->_process_relation[$name] = true;
                }
            } else {
                if ($values !== null) {
                    $newChild = $values instanceof $class ? $values : $children;
                    if ($values instanceof $class) {
                        $newChild = $values;
                        $newChild->oldAttributes = $children->oldAttributes;
                        $newChild->isNewRecord = $children->isNewRecord;
                    } else {
                        $newChild = $children;
                    }
                } else {
                    $this->_old_relations[$name] = [$children];
                }
                $this->_process_relation[$name] = true;
            }
            if ($newChild !== null) {
                if (isset($this->relatedScenarios[$name])) {
                    $newChild->scenario = $this->relatedScenarios[$name];
                }
                if (!$values instanceof $class) {
                    $newChild->load($values, '');
                }
                foreach ($link as $from => $to) {
                    $newChild->$from = $this->owner->$to;
                }
            }
            $this->owner->populateRelation($name, $newChild);
        }
        return true;
    }