From 21cc40b27c82e9ab10c03be198ec00353b8a1861 Mon Sep 17 00:00:00 2001 From: IT-Talents Date: Fri, 18 May 2018 14:29:21 +0200 Subject: [PATCH 1/2] add method to set the entity short name, reset the querybuilder and reinit column arrays --- Response/DatatableQueryBuilder.php | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/Response/DatatableQueryBuilder.php b/Response/DatatableQueryBuilder.php index ee2c5fca..71dbaa70 100644 --- a/Response/DatatableQueryBuilder.php +++ b/Response/DatatableQueryBuilder.php @@ -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(); @@ -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'); @@ -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. * From 7da4ff0bfce1cf6c1e809f1c37d872477d6adb3f Mon Sep 17 00:00:00 2001 From: IT-Talents Date: Fri, 18 May 2018 14:46:43 +0200 Subject: [PATCH 2/2] add documentation --- Resources/doc/query.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Resources/doc/query.md b/Resources/doc/query.md index aba9af1e..4033107d 100644 --- a/Resources/doc/query.md +++ b/Resources/doc/query.md @@ -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.