Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Override QueryBuilder alias #779

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Resources/doc/query.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,33 @@ public function indexAction(Request $request)
}
```

If you need to override the entity alias, set the entity short name.

``` php
public function indexAction(Request $request)
{
// ...

if ($request->isXmlHttpRequest()) {
$responseService = $this->get('sg_datatables.response');
$responseService->setDatatable($datatable);

$datatableQueryBuilder = $responseService->getDatatableQueryBuilder();
$datatableQueryBuilder->setEntityShortName('p');

/** @var QueryBuilder $qb */
$qb = $datatableQueryBuilder->getQb();
$qb->andWhere('createdBy.username = :username');
$qb->setParameter('username', 'root');
$qb->andWhere('p.deleted = 0');

return $responseService->getResponse();
}

// ...
}
```

## 2. Subqueries

Sometimes it is needed to count the number of rows or concatenate multiple fields.
Expand Down
27 changes: 22 additions & 5 deletions Response/DatatableQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,6 @@ public function __construct(array $requestParams, DatatableInterface $datatable)

$this->columns = $datatable->getColumnBuilder()->getColumns();

$this->selectColumns = array();
$this->searchColumns = array();
$this->orderColumns = array();
$this->joins = array();

$this->options = $datatable->getOptions();
$this->features = $datatable->getFeatures();
$this->ajax = $datatable->getAjax();
Expand All @@ -234,6 +229,11 @@ public function __construct(array $requestParams, DatatableInterface $datatable)
*/
private function initColumnArrays()
{
$this->selectColumns = array();
$this->searchColumns = array();
$this->orderColumns = array();
$this->joins = array();

foreach ($this->columns as $key => $column) {
$dql = $this->accessor->getValue($column, 'dql');
$data = $this->accessor->getValue($column, 'data');
Expand Down Expand Up @@ -776,6 +776,23 @@ private function getEntityShortName(ClassMetadata $metadata, EntityManagerInterf
return $isReservedKeyword ? "_{$entityShortName}" : $entityShortName;
}

/**
* Set entity short name.
*
* @param string $entityShortName
*
* @return $this
*/
public function setEntityShortName($entityShortName)
{
$this->entityShortName = $entityShortName;

$this->qb = $this->em->createQueryBuilder()->from($this->entityName, $this->entityShortName);
$this->initColumnArrays();

return $this;
}

/**
* Get identifier.
*
Expand Down