我尝试从本机查询中使用querybuilder创建查询。
干净的SQL查询
SELECT p.param, c.value, a.action_date AS last_run FROM search_conditions c
RIGHT JOIN search_parameters p ON p.id = c.parameter_id
LEFT JOIN (
SELECT condition_id, MAX(action_date) AS action_date
FROM search_actions group by condition_id
) a ON a.condition_id = c.id
WHERE p.param != \'status\' AND c.value IS NOT NULL
所以我想从search_action表中选择MAX()
值。
SearchParameterRepository
public function getActionsConditionsWithMaxDateQB()
{
$q = $this->createQueryBuilder('p');
$q->select('p.param', 'c.value')
->leftJoin('p.searchConditions', 'c')
->leftJoin('c.searchActions', 'a', Expr\Join::WITH, $q->expr()->max('a.actionDate') );
此抛出错误
[语法错误]行0,列-1:错误:预期=,<,<=,<>,>,> =, !=,以字符串结尾。
设置使用的字段名称
SearchAction
- $condition (ManyToOne, SearchCondition)
- $parameter (ManyToOne, SearchParameter)
- $actionDate
SearchParameter
- $param
SearchCondition
- $value
问题
如何使用QueryBuilder构建该查询?