Skip to content

Commit

Permalink
Support for sum aggregation function in Nextras DataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
milsorm authored and paveljanda committed Mar 21, 2021
1 parent a661bf8 commit e4ae5d2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/AggregationFunction/FunctionSum.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Doctrine\ORM\QueryBuilder;
use Nette\Database\Table\Selection;
use Nette\Utils\Strings;
use Nextras\Orm\Collection\DbalCollection;
use Symfony\Component\ErrorHandler\Debug;
use Tracy\Debugger;
use Ublaboo\DataGrid\Utils\PropertyAccessHelper;

class FunctionSum implements ISingleColumnAggregationFunction
Expand Down Expand Up @@ -51,7 +54,7 @@ public function getFilterDataType(): string


/**
* @param Fluent|QueryBuilder|Collection|Selection $dataSource
* @param Fluent|QueryBuilder|Collection|Selection|DbalCollection $dataSource
*/
public function processDataSource($dataSource): void
{
Expand Down Expand Up @@ -82,6 +85,11 @@ public function processDataSource($dataSource): void
return true;
});
}

if ( $dataSource instanceof DbalCollection) {
foreach( $dataSource->fetchAll() as $item )
$this->result += $item->getValue( $this->column );
}
}


Expand Down
13 changes: 12 additions & 1 deletion src/DataSource/NextrasDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Nette\Utils\Strings;
use Nextras\Orm\Collection\Expression\LikeExpression;
use Nextras\Orm\Collection\ICollection;
use Ublaboo\DataGrid\AggregationFunction\IAggregatable;
use Ublaboo\DataGrid\AggregationFunction\IAggregationFunction;
use Ublaboo\DataGrid\Exception\DataGridDateTimeHelperException;
use Ublaboo\DataGrid\Filter\FilterDate;
use Ublaboo\DataGrid\Filter\FilterDateRange;
Expand All @@ -19,7 +21,7 @@
use Ublaboo\DataGrid\Utils\Sorting;
use UnexpectedValueException;

class NextrasDataSource extends FilterableDataSource implements IDataSource
class NextrasDataSource extends FilterableDataSource implements IDataSource, IAggregatable
{

/**
Expand Down Expand Up @@ -341,4 +343,13 @@ private function prepareColumn(string $column): string

return $column;
}

/**
* @param IAggregationFunction $function
* @return void
*/
public function processAggregation(IAggregationFunction $function): void
{
$function->processDataSource(clone $this->dataSource);
}
}

0 comments on commit e4ae5d2

Please sign in to comment.