我有一个函数从表中选择一些列,使用QueryBuilder,我想在结果查询中添加一个自定义列。
像这样SELECT u.id, "paid" as type FROM users as u WHERE u.deleted_at IS NOT NULL
这就是我所拥有的:
/**
* {@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;
}
有错误
[Syntax Error] line 0, col 270: Error: Expected IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression | "(" Subselect ")" | CaseExpression, got '"'
如何在“查询”构建器中的选择中添加自定义列?
答案 0 :(得分:2)
尝试更改此内容:
[DataContract]
public class Demo : DynamicObject
{
// This property is ignore in api response
[DataMember] OR [JsonProperty]
public int Prop1 {get; set;}
}
使用:
->addSelect('
"paid" as type
')
我成功转储了值作为示例
->addSelect('
\'paid\' as type
')
希望这个帮助
答案 1 :(得分:-1)
试试这样:
$qb = $this->createQueryBuilder('e');
$qb->select('e')
->addSelect('e.paid as HIDDEN paid')