Files
kupshop/bundles/KupShop/ComponentsBundle/Dto/SectionsMultiFetch.php
2025-08-02 16:30:27 +02:00

46 lines
1.4 KiB
PHP

<?php
namespace KupShop\ComponentsBundle\Dto;
use KupShop\ContentBundle\Util\SliderUtil;
use Query\Operator;
use Query\QueryBuilder;
class SectionsMultiFetch
{
private array $wasCalled = [];
public function __construct(protected array &$sections, private readonly SliderUtil $sliderUtil)
{
}
public function fetchSliders(): void
{
if (array_key_exists(__FUNCTION__, $this->wasCalled)) {
return;
}
$loadedSliders = $this->sliderUtil->fetchSliders(
function (QueryBuilder $qb) {
$sectionPositions = sqlQueryBuilder()
->select('DISTINCT sis.id_slider', 'sis.position, sis.id_section')
->from('sliders_in_sections', 'sis')
->andWhere(Operator::inIntArray(array_keys($this->sections), 'sis.id_section'));
$qb->addSelect('sis.position AS section_position, sis.id_section')
->joinSubQuery('sl', $sectionPositions, 'sis', 'sl.id = sis.id_slider');
},
indexBy: 'section_position',
);
foreach ($loadedSliders as $key => $slider) {
$object = $this->sections[$slider['id_section']]->getObject();
$sliders = $object->getSliders();
$sliders[$key] = $slider;
$object->setSliders($sliders);
}
$this->wasCalled[__FUNCTION__] = true;
}
}