Fixed field ordering on SQLite

This commit is contained in:
Jan Böhmer 2023-07-29 16:42:27 +02:00
parent 5c30210534
commit 62b1e33616
7 changed files with 214 additions and 143 deletions

View file

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace App\Repository;
use App\Doctrine\Helpers\FieldHelper;
use App\Entity\Attachments\AttachmentContainingDBElement;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
@ -45,6 +46,11 @@ class AttachmentContainingDBElementRepository extends NamedDBElementRepository
*/
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);
@ -53,13 +59,16 @@ class AttachmentContainingDBElementRepository extends NamedDBElementRepository
return $this->elementsAndPreviewAttachmentCache[$cache_key];
}
$qb = $this->createQueryBuilder('element');
$q = $qb->select('element')
$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)
->orderBy('FIELD(element.id, ?1)')
->setParameter(1, $ids)
->getQuery();
->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', ClassMetadataInfo::FETCH_EAGER);

View file

@ -41,7 +41,9 @@ declare(strict_types=1);
namespace App\Repository;
use App\Doctrine\Helpers\FieldHelper;
use App\Entity\Base\AbstractDBElement;
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
use Doctrine\ORM\EntityRepository;
use ReflectionClass;
@ -101,6 +103,11 @@ class DBElementRepository extends EntityRepository
*/
public function findByIDInMatchingOrder(array $ids): array
{
//If no IDs are given, return an empty array
if (count($ids) === 0) {
return [];
}
$cache_key = implode(',', $ids);
//Check if the result is already cached
@ -110,12 +117,14 @@ class DBElementRepository extends EntityRepository
//Otherwise do the query
$qb = $this->createQueryBuilder('element');
$q = $qb->select('element')
$qb->select('element')
->where('element.id IN (?1)')
->setParameter(1, $ids)
//Order the results in the same order as the IDs in the input array (mysql supports this native, for SQLite we emulate it)
->orderBy('FIELD(element.id, ?1)')
->getQuery();
->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();
$result = $q->getResult();