Skip to content

count query fails when placeholders in select #168

@greg606

Description

@greg606

Given the query

$qb->select("c, u, p, a, pd, ad, GEO_DISTANCE_BY_POSTAL_CODE('GB', :postcode, 'GB', ad.outerPostcode) AS distance");

the count query fails as it strips the select clause.

quick fix for it is like this:
from Ali/DatatableBundle/Util/Factory/Query/DoctrineBuilder.php:148

    /**
     * get total records
     * 
     * @return integer 
     */
    public function getTotalRecords()
    {
        $qb = clone $this->queryBuilder;
        $this->_addSearch($qb);
        $qb->resetDQLPart('orderBy');

        $gb = $qb->getDQLPart('groupBy');
        if (empty($gb) || !in_array($this->fields['_identifier_'], $gb))
        {
            $qb->select(" count({$this->fields['_identifier_']}), (:postcode) distance ");

            return $qb->getQuery()->getSingleResult()[1];
        }
        else
        {
            $qb->resetDQLPart('groupBy');
            $qb->select(" count(distinct {$this->fields['_identifier_']}) ");
            return $qb->getQuery()->getSingleScalarResult();
        }
    }

Of course it will break other datatables.
I think it should be catered for in more general way.
Other possible soloution - remove parameters bound.

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