diff --git a/src/Oro/Bundle/FormBundle/Autocomplete/SearchHandler.php b/src/Oro/Bundle/FormBundle/Autocomplete/SearchHandler.php index 74eae0bbf96..a1e1ffb7e09 100644 --- a/src/Oro/Bundle/FormBundle/Autocomplete/SearchHandler.php +++ b/src/Oro/Bundle/FormBundle/Autocomplete/SearchHandler.php @@ -170,7 +170,24 @@ protected function searchEntities($search, $firstResult, $maxResults) $resultEntities = []; if ($entityIds) { - $resultEntities = $this->getEntitiesByIds($entityIds); + $unsortedEntities = $this->getEntitiesByIds($entityIds); + + /** + * We need to sort entities in the same order given by method searchIds. + * + * @todo Should be not necessary after implementation of BAP-5691. + */ + $entityByIdHash = []; + + foreach ($unsortedEntities as $entity) { + $entityByIdHash[$this->getPropertyValue($this->idFieldName, $entity)] = $entity; + } + + foreach ($entityIds as $entityId) { + if (isset($entityByIdHash[$entityId])) { + $resultEntities[] = $entityByIdHash[$entityId]; + } + } } return $resultEntities; diff --git a/src/Oro/Bundle/FormBundle/Tests/Unit/Autocomplete/SearchHandlerTest.php b/src/Oro/Bundle/FormBundle/Tests/Unit/Autocomplete/SearchHandlerTest.php index a2c91a4cfab..eb630e180a3 100644 --- a/src/Oro/Bundle/FormBundle/Tests/Unit/Autocomplete/SearchHandlerTest.php +++ b/src/Oro/Bundle/FormBundle/Tests/Unit/Autocomplete/SearchHandlerTest.php @@ -283,13 +283,16 @@ public function searchDataProvider() 'getResult', array(), array( + /** + * test sorting works correct + */ + array(self::TEST_ID_FIELD => 3,'name' => 'Jack'), $this->createMockEntity( array(self::TEST_ID_FIELD => 1, 'name' => 'John', 'email' => 'john@example.com') ), $this->createMockEntity( array(self::TEST_ID_FIELD => 2, 'name' => 'Jane', 'email' => 'jane@example.com') - ), - array(self::TEST_ID_FIELD => 3,'name' => 'Jack'), + ), $this->createStubEntityWithProperties( array( self::TEST_ID_FIELD => 4, @@ -337,11 +340,14 @@ public function searchDataProvider() 'getResult', array(), array( + /** + * test sorting works correct + */ $this->createMockEntity( - array(self::TEST_ID_FIELD => 1, 'name' => 'John', 'email' => 'john@example.com') + array(self::TEST_ID_FIELD => 2, 'name' => 'Jane', 'email' => 'jane@example.com') ), $this->createMockEntity( - array(self::TEST_ID_FIELD => 2, 'name' => 'Jane', 'email' => 'jane@example.com') + array(self::TEST_ID_FIELD => 1, 'name' => 'John', 'email' => 'john@example.com') ) ) ),