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

Setting the entity alias in the DatatableQueryBuilder #778

@ghost

Description

It would be nice if there was any way overriding the entity alias (currently defined by the method getEntityShortName). Sometimes it's necessary to manipulate the QueryBuilder like explained in the docs, but what if that is done using methods from a service which uses another alias?

Additionally in my special case the entity is named Member. The problem here seems to be, that MEMBER is a reserved keyword of DQL (not SQL, which is already checked in the method getEntityShortName), for an example look here. This leads to syntax errors in the DQL query.

I came up with a quick solution by adding a method to the DatatableQueryBuilder:

public function setEntityShortName($entityShortName)
{
    $this->entityShortName = $entityShortName;
    
    // reset qb and reinitialize columns
    $this->qb = $this->em->createQueryBuilder()->from($this->entityName, $this->entityShortName);
    $this->initColumnArrays();

    return $this;
}

private function initColumnArrays()
{
    $this->selectColumns = array();
    $this->searchColumns = array();
    $this->orderColumns = array();
    $this->joins = array();

    foreach ($this->columns as $key => $column) {
    ....
}

Use this method to override the alias in the controller:

$datatableQb = $responseService->getDatatableQueryBuilder();
$datatableQb->setEntityShortName('m');

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions