. */ declare(strict_types=1); namespace App\Repository; use App\Doctrine\Helpers\FieldHelper; use App\Entity\Attachments\AttachmentContainingDBElement; use Doctrine\ORM\Mapping\ClassMetadata; /** * @template TEntityClass of AttachmentContainingDBElement * @extends NamedDBElementRepository * @see \App\Tests\Repository\AttachmentContainingDBElementRepositoryTest */ 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 * @phpstan-return array */ public function getElementsAndPreviewAttachmentByIDs(array $ids): array { //If no IDs are given, return an empty array if (count($ids) === 0) { return []; } //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]; } $qb = $this->createQueryBuilder('element') ->select('element') ->where('element.id IN (?1)') //Order the results in the same order as the IDs in the input array (mysql supports this native, for SQLite we emulate it) ->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(); $q->setFetchMode($this->getEntityName(), 'master_picture_attachment', ClassMetadata::FETCH_EAGER); $result = $q->getResult(); //Cache the result $this->elementsAndPreviewAttachmentCache[$cache_key] = $result; return $result; } }