allow exportInterface to return NativeQuery

ref #9
This commit is contained in:
Julien Fastré 2016-04-25 21:57:23 +02:00
parent 5b879ae13c
commit a182dcc1a1
2 changed files with 38 additions and 21 deletions

View File

@ -57,12 +57,12 @@ interface ExportInterface extends ExportElementInterface
* what the user is allowed to see. (Do not show personal data the user
* is not allowed to see).
*
* @param QueryBuilder $qb
* @param array $requiredModifiers
* @param array $acl an array where each row has a `center` key containing the Chill\MainBundle\Entity\Center, and `circles` keys containing the reachable circles. Example: `array( array('center' => $centerA, 'circles' => array($circleA, $circleB) ) )`
* @param array $data the data from the form, if any
* @return the query to execute.
*/
public function initiateQuery(QueryBuilder $qb, array $requiredModifiers, array $acl, array $data = array());
public function initiateQuery(array $requiredModifiers, array $acl, array $data = array());
/**
* Inform which ModifiersInterface (i.e. AggregatorInterface, FilterInterface)
@ -102,11 +102,11 @@ interface ExportInterface extends ExportElementInterface
/**
* Return the results of the query builder.
*
* @param QueryBuilder $qb
* @param QueryBuilder|\Doctrine\ORM\NativeQuery $query
* @param mixed[] $data the data from the export's fomr (added by self::buildForm)
* @return mixed[] an array of results
*/
public function getResult(QueryBuilder $qb, $data);
public function getResult($query, $data);
/**

View File

@ -382,36 +382,53 @@ class ExportManager
public function generate($exportAlias, array $pickedCentersData, array $data, array $formatterData)
{
$export = $this->getExport($exportAlias);
$qb = $this->em->createQueryBuilder();
//$qb = $this->em->createQueryBuilder();
$centers = $this->getPickedCenters($pickedCentersData);
$qb = $export->initiateQuery(
$qb,
$query = $export->initiateQuery(
$this->retrieveUsedModifiers($data),
$this->buildCenterReachableScopes($centers, $export),
$data[ExportType::EXPORT_KEY]
);
//handle filters
$this->handleFilters($export, $qb, $data[ExportType::FILTER_KEY], $centers);
if ($query instanceof \Doctrine\ORM\NativeQuery) {
// throw an error if the export require other modifier, which is
// not allowed when the export return a `NativeQuery`
if (count($export->supportsModifiers()) > 0) {
throw new \LogicException("The export with alias `$exportAlias` return "
. "a `\Doctrine\ORM\NativeQuery` and supports modifiers, which is not "
. "allowed. Either the method `supportsModifiers` should return an empty "
. "array, or return a `Doctrine\ORM\QueryBuilder`");
}
} elseif ($query instanceof QueryBuilder) {
//handle filters
$this->handleFilters($export, $query, $data[ExportType::FILTER_KEY], $centers);
//handle aggregators
$this->handleAggregators($export, $query, $data[ExportType::AGGREGATOR_KEY], $centers);
$this->logger->debug('current query is '.$query->getDQL(), array(
'class' => self::class, 'function' => __FUNCTION__
));
} else {
throw new \UnexpectedValueException("The method `intiateQuery` should return "
. "a `\Doctrine\ORM\NativeQuery` or a `Doctrine\ORM\QueryBuilder` "
. "object.");
}
//handle aggregators
$this->handleAggregators($export, $qb, $data[ExportType::AGGREGATOR_KEY], $centers);
// $this->logger->debug('current query is '.$qb->getDQL(), array(
// 'class' => self::class, 'function' => __FUNCTION__
// ));
$result = $export->getResult($qb, $data[ExportType::EXPORT_KEY]);
$result = $export->getResult($query, $data[ExportType::EXPORT_KEY]);
/* @var $formatter Formatter\CSVFormatter */
$formatter = $this->getFormatter($this->getFormatterAlias($data));
$filters = array();
$aggregators = $this->retrieveUsedAggregators($data[ExportType::AGGREGATOR_KEY]);
$aggregatorsData = array();
foreach($aggregators as $alias => $aggregator) {
$aggregatorsData[$alias] = $data[ExportType::AGGREGATOR_KEY][$alias]['form'];
if ($query instanceof QueryBuilder) {
$aggregators = $this->retrieveUsedAggregators($data[ExportType::AGGREGATOR_KEY]);
foreach($aggregators as $alias => $aggregator) {
$aggregatorsData[$alias] = $data[ExportType::AGGREGATOR_KEY][$alias]['form'];
}
}
return $formatter->getResponse(