tetranz / select2entity-bundle

A Symfony2 bundle that integrates Select2 as a drop-in replacement for a standard entity field on a Symfony form.
MIT License
218 stars 110 forks source link

[EntitiesToPropertyTransformer] Entities result order issue #167

Open jkgroupe opened 4 years ago

jkgroupe commented 4 years ago

Hi,

I have an issue when I select tags in a specific order, the entities result is reordered by id and not by the specific selection

Values order [5,2,3,1,4]

Result from transformer [1,2,3,4,5]

To resolve this issue with beberlei/DoctrineExtensions package

// get multiple entities with one query
$entities = $this->em->createQueryBuilder()
       ->select('entity')
       ->from($this->className, 'entity')
       ->where('entity.' . $this->primaryKey . ' IN (:ids)')
       ->setParameter('ids', $values)
       ->orderBy('FIELD(entity.' . $this->primaryKey . ', :ids)') // Ordering by ids
       ->getQuery()
       ->getResult();

Thanks