mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-05-07 17:40:57 +00:00
Eager loading for titles
This commit is contained in:
parent
3e656efb00
commit
90a8070518
1 changed files with 21 additions and 0 deletions
|
@ -106,6 +106,10 @@ class ApiEntityListFormatter
|
||||||
*/
|
*/
|
||||||
public function format(): array
|
public function format(): array
|
||||||
{
|
{
|
||||||
|
if ($this->includeRelatedTitles) {
|
||||||
|
$this->loadRelatedTitles();
|
||||||
|
}
|
||||||
|
|
||||||
$results = [];
|
$results = [];
|
||||||
|
|
||||||
foreach ($this->list as $item) {
|
foreach ($this->list as $item) {
|
||||||
|
@ -115,6 +119,23 @@ class ApiEntityListFormatter
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Eager load the related book and chapter data when needed.
|
||||||
|
*/
|
||||||
|
protected function loadRelatedTitles(): void
|
||||||
|
{
|
||||||
|
$pages = collect($this->list)->filter(fn($item) => $item instanceof Page);
|
||||||
|
|
||||||
|
foreach ($this->list as $entity) {
|
||||||
|
if (method_exists($entity, 'book')) {
|
||||||
|
$entity->load('book');
|
||||||
|
}
|
||||||
|
if ($entity instanceof Page && $entity->chapter_id) {
|
||||||
|
$entity->load('chapter');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format a single entity item to a plain array.
|
* Format a single entity item to a plain array.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue