public function embed(&$collection, $relations, $options = [])
    {
        $expanded = [];
        $relations = $this->expand($relations);
        $tree = $this->treeify($relations);

        $habtm = [];

        foreach ($tree as $name => $subtree) {
            $rel = $this->relation($name);
            if ($rel->type() === 'hasManyThrough') {
                $habtm[] = $name;
                continue;
            }

            $to = $rel->to();
            $query = empty($relations[$name]) ? [] : $relations[$name];
            if (is_callable($query)) {
                $options['query']['handler'] = $query;
            } else {
                $options['query'] = $query;
            }
            $related = $rel->embed($collection, $options);

            $subrelations = [];
            foreach ($relations as $path => $value) {
                if (preg_match('~^'.$name.'\.(.*)$~', $path, $matches)) {
                    $subrelations[$matches[1]] = $value;
                }
            }
            if ($subrelations) {
                $to::definition()->embed($related, $subrelations, $options);
            }
        }

        foreach ($habtm as $name) {
            $rel = $this->relation($name);
            $related = $rel->embed($collection, $options);
        }
    }
public function embed(&$collection, $relations, $options = [])
    {
        $expanded = [];
        $relations = $this->expand($relations);
        $tree = $this->treeify($relations);

        $habtm = [];

        foreach ($tree as $name => $subtree) {
            $rel = $this->relation($name);
            if ($rel->type() === 'hasManyThrough') {
                $habtm[] = $name;
                continue;
            }

            $to = $rel->to();
            $query = empty($relations[$name]) ? [] : $relations[$name];
            if (is_callable($query)) {
                $options['query']['handler'] = $query;
            } else {
                $options['query'] = $query;
            }
            $related = $rel->embed($collection, $options);

            $subrelations = [];
            foreach ($relations as $path => $value) {
                if (preg_match('~^'.$name.'\.(.*)$~', $path, $matches)) {
                    $subrelations[$matches[1]] = $value;
                }
            }
            if ($subrelations) {
                $to::definition()->embed($related, $subrelations, $options);
            }
        }

        foreach ($habtm as $name) {
            $rel = $this->relation($name);
            $related = $rel->embed($collection, $options);
        }
    }
public function embed ( & $ collection , $ relations , $ options = [ ] ) { $ expanded = [ ] ; $ relations = $ this -> expand ( $ relations ) ; $ tree = $ this -> treeify ( $ relations ) ; $ habtm = [ ] ; foreach ( $ tree as $ name => $ subtree ) { $ rel = $ this -> relation ( $ name ) ; if ( $ rel -> type ( ) === 'hasManyThrough' ) { $ habtm [ ] = $ name ; continue ; } $ to = $ rel -> to ( ) ; $ query = empty ( $ relations [ $ name ] ) ? [ ] : $ relations [ $ name ] ; if ( is_callable ( $ query ) ) { $ options [ 'query' ] [ 'handler' ] = $ query ; } else { $ options [ 'query' ] = $ query ; } $ related = $ rel -> embed ( $ collection , $ options ) ; $ subrelations = [ ] ; foreach ( $ relations as $ path => $ value ) { if ( preg_match ( '~^' . $ name . '\.(.*)$~' , $ path , $ matches ) ) { $ subrelations [ $ matches [ 1 ] ] = $ value ; } } if ( $ subrelations ) { $ to :: definition ( ) -> embed ( $ related , $ subrelations , $ options ) ; } } foreach ( $ habtm as $ name ) { $ rel = $ this -> relation ( $ name ) ; $ related = $ rel -> embed ( $ collection , $ options ) ; } }
Eager loads relations.

@param array $collection The collection to extend.
@param array $relations  The relations to eager load.
@param array $options    The fetching options.
Eager loads relations .