<?php
namespace Cake\ORM;
use Cake\Collection\Collection;
use Cake\Database\Expression\TupleComparison;
use Cake\Datasource\EntityInterface;
class LazyEagerLoader
{
public function loadInto($entities, array $contain, Table $source)
{
$returnSingle = false;
if ($entities instanceof EntityInterface) {
$entities = [$entities];
$returnSingle = true;
}
$entities = new Collection($entities);
$query = $this->_getQuery($entities, $contain, $source);
$associations = array_keys($query->getContain());
$entities = $this->_injectResults($entities, $query, $associations, $source);
return $returnSingle ? array_shift($entities) : $entities;
}
protected function _getQuery($objects, $contain, $source)
{
$primaryKey = $source->getPrimaryKey();
$method = is_string($primaryKey) ? 'get' : 'extract';
$keys = $objects->map(function ($entity) use ($primaryKey, $method) {
return $entity->{$method}($primaryKey);
});
$query = $source
->find()
->select((array)$primaryKey)
->where(function ($exp, $q) use ($primaryKey, $keys, $source) {
if (is_array($primaryKey) && count($primaryKey) === 1) {
$primaryKey = current($primaryKey);
}
if (is_string($primaryKey)) {
return $exp->in($source->aliasField($primaryKey), $keys->toList());
}
$types = array_intersect_key($q->getDefaultTypes(), array_flip($primaryKey));
$primaryKey = array_map([$source, 'aliasField'], $primaryKey);
return new TupleComparison($primaryKey, $keys->toList(), $types, 'IN');
})
->contain($contain);
foreach ($query->getEagerLoader()->attachableAssociations($source) as $loadable) {
$config = $loadable->getConfig();
$config['includeFields'] = true;
$loadable->setConfig($config);
}
return $query;
}
protected function _getPropertyMap($source, $associations)
{
$map = [];
$container = $source->associations();
foreach ($associations as $assoc) {
$map[$assoc] = $container->get($assoc)->getProperty();
}
return $map;
}
protected function _injectResults($objects, $results, $associations, $source)
{
$injected = [];
$properties = $this->_getPropertyMap($source, $associations);
$primaryKey = (array)$source->getPrimaryKey();
$results = $results
->indexBy(function ($e) use ($primaryKey) {
return implode(';', $e->extract($primaryKey));
})
->toArray();
foreach ($objects as $k => $object) {
$key = implode(';', $object->extract($primaryKey));
if (!isset($results[$key])) {
$injected[$k] = $object;
continue;
}
$loaded = $results[$key];
foreach ($associations as $assoc) {
$property = $properties[$assoc];
$object->set($property, $loaded->get($property), ['useSetters' => false]);
$object->setDirty($property, false);
}
$injected[$k] = $object;
}
return $injected;
}
}