【问题标题】:How convert SQL query to Doctrine2?如何将 SQL 查询转换为 Doctrine2?
【发布时间】:2015-03-13 15:57:05
【问题描述】:

我想在 Doctrine2 中转换它。

类似于这些问题:
Symfony2 / Doctrine2 - How to convert this SQL request with QueryBuilder?
How to convert a complex MySQL Query to Doctrine2
Convert SQL to Doctrine 2 Query Builder or DQL by using NOT IN?

有可能吗?

SELECT 
SUM(q.amount)

FROM(
 SELECT  amount
    FROM expense_report e0_
    WHERE
        (e0_.is_account_transfer = 0)
            AND (e0_.remove_date IS NULL)
    LIMIT 0, 2) q

谢谢

【问题讨论】:

  • (通过帮助和改进审核队列) - 改进了标题,添加了对类似问题的引用
  • 嗨@user783029,你觉得我的解决方案怎么样?

标签: php sql symfony doctrine-orm dql


【解决方案1】:

您可以使用此自定义存储库方法归档您的问题:

public function getAmount()
{
    $qb = $this->createQueryBuilder("q");
    return
        $qb
            ->select("sum(q.amount)")
            ->where($qb->expr()->isNull('q.remove_date'))
            ->andWhere('q.is_account_transfer = 0')
            ->setFirstResult(0)
            ->setMaxResults(2)
            ->getQuery()
            ->getSingleScalarResult();
}

并简单地使用它,例如,在控制器方法中:

public function testAction()
{
   ....
    $totalAmount = $this->getDoctrine()
                        ->getRepository("AcmeDemoBundle:ExpenseReport")
                        ->getAmount();
    ...
}

希望有帮助

【讨论】:

  • 对不起@user783029 你是对的,我还没有测试主要的东西......目前我还没有任何新的想法......如果你找到了或者我/你会找到一个解决方案告诉我!
猜你喜欢
  • 1970-01-01
  • 2020-07-19
  • 2021-10-11
  • 2017-10-29
  • 2015-04-21
  • 2019-04-24
  • 2016-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多