-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #265 from analogueorm/fix/inheritance-eager-loading
Fix/inheritance eager loading
- Loading branch information
Showing
9 changed files
with
290 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
src/System/EntityBuilder.php → src/System/Builders/EntityBuilder.php
100755 → 100644
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
namespace Analogue\ORM\System\Builders; | ||
|
||
use Analogue\ORM\System\Manager; | ||
use Analogue\ORM\System\Mapper; | ||
|
||
class PolymorphicResultBuilder implements ResultBuilderInterface | ||
{ | ||
/** | ||
* The default mapper used to build entities with. | ||
* | ||
* @var \Analogue\ORM\System\Mapper | ||
*/ | ||
protected $defaultMapper; | ||
|
||
/** | ||
* Reference to all mappers used in this result set. | ||
* | ||
* @var array | ||
*/ | ||
protected $mappers = []; | ||
|
||
/** | ||
* Relations that will be eager loaded on this query. | ||
* | ||
* @var array | ||
*/ | ||
protected $eagerLoads; | ||
|
||
/** | ||
* The Entity Map for the entity to build. | ||
* | ||
* @var \Analogue\ORM\EntityMap | ||
*/ | ||
protected $entityMap; | ||
|
||
/** | ||
* An array of builders used by this class to build necessary | ||
* entities for each result type. | ||
* | ||
* @var array | ||
*/ | ||
protected $builders = []; | ||
|
||
/** | ||
* ResultBuilder constructor. | ||
* | ||
* @param Mapper $defaultMapper | ||
*/ | ||
public function __construct(Mapper $defaultMapper) | ||
{ | ||
$this->defaultMapper = $defaultMapper; | ||
$this->entityMap = $defaultMapper->getEntityMap(); | ||
} | ||
|
||
/** | ||
* Convert a result set into an array of entities. | ||
* | ||
* @param array $results | ||
* @param array $eagerLoads name of the relation(s) to be eager loaded on the Entities | ||
* | ||
* @return array | ||
*/ | ||
public function build(array $results, array $eagerLoads) | ||
{ | ||
// Make a list of all primary key of the current result set. This will | ||
// allow us to group all polymorphic operations by type, then put | ||
// back every object in the intended order. | ||
$primaryKeyColumn = $this->entityMap->getKeyName(); | ||
$ids = array_map(function ($row) use ($primaryKeyColumn) { | ||
return $row[$primaryKeyColumn]; | ||
}, $results); | ||
|
||
$results = array_combine($ids, $results); | ||
|
||
// Make a list of types appearing within this result set. | ||
$discriminatorColumn = $this->entityMap->getDiscriminatorColumn(); | ||
$types = array_unique(array_pluck($results, $discriminatorColumn)); | ||
|
||
// We'll split the result set by type that will make it easier to deal | ||
// with. | ||
$entities = []; | ||
|
||
foreach ($types as $type) { | ||
$this->mappers[$type] = $this->getMapperForType($type); | ||
|
||
$resultsByType[$type] = array_filter($results, function (array $row) use ($type, $discriminatorColumn) { | ||
return $row[$discriminatorColumn] === $type; | ||
}); | ||
|
||
$entities = $entities + $this->buildResultsForType($resultsByType[$type], $type, $eagerLoads); | ||
} | ||
|
||
return array_map(function ($id) use ($entities) { | ||
return $entities[$id]; | ||
}, $ids); | ||
} | ||
|
||
protected function buildResultsForType($results, $type, array $eagerLoads) | ||
{ | ||
$builder = new ResultBuilder($this->mappers[$type]); | ||
|
||
return $builder->build($results, $eagerLoads); | ||
} | ||
|
||
protected function getMapperForType(string $type) : Mapper | ||
{ | ||
$columnMap = $this->entityMap->getDiscriminatorColumnMap(); | ||
|
||
$class = isset($columnMap[$type]) ? $columnMap[$type] : $type; | ||
|
||
return Manager::getInstance()->mapper($class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Analogue\ORM\System\Builders; | ||
|
||
use Analogue\ORM\System\Mapper; | ||
|
||
class ResultBuilderFactory | ||
{ | ||
public function make(Mapper $mapper) : ResultBuilderInterface | ||
{ | ||
switch ($mapper->getEntityMap()->getInheritanceType()) { | ||
case 'single_table': | ||
return new PolymorphicResultBuilder($mapper); | ||
default: | ||
return new ResultBuilder($mapper); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Analogue\ORM\System\Builders; | ||
|
||
interface ResultBuilderInterface | ||
{ | ||
public function build(array $results, array $eagerLoads); | ||
} |
Oops, something went wrong.