Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Feb 8, 2018
1 parent db3b4b4 commit 6f201dd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/Configuration/ViewConfigPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ private function processSortingConfig(array $backendConfig)
throw new \InvalidArgumentException(sprintf('If defined, the second value of the "sort" option of the "%s" view of the "%s" entity can only be "ASC" or "DESC".', $view, $entityName));
}

if (isset($entityConfig[$view]['fields'][$sortConfig['field']]) && true === $entityConfig[$view]['fields'][$sortConfig['field']]['virtual']) {
$isSortedByDoctrineAssociation = false !== strpos($sortConfig['field'], '.');
if (!$isSortedByDoctrineAssociation && (isset($entityConfig[$view]['fields'][$sortConfig['field']]) && true === $entityConfig[$view]['fields'][$sortConfig['field']]['virtual'])) {
throw new \InvalidArgumentException(sprintf('The "%s" field cannot be used in the "sort" option of the "%s" view of the "%s" entity because it\'s a virtual property that is not persisted in the database.', $sortConfig['field'], $view, $entityName));
}

Expand Down
20 changes: 11 additions & 9 deletions src/Search/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,18 @@ public function createSearchQueryBuilder(array $entityConfig, $searchQuery, $sor
->from($entityConfig['class'], 'entity')
;

$entitiesAlreadyJoined = array();
$isSortedByDoctrineAssociation = false !== strpos($sortField, '.');
if ($isSortedByDoctrineAssociation) {
list($associatedEntityName, $associatedFieldName) = explode('.', $sortField);
$queryBuilder->leftJoin('entity.'.$associatedEntityName, $associatedFieldName);
$entitiesAlreadyJoined[] = $associatedEntityName;
}

$isSearchQueryNumeric = is_numeric($searchQuery);
$isSearchQuerySmallInteger = (is_int($searchQuery) || ctype_digit($searchQuery)) && $searchQuery >= -32768 && $searchQuery <= 32767;
$isSearchQueryInteger = (is_int($searchQuery) || ctype_digit($searchQuery)) && $searchQuery >= -2147483648 && $searchQuery <= 2147483647;
$isSearchQueryUuid = 1 === preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i', $searchQuery);
$lowerSearchQuery = mb_strtolower($searchQuery);

$queryParameters = array();
$entitiesAlreadyJoined = array();
foreach ($entityConfig['search']['fields'] as $fieldName => $metadata) {
$entityName = 'entity';
if (false !== strpos($fieldName, '.')) {
list($associatedEntityName, $associatedFieldName) = explode('.', $fieldName);
list($associatedEntityName, $associatedFieldName) = explode('.', $fieldName);
if (!in_array($associatedEntityName, $entitiesAlreadyJoined)) {
$queryBuilder->leftJoin('entity.'.$associatedEntityName, $associatedEntityName);
$entitiesAlreadyJoined[] = $associatedEntityName;
Expand Down Expand Up @@ -151,6 +144,15 @@ public function createSearchQueryBuilder(array $entityConfig, $searchQuery, $sor
$queryBuilder->andWhere($dqlFilter);
}

$isSortedByDoctrineAssociation = false !== strpos($sortField, '.');
if ($isSortedByDoctrineAssociation) {
list($associatedEntityName, $associatedFieldName) = explode('.', $sortField);
if (!in_array($associatedEntityName, $entitiesAlreadyJoined)) {
$queryBuilder->leftJoin('entity.'.$associatedEntityName, $associatedFieldName);
$entitiesAlreadyJoined[] = $associatedEntityName;
}
}

if (null !== $sortField) {
$queryBuilder->orderBy(sprintf('%s%s', $isSortedByDoctrineAssociation ? '' : 'entity.', $sortField), $sortDirection ?: 'DESC');
}
Expand Down

0 comments on commit 6f201dd

Please sign in to comment.