douzoudang1511 2016-09-05 09:33
浏览 97
已采纳

如何在“查询”构建器中向我的选择中添加自定义列?

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 ?

  • 写回答

2条回答 默认 最新

  • doujiaozhao2489 2016-09-05 11:56
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改