lithium\data\Model::embed()
Eager loads relations.
Parameters
-
mixed
$collection
The collection to extend.
-
array
$relations
The relations to eager load.
Returns
mixedThe collection.
Source
public static function embed(&$collection, $relations) {
$tree = Set::expand(array_fill_keys(array_keys(Set::normalize($relations)), []));
foreach ($tree as $name => $subtree) {
$rel = static::relations($name);
$to = $rel->to();
$related = $rel->embed($collection, isset($relations[$name]) ? $relations[$name] : []);
$subrelations = [];
foreach ($relations as $path => $value) {
if (preg_match('~^'.$name.'\.(.*)$~', $path, $matches)) {
$subrelations[] = $matches[1];
}
}
if ($subrelations) {
$to::embed($related, $subrelations);
}
}
return $collection;
}