Open Voltra opened 3 months ago
Preloading related data implementation
The code has been added to reduce the loading time and improve the performance for the page-blocks.blade.php
file in the system by preloading the required data.
New helper file inclusion
A new file Helpers.php
has been added, including an additional function to efficiently manage and group data. It simplifies the process of grouping an array of associative arrays by a specific key.
New method addition to the PageBlock class
The preloadRelatedData
method has been added to the PageBlock
component (located in the PageBlock.php
file). This new method further enhances data handling capabilities, making the system smoother and faster by managing the loading of necessary data ahead of time.
Some components, like media-based components, accept either an ID or a
Model
instance as their input. In the latter case, they simply use the instance as is. In the former, they need to load the model from the database using the ID.As such, using said components with Fabricator (or any kind of builder / repeater) gives us an N+1 query problem. To fix this we could batch the data loading. That is the feature this PR provides.
Block::preloadRelatedData(Page $page, array &$blocks)
is a public static method that is called once per block type when rendering a page. It's meant to be used for batch loading data or any kind of action that is meant to be done once per page per block type.For instance, let's say we have a
TextImageBlock
class that extends fromPageBlock
and contains animage
field (e.g. file picker, curator, media-library). Said field only stores the ID of the media model in the database, and the display component accepts either an ID or aModel
instance.In this scenario, we can implement the block's
preloadRelatedData
to batch-load the media models:This way we only have 1 database query for the medias for the whole page.