shuba.ivan
shuba.ivan

Reputation: 4061

How to add custom column to my select in Query builder?

I have a function selecting some columns from a table, using QueryBuilder, and I would like to add a custom column to the resulting query.

like this

SELECT u.id, "paid" as type FROM users as u WHERE u.deleted_at IS NOT NULL

This is what I have:

    /**
 * {@inheritdoc}
 */
public function getPaidOrderAnalytics(
    Agents $agent,    
    ParamFetcher $paramFetcher,
    $dateFrom,
    $dateTo
)
{
    $qb = $this->getEntityManager()->createQueryBuilder();
    $qb
        ->select('
             DISTINCT(o.id) as _id,
             o.createdAt as order_created_at,
             o.publicCost as public_cost,
             o.orderStatus as order_status,
             srv.services as service_name
        ')
        ->addSelect('
             "paid" as type

        ')
        ->from('ArtelProfileBundle:Orders', 'o')
        ->leftJoin('o.services', 'srv')
        ->leftJoin('o.leadService', 'ls')
        ->leftJoin('ls.lead', 'l')
        ->andWhere('l.agent = :agent')
        ->setParameter(':agent', $agent)
        ->orderBy('o.' . $paramFetcher->get('sort_by'), $paramFetcher->get('sort_order'))
        ->setFirstResult($paramFetcher->get('count') * ($paramFetcher->get('page') - 1))
        ->setMaxResults($paramFetcher->get('count'));

    $query = $qb->getQuery();
    $results = $query->getResult();

    return $results;
}

have error

[Syntax Error] line 0, col 270: Error: Expected IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression | "(" Subselect ")" | CaseExpression, got '"'

How to add custom column to my select in Query builder ?

Upvotes: 2

Views: 6249

Answers (2)

Matteo
Matteo

Reputation: 39390

Try changing this:

        ->addSelect('
         "paid" as type

    ')

with:

        ->addSelect('
         \'paid\' as type

    ')

I successfully dump the value as example

    var_dump($results[0]['type']);

Hope this help

Upvotes: 2

Chuck Norris
Chuck Norris

Reputation: 1125

Try like this :

$qb = $this->createQueryBuilder('e');
$qb->select('e')
   ->addSelect('e.paid as HIDDEN paid')

Upvotes: 0

Related Questions