2023-07-20 23:20:46 +02:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Repository;
|
|
|
|
|
2023-07-29 16:42:27 +02:00
|
|
|
use App\Doctrine\Helpers\FieldHelper;
|
2023-07-20 23:20:46 +02:00
|
|
|
use App\Entity\Attachments\AttachmentContainingDBElement;
|
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template TEntityClass of AttachmentContainingDBElement
|
|
|
|
* @extends NamedDBElementRepository<TEntityClass>
|
|
|
|
*/
|
|
|
|
class AttachmentContainingDBElementRepository extends NamedDBElementRepository
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array This array is used to cache the results of getElementsAndPreviewAttachmentByIDs function.
|
|
|
|
*/
|
|
|
|
private array $elementsAndPreviewAttachmentCache = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to the findByIDInMatchingOrder function, but it also hints to doctrine that the master picture attachment should be fetched eagerly.
|
|
|
|
* @param array $ids
|
|
|
|
* @return array
|
2023-07-23 00:14:29 +02:00
|
|
|
* @phpstan-return array<int, TEntityClass>
|
2023-07-20 23:20:46 +02:00
|
|
|
*/
|
|
|
|
public function getElementsAndPreviewAttachmentByIDs(array $ids): array
|
|
|
|
{
|
2023-07-29 16:42:27 +02:00
|
|
|
//If no IDs are given, return an empty array
|
|
|
|
if (count($ids) === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-07-20 23:20:46 +02:00
|
|
|
//Convert the ids to a string
|
|
|
|
$cache_key = implode(',', $ids);
|
|
|
|
|
|
|
|
//Check if the result is already cached
|
|
|
|
if (isset($this->elementsAndPreviewAttachmentCache[$cache_key])) {
|
|
|
|
return $this->elementsAndPreviewAttachmentCache[$cache_key];
|
|
|
|
}
|
|
|
|
|
2023-07-29 16:42:27 +02:00
|
|
|
$qb = $this->createQueryBuilder('element')
|
|
|
|
->select('element')
|
2023-07-20 23:20:46 +02:00
|
|
|
->where('element.id IN (?1)')
|
2023-07-23 01:19:48 +02:00
|
|
|
//Order the results in the same order as the IDs in the input array (mysql supports this native, for SQLite we emulate it)
|
2023-07-29 16:42:27 +02:00
|
|
|
->setParameter(1, $ids);
|
|
|
|
|
|
|
|
//Order the results in the same order as the IDs in the input array
|
|
|
|
FieldHelper::addOrderByFieldParam($qb, 'element.id', 1);
|
|
|
|
|
|
|
|
$q = $qb->getQuery();
|
2023-07-20 23:20:46 +02:00
|
|
|
|
|
|
|
$q->setFetchMode($this->getEntityName(), 'master_picture_attachment', ClassMetadataInfo::FETCH_EAGER);
|
|
|
|
|
|
|
|
$result = $q->getResult();
|
|
|
|
|
|
|
|
//Cache the result
|
|
|
|
$this->elementsAndPreviewAttachmentCache[$cache_key] = $result;
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|