mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-24 02:38:50 +02:00
Do not cache entities directly in NodesListBuilder but cache only the IDs instead
Otherwise the doctrine proxies break, and we get issues with loading the preview_images in structural Elements.
This commit is contained in:
parent
2e8cb35acc
commit
8ce5f4a796
9 changed files with 173 additions and 25 deletions
72
src/Repository/AttachmentContainingDBElementRepository.php
Normal file
72
src/Repository/AttachmentContainingDBElementRepository.php
Normal file
|
@ -0,0 +1,72 @@
|
|||
<?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;
|
||||
|
||||
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
|
||||
*/
|
||||
public function getElementsAndPreviewAttachmentByIDs(array $ids): array
|
||||
{
|
||||
//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');
|
||||
$q = $qb->select('element')
|
||||
->where('element.id IN (?1)')
|
||||
->setParameter(1, $ids)
|
||||
->getQuery();
|
||||
|
||||
$q->setFetchMode($this->getEntityName(), 'master_picture_attachment', ClassMetadataInfo::FETCH_EAGER);
|
||||
|
||||
$result = $q->getResult();
|
||||
$result = array_combine($ids, $result);
|
||||
$result = array_map(fn ($id) => $result[$id], $ids);
|
||||
|
||||
//Cache the result
|
||||
$this->elementsAndPreviewAttachmentCache[$cache_key] = $result;
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
|
@ -51,6 +51,8 @@ use ReflectionClass;
|
|||
*/
|
||||
class DBElementRepository extends EntityRepository
|
||||
{
|
||||
private array $find_elements_by_id_cache = [];
|
||||
|
||||
/**
|
||||
* Changes the ID of the given element to a new value.
|
||||
* You should only use it to undelete former existing elements, everything else is most likely a bad idea!
|
||||
|
@ -91,6 +93,38 @@ class DBElementRepository extends EntityRepository
|
|||
return $q->getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the elements with the given IDs in the same order, as they were given in the input array.
|
||||
*
|
||||
* @param array $ids
|
||||
* @return array
|
||||
*/
|
||||
public function findByIDInMatchingOrder(array $ids): array
|
||||
{
|
||||
$cache_key = implode(',', $ids);
|
||||
|
||||
//Check if the result is already cached
|
||||
if (isset($this->find_elements_by_id_cache[$cache_key])) {
|
||||
return $this->find_elements_by_id_cache[$cache_key];
|
||||
}
|
||||
|
||||
//Otherwise do the query
|
||||
$qb = $this->createQueryBuilder('element');
|
||||
$q = $qb->select('element')
|
||||
->where('element.id IN (?1)')
|
||||
->setParameter(1, $ids)
|
||||
->getQuery();
|
||||
|
||||
$result = $q->getResult();
|
||||
$result = array_combine($ids, $result);
|
||||
$result = array_map(fn ($id) => $result[$id], $ids);
|
||||
|
||||
//Cache the result
|
||||
$this->find_elements_by_id_cache[$cache_key] = $result;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function setField(AbstractDBElement $element, string $field, int $new_value): void
|
||||
{
|
||||
$reflection = new ReflectionClass($element::class);
|
||||
|
|
|
@ -65,11 +65,11 @@ class NamedDBElementRepository extends DBElementRepository
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the list of all nodes to use in a select box.
|
||||
* Returns a flattened list of all nodes.
|
||||
* @return AbstractNamedDBElement[]
|
||||
* @phpstan-return array<int, AbstractNamedDBElement>
|
||||
*/
|
||||
public function toNodesList(): array
|
||||
public function getFlatList(): array
|
||||
{
|
||||
//All nodes are sorted by name
|
||||
return $this->findBy([], ['name' => 'ASC']);
|
||||
|
|
|
@ -32,7 +32,7 @@ use RecursiveIteratorIterator;
|
|||
* @template TEntityClass of AbstractStructuralDBElement
|
||||
* @extends NamedDBElementRepository<TEntityClass>
|
||||
*/
|
||||
class StructuralDBElementRepository extends NamedDBElementRepository
|
||||
class StructuralDBElementRepository extends AttachmentContainingDBElementRepository
|
||||
{
|
||||
/**
|
||||
* @var array An array containing all new entities created by getNewEntityByPath.
|
||||
|
@ -85,7 +85,7 @@ class StructuralDBElementRepository extends NamedDBElementRepository
|
|||
* @return AbstractStructuralDBElement[] a flattened list containing the tree elements
|
||||
* @phpstan-return array<int, TEntityClass>
|
||||
*/
|
||||
public function toNodesList(?AbstractStructuralDBElement $parent = null): array
|
||||
public function getFlatList(?AbstractStructuralDBElement $parent = null): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue