Skip to content

Commit f0303de

Browse files
committed
Search: Improved result hydration performance
1 parent 0b26573 commit f0303de

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

app/Entities/Tools/EntityHydrator.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ public function hydrate(): array
3737
$hydrated = [];
3838

3939
foreach ($this->entities as $entity) {
40-
$data = $entity->toArray();
40+
$data = $entity->getRawOriginal();
4141
$instance = Entity::instanceFromType($entity->type);
4242

4343
if ($instance instanceof Page) {
4444
$data['text'] = $data['description'];
4545
unset($data['description']);
4646
}
4747

48-
$instance->forceFill($data);
48+
$instance = $instance->setRawAttributes($data, true);
4949
$hydrated[] = $instance;
5050
}
5151

@@ -131,20 +131,21 @@ protected function loadParentsIntoModels(array $entities): void
131131
}
132132
});
133133

134-
$parents = $filtered ? (new EntityHydrator($parentQuery->get()->all()))->hydrate() : [];
134+
$parentModels = $filtered ? $parentQuery->get()->all() : [];
135+
$parents = (new EntityHydrator($parentModels))->hydrate();
135136
$parentMap = [];
136137
foreach ($parents as $parent) {
137138
$parentMap[$parent->type . ':' . $parent->id] = $parent;
138139
}
139140

140141
foreach ($entities as $entity) {
141142
if ($entity instanceof Page || $entity instanceof Chapter) {
142-
$key = 'book:' . $entity->getAttribute('book_id');
143-
$entity->setRelation('book', $parentMap[$key] ?? null);
143+
$key = 'book:' . $entity->getRawAttribute('book_id');
144+
$entity->setAttribute('book', $parentMap[$key] ?? null);
144145
}
145146
if ($entity instanceof Page) {
146-
$key = 'chapter:' . $entity->getAttribute('chapter_id');
147-
$entity->setRelation('chapter', $parentMap[$key] ?? null);
147+
$key = 'chapter:' . $entity->getRawAttribute('chapter_id');
148+
$entity->setAttribute('chapter', $parentMap[$key] ?? null);
148149
}
149150
}
150151
}

app/Search/SearchRunner.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,12 @@ public function searchChapter(int $chapterId, string $searchString): Collection
109109
protected function getPageOfDataFromQuery(EloquentBuilder $query, int $page, int $count): Collection
110110
{
111111
$entities = $query->clone()
112-
// ->with(array_filter($relations))
113112
->skip(($page - 1) * $count)
114113
->take($count)
115114
->get();
116115

117116
$hydrated = (new EntityHydrator($entities->all(), true, true))->hydrate();
118117

119-
// TODO - Load in books for pages/chapters efficiently (scoped to visible)
120-
// TODO - Load in chapters for pages efficiently (scoped to visible)
121-
// TODO - Load in tags efficiently
122-
123118
return collect($hydrated);
124119
}
125120

0 commit comments

Comments
 (0)