Performance optimization & extendability improvement for eager loading of authors #9907
-
Background: Problem & Proposed solution: Which happens here: This query is unnecasarry, as the Old method: public static function eagerLoadingMap(array $sourceElements, string $handle)
{
if ($handle === 'author') {
// Get the source element IDs
$sourceElementIds = ArrayHelper::getColumn($sourceElements, 'id');
$map = (new Query())
->select(['id as source', 'authorId as target'])
->from([Table::ENTRIES])
->where(['and', ['id' => $sourceElementIds], ['not', ['authorId' => null]]])
->all();
return [
'elementType' => User::class,
'map' => $map,
'criteria' => [
'status' => null,
],
];
}
return parent::eagerLoadingMap($sourceElements, $handle);
} Proposed solution: public static function eagerLoadingMap(array $sourceElements, string $handle)
{
if ($handle === 'author') {
$sourceElementsWithAuthors = array_filter($sourceElements, function(Entry $entry) {
return $entry->authorId !== null;
});
$map = array_map(function(Entry $entry){
return [
'source' => $entry->id,
'target' => $entry->authorId
];
}, $sourceElementsWithAuthors);
return [
'elementType' => User::class,
'map' => $map,
'criteria' => [
'status' => null,
],
];
}
return parent::eagerLoadingMap($sourceElements, $handle);
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Thanks for the suggestion! Just implemented that change for the next release. |
Beta Was this translation helpful? Give feedback.
Thanks for the suggestion! Just implemented that change for the next release.