QueryBuilder / Doctrine选择join groupby

时间:2015-03-31 21:38:18

标签: php mysql symfony doctrine-orm query-builder

所以最近我一直在思考,但由于缺乏doctrine2和symfony查询构建器的开发,我找不到解决这个问题的方法。

我有2张桌子: 目标:id,user_id,target_value ...... 节省:id,goal_id,金额

我需要从目标中进行选择(我的表中的所有信息都来自目标表,除了我需要从每个目标的储蓄表中制作SUM(金额),因此我可以向用户显示他为自己的目标节省了多少钱)

这是MySQL查询:

    select 

    admin_goals.created,
    admin_goals.description,
    admin_goals.goal_date,
    admin_goals.value,
    admin_goals.budget_categ,
    sum(admin_savings.value) 

    from admin_goals 
inner join admin_savings on admin_savings.goal_id=admin_goals.id 
    where admin_goals.user_id=1 
    group by admin_goals.id

它返回我想要的内容,但我不知道如何使用doctrine或query builder实现它,你能告诉我两个方面的例子吗? enter image description here 我非常感谢! enter image description here

2 个答案:

答案 0 :(得分:2)

我假设您只需要这些字段,而不是您的AdminGoals实体。在AdminGoalsRepository上,您可以执行以下操作:

public function getGoalsByUser(User $user) 
{
    $qb = $this->createQueryBuilder('goal');
    $qb->select('SUM(savings.value) AS savings_value')
       ->addSelect('goal.created')
       ->addSelect('goal.description')
       ->addSelect('goal.goalDate')
       ->addSelect('goal.value')
       ->addSelect('goal.budgetCat') //is this an entity? it will be just an ID
       ->join('goal.adminSavings', 'savings', Join::WITH))
       ->where($qb->expr()->eq('goal.user', ':user'))
       ->groupBy('goal.id')
       ->setParameter('user', $user);

    return $qb->getQuery()->getScalarResult();
}

请记住,返回对象将是一个行数组,每一行都是一个关联数组,其中的键与上面的映射一样。

修改

在更新问题之后,我将更改我建议的功能,但如果其他人希望看到差异,则会离开上面的示例。

首先,由于这是AdminSavingsAdminGoals之间的单向ManyToOne,因此自定义查询应位于AdminSavingsRepository不像上面)。此外,由于您需要aggregated field,这将“中断”您的一些数据提取。当您不仅仅是渲染模板时,尽量保持OOP。

public function getSavingsByUser(User $user)
{
    $qb = $this->createQueryBuilder('savings');
    //now we can use the expr() function
    $qb->select('SUM(savings.value) AS savings_value')
       ->addSelect('goal.created')
       ->addSelect('goal.description')
       ->addSelect('goal.goalDate')
       ->addSelect('goal.value')
       ->addSelect('goal.budgetCat') //this will be just an ID
       ->join('savings.goal', 'goal', Join::WITH))
       ->where($qb->expr()->eq('goal.user', ':user'))
       ->groupBy('goal.id')
       ->setParameter('user', $user);

       return $qb->getQuery()->getScalarResult();
}

加成

public function FooAction($args) 
{
    $em = $this->getDoctrine()->getManager();
    $user = $this->getUser();
    //check if user is User etc depends on your config
    ...

    $savings = $em->getRepository('AcmeBundle:AdminSavings')->getSavingsByUser($user);

    foreach($savings as $row) {
        $savings = $row['savings_value'];
        $goalId =  $row['id'];  
        $goalCreated = $row['created'];
        [...]
    }
    [...]
}

答案 1 :(得分:0)

如果你使用createQuery(),那么你可以这样做:

    $dqlStr = <<<"DSQL"
select 
admin_goals.created,
admin_goals.description,
admin_goals.goal_date,
admin_goals.value,
admin_goals.budget_categ,
sum(admin_savings.value)
from admin_goals 
inner join admin_savings on admin_savings.goal_id=admin_goals.id 
where admin_goals.user_id=1 
group by admin_goals.id
DSQL;

    $em = $this->getDoctrine()->getManager();
    $query = $em->createQuery($dqlStr);
    $query->getResult();

另一方面,如果您想使用createQueryBuilder(),可以查看以下链接:http://inchoo.net/dev-talk/symfony2-dbal-querybuilder/