This method automagically bind in the fly unloaded relations.

@see lithium\data\Model::relations()
@param $type A type of model relation.
@param $name A relation name.
@return An array of relation instances or an instance of relation.
960117
protected static function _relations($type = null, $name = null) {
		$self = static::_object();

		if ($name) {
			if (isset($self->_relationsToLoad[$name])) {
				$t = $self->_relationsToLoad[$name];
				unset($self->_relationsToLoad[$name]);
				return static::bind($t, $name, (array) $self->{$t}[$name]);
			}
			return isset($self->_relations[$name]) ? $self->_relations[$name] : null;
		}
		if (!$type) {
			foreach ($self->_relationsToLoad as $name => $t) {
				static::bind($t, $name, (array) $self->{$t}[$name]);
			}
			$self->_relationsToLoad = [];
			return $self->_relations;
		}
		foreach ($self->_relationsToLoad as $name => $t) {
			if ($type === $t) {
				static::bind($t, $name, (array) $self->{$t}[$name]);
				unset($self->_relationsToLoad[$name]);
			}
		}
		return array_filter($self->_relations, function($i) use ($type) {
			return $i->data('type') === $type;
		});
	}
725002
public function getRelationship($name)
    {
        // If relationship does not exist throw an exception
        if ( ! $this->isRelationship($name)) {
            $exception = new ModelNotFoundException();
            $exception->setModel($name);
            throw $exception;
        }

        return $this->relationships[$name];
    }
995808
public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
    {
        $instance = $this->newRelatedInstance($related);

        // Here we will gather up the morph type and ID for the relationship so that we
        // can properly query the intermediate table of a relation. Finally, we will
        // get the table and create the relationship instances for the developers.
        [$type, $id] = $this->getMorphs($name, $type, $id);

        $table = $instance->getTable();

        $localKey = $localKey ?: $this->getKeyName();

        return $this->newMorphMany($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey);
    }
723539
public function exchange($name, $type = ExchangeType::DIRECT, array $options = [])
    {
        return $this->exchanges[$name] = (new ExchangeBuilder($this, $name, $type))->options($options);
    }
262535
public function getNestedRelationships($name)
    {
        $pieces     = explode('.', $name);
        $models     = array();
        $num_pieces = sizeof($pieces);

        //iterate over the relationships to see if they're all valid
        foreach ($pieces as $i => $rel) {
            //if this is the first item, then the model is the config's model
            if ($i === 0) {
                $models[] = $this->config->getDataModel();
            }

            //if the model method doesn't exist for any of the pieces along the way, exit out
            if (!method_exists($models[$i], $rel) || !is_a($models[$i]->{$rel}(), self::BELONGS_TO)) {
                throw new \InvalidArgumentException("The '".$this->getOption('column_name')."' column in your ".$this->config->getOption('name').
                    " model configuration needs to be either a belongsTo relationship method name or a sequence of them connected with a '.'");
            }

            //we don't need the model of the last item
            $models[] = $models[$i]->{$rel}()->getRelated();
        }

        return array('models' => $models, 'pieces' => $pieces);
    }
1369680
public function getRelation($name)
    {
        $preparedName = $this->prepareRelationName($name);

        if (!key_exists($preparedName, $this->relations)) {
            throw new InvalidParamException("Relation '$name' isn't exist");
        }


        if (!isset($this->_relations[$preparedName])) {
            $this->buildRelation($name);
        }

        return $this->_relations[$preparedName];
    }
425929
public function getRelationships($type = null, $direction = null)
    {
        if (null === $direction) {
            $relationships = array_merge($this->inboundRelationships, $this->outboundRelationships);
        } else {
            $dir = strtoupper($direction);
            if (!in_array($direction, array('IN', 'OUT'))) {
                throw new \InvalidArgumentException(sprintf('The direction "%s" is not valid', $direction));
            }
            $relationships = ('IN' === $dir) ? $this->getInboundRelationships() : $this->getOutboundRelationships();
        }

        if (null === $type) {
            return $relationships;
        }

        $collection = array();
        foreach ($relationships as $rel) {
            if ($rel->getType() === $type) {
                $collection[] = $rel;
            }
        }

        return $collection;
    }
203149
protected function getMorphs($name, $type, $id)
    {
        $type = $type ?: $name.'_type';

        $id = $this->getkeyname();

        return array($type, $id);
    }
332835
public function getTaxonomiesByType($type)
    {
        $taxonomies = new ArrayCollection();

        /** @var TermRelationships $relationship */
        foreach ($this->getTermRelationships() as $relationship) {
            if ($type === $relationship->getTaxonomy()->getTaxonomy()) {
                $taxonomies[] = $relationship->getTaxonomy();
            }
        }

        return ($taxonomies->count() == 0) ? null : $taxonomies;
    }
367691
public static function build($name, $type)
	{
		$factory = new BuildFactory(self::config($name), $type);
		$buildNode = $factory->build();
		$buildNode->build();
		return $buildNode->getNodes();
	}
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;
	}
236098
protected function addRelation($name)
    {
        if (strstr($name, '.')) {
            $parts = explode('.', $name);
            $this->name = array_pop($parts);
            $this->relation = $parts;
        } else {
            $this->name = $name;
        }
    }
393745
public function getInstance(string $type, $name = null)
    {
        if (__CLASS__ === $type) {
            return $this;
        }

        array_push($this->injectionStack, $type . '#' . $name);
        $instance = $this->getBinding($type, $name)->getInstance($this, $name);
        array_pop($this->injectionStack);
        return $instance;
    }
140841
public static function findByNameType( $name, $type, $config = [] ) {

		return self::queryByNameType( $name, $type, $config )->all();
	}
833011
public function sync($name, array $ids, $delete = true)
    {
        /** @var \yii\db\ActiveRecordInterface $relationClass */
        $relationClass = $this->owner->getRelation($name)->modelClass;

        $current = $this->getLinkedIds($name);
        $unlink = array_diff($current, $ids);
        $link = array_diff($ids, $current);

        $models = !empty($unlink) ? $relationClass::findAll($unlink) : [];
        foreach ($models as $model) {
            $this->owner->unlink($name, $model, $delete);
        }

        $models = !empty($link) ? $relationClass::findAll($link) : [];
        foreach ($models as $model) {
            $this->owner->link($name, $model);
        }

        $this->afterSync($name);

        return ['linked' => $link, 'unlinked' => $unlink];
    }
531491
protected function getRelation($name)
    {
        $relation = $this->relations[$name];
        if ($relation instanceof self || (\is_array($relation) && $relation[0] instanceof self)) {
            return $relation;
        }

        $class = 'App\\Model\\' . $relation[1];
        /** @var Model $model */
        $model = new $class();

        switch ($relation[0]) {
            case self::HAS_ONE:
                $this->relations[$name] = $model->eq($relation[2], $this->__get($this->primary))->get();
                break;
            case self::HAS_MANY:
                $this->relations[$name] = $model->eq($relation[2], $this->__get($this->primary))->select();
                break;
            case self::BELONGS_TO:
                $this->relations[$name] = $model->get($this->__get($relation[2]));
                break;
            default:
                throw new ModelException("Relation $name not found.");
        }

        return $this->relations[$name];
    }
586032
protected function loadRelation(array $models, $name, Closure $constraints)
    {
        $relation = $this->getRelation($name);

        $relation->addEagerConstraints($models);

        call_user_func($constraints, $relation);

        $models = $relation->initRelation($models, $name);

        $results = $relation->getEager();

        return $relation->match($models, $results, $name);
    }