Skip to content

Commit

Permalink
Add groupByFields to DoctrineListRepresentationFactory (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-rath authored Nov 7, 2022
1 parent 47c716f commit 15506a9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
12 changes: 9 additions & 3 deletions ListRepresentation/DoctrineListRepresentationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ public function __construct(
}

/**
* @param mixed[] $filters
* @param mixed[] $parameters
* @param array<string, mixed> $filters
* @param array<string, mixed> $parameters
* @param string[] $includedFields
* @param string[] $groupByFields
*/
public function createDoctrineListRepresentation(
string $resourceKey,
array $filters = [],
array $parameters = [],
array $includedFields = []
array $includedFields = [],
array $groupByFields = []
): PaginatedRepresentation {
/** @var DoctrineFieldDescriptor[] $fieldDescriptors */
$fieldDescriptors = $this->fieldDescriptorFactory->getFieldDescriptors($resourceKey);
Expand All @@ -74,6 +76,10 @@ public function createDoctrineListRepresentation(
$listBuilder->addSelectField($fieldDescriptors[$field]);
}

foreach ($groupByFields as $field) {
$listBuilder->addGroupBy($fieldDescriptors[$field]);
}

$items = $listBuilder->execute();

// sort the items to reflect the order of the given ids if the list was requested to include specific ids
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
interface DoctrineListRepresentationFactoryInterface
{
/**
* @param mixed[] $filters
* @param mixed[] $parameters
* @param array<string, mixed> $filters
* @param array<string, mixed> $parameters
* @param string[] $includedFields
* @param string[] $groupByFields
*/
public function createDoctrineListRepresentation(
string $resourceKey,
array $filters = [],
array $parameters = [],
array $includedFields = []
array $includedFields = [],
array $groupByFields = []
): PaginatedRepresentation;
}
18 changes: 15 additions & 3 deletions ListRepresentation/DoctrineNestedListRepresentationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,21 @@ public function __construct(
}

/**
* @param mixed[] $filters
* @param mixed[] $parameters
* @param array<string, mixed> $filters
* @param array<string, mixed> $parameters
* @param int|string|null $parentId
* @param int[]|string[] $expandedIds
* @param string[] $includedFields
* @param string[] $groupByFields
*/
public function createDoctrineListRepresentation(
string $resourceKey,
array $filters = [],
array $parameters = [],
$parentId = null,
array $expandedIds = []
array $expandedIds = [],
array $includedFields = [],
array $groupByFields = []
): CollectionRepresentation {
/** @var DoctrineFieldDescriptor[] $fieldDescriptors */
$fieldDescriptors = $this->fieldDescriptorFactory->getFieldDescriptors($resourceKey);
Expand All @@ -72,6 +76,14 @@ public function createDoctrineListRepresentation(
$listBuilder->where($fieldDescriptors[$key], $value);
}

foreach ($includedFields as $field) {
$listBuilder->addSelectField($fieldDescriptors[$field]);
}

foreach ($groupByFields as $field) {
$listBuilder->addGroupBy($fieldDescriptors[$field]);
}

// disable pagination to simplify tree handling and select tree related properties that are used below
$listBuilder->limit(\PHP_INT_MAX);
$listBuilder->addSelectField($fieldDescriptors['lft']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@
interface DoctrineNestedListRepresentationFactoryInterface
{
/**
* @param mixed[] $filters
* @param mixed[] $parameters
* @param array<string, mixed> $filters
* @param array<string, mixed> $parameters
* @param int|string|null $parentId
* @param int[]|string[] $expandedIds
* @param string[] $includedFields
* @param string[] $groupByFields
*/
public function createDoctrineListRepresentation(
string $resourceKey,
array $filters = [],
array $parameters = [],
$parentId = null,
array $expandedIds = []
array $expandedIds = [],
array $includedFields = [],
array $groupByFields = []
): CollectionRepresentation;
}

0 comments on commit 15506a9

Please sign in to comment.