如何在其他查询中使用QueryBuilder格式化查询重用格式化的Doctrine 2

问题描述 投票:0回答:1

我已经使用doctrine查询构建器格式化了查询。它有点复杂,因为在那个查询中有一些我自己定义的学说函数和学说扩展。同样非常重要的是要提到这个查询使用过滤器,这需要一些预先选择的值。问题是当我想从该查询中计算记录时。当然我可以写计数(结果),但它对性能非常不利。

我尝试用我自己的预定义查询作为源创建新的SQL查询,但是没有注入参数的问题。

格式化的查询以及我想要计算的结果:

private function getCategoriesQuery(Filter $filter = null, Sort $sort = null, Pagination $pagination = null)
{
        $qb = $this->createQueryBuilder('c')
            ->select('c.id as id')
            ->addSelect('c.title as title')
            ->addSelect('DATE_FORMAT(c.updationDate, \'%Y-%m-%d %H:%i:%S\') as updationDate')
            ->addSelect('DATE_FORMAT(c.creationDate, \'%Y-%m-%d %H:%i:%S\') as creationDate')
            ->addSelect('coalesce(sum(r.amount), 0.00) as total')
            ->addSelect('round(AveragePerMonth(coalesce(sum(r.amount), 0.00), c.creationDate, CURRENT_DATE()), 2) as averagePerMonth')
            ->leftJoin('c.records', 'r')
            ->groupBy('c.id')
            ->andWhere('c.user = :user')->setParameter('user', $this->user);

        if ($filter) $this->applyFilters($filter, $qb);
        if ($sort) $this->applySort($sort, $qb);
        if ($pagination) $this->applyPagination($pagination, $qb);

        return $qb->getQuery();
}

我试过了:

public function countUserCategories(Filter $filter = null)
{
        $rsm = new ResultSetMapping();
        $sql = $this->_em->createNativeQuery('select count(*) from ('.$this->getCategoriesQuery($filter)->getSQL().') as src', $rsm);
        return $sql->getSingleScalarResult();
}

如果有可能将这些查询结合起来,如果有可能我可以做到这一点,我期待答案。谢谢

ApplyFilter方法:

private function applyFilters(Filter $filter, QueryBuilder $qb)
{
        if ($filter->getCategories()) {
            $qb->andWhere('c.id IN(:categories)')->setParameter('categories', $filter->getCategories());
        }

        if ($filter->getEndDate()) {
            $qb->andWhere('r.date <= :endDate')->setParameter('endDate', $filter->getEndDate());
        }

        if ($filter->getStartDate()) {
            $qb->andWhere('r.date >= :startDate')->setParameter('startDate', $filter->getStartDate());
        }

        if ($filter->getStartTotal() !== null) {
            $qb->andHaving('total >= :startTotal')->setParameter('startTotal', $filter->getStartTotal());
        }

        if ($filter->getEndTotal() !== null) {
            $qb->andHaving('total <= :endTotal')->setParameter('endTotal', $filter->getEndTotal());
        }

        if ($filter->getStartAveragePerMonth() !== null) {
            $qb->andHaving('averagePerMonth >= :startAveragePerMonth')->setParameter('startAveragePerMonth', $filter->getStartAveragePerMonth());
        }

        if ($filter->getEndAveragePerMonth() !== null) {
            $qb->andHaving('averagePerMonth <= :endAveragePerMonth')->setParameter('endAveragePerMonth', $filter->getEndAveragePerMonth());
        }
}

建议的方法来解决这个问题,但返回响应:The query returned multiple rows. Change the query or use a different result function like getScalarResult().

public function countUserCategories(Filter $filter = null)
{
        return (clone $this->getCategoriesQueryBuilder($filter))
            ->resetDQLPart('select')
            ->select('COUNT(c)')
            ->getQuery()
            ->getSingleScalarResult();
}

UPDATE

我通过使用SQL查询构建器来完成这项工作

查询构建器创建

private function getCategoriesQueryBuilder(Filter $filter = null, Sort $sort = null, Pagination $pagination = null)
{
        $qb = $this->_em->getConnection()->createQueryBuilder()
            ->select('c.id AS id')
            ->addSelect('c.title AS title')
            ->addSelect('DATE_FORMAT(c.updation_date, \'%Y-%m-%d %H:%i:%S\') AS updationDate')
            ->addSelect('DATE_FORMAT(c.creation_date, \'%Y-%m-%d %H:%i:%S\') AS creationDate')
            ->addSelect('COALESCE(SUM(r.amount), 0.00) AS total')
            ->addSelect('(SELECT ROUND(COALESCE(SUM(r.amount), 0.00) / COUNT(DISTINCT(DATE_FORMAT(calendar.date, \'%Y-%m\'))), 2)
                                FROM calendar as calendar
                                WHERE calendar.date BETWEEN DATE_FORMAT(c.creation_date, \'%Y-%m-%d\') AND DATE_FORMAT(CURRENT_DATE(), \'%Y-%m-%d\')) 
                                AS averagePerMonth')
            ->from('category', 'c')
            ->leftJoin('c', 'record', 'r', 'r.category_id = c.id')
            ->join('c', 'users', 'u', 'u.id = c.user_id')
            ->where('u.id = :userId')->setParameter('userId', $this->user->getId())
            ->groupBy('c.id');

        if ($sort) $this->applySort($sort, $qb);
        if ($pagination) $this->applyPagination($pagination, $qb);
        if ($filter) $this->applyFilter($filter, $qb);

        return $qb;
}

过滤

private function applyFilter(Filter $filter, QueryBuilder $qb)
{
        if ($filter->getCategories()) {
            $qb->andWhere('c.id IN(:categories)')->setParameter('categories', $filter->getCategories());
        }

        if ($filter->getEndDate()) {
            $qb->andWhere('r.date <= :endDate')->setParameter('endDate', $filter->getEndDate());
        }

        if ($filter->getStartDate()) {
            $qb->andWhere('r.date >= :startDate')->setParameter('startDate', $filter->getStartDate());
        }

        if ($filter->getStartTotal() !== null) {
            $qb->andHaving('total >= :startTotal')->setParameter('startTotal', $filter->getStartTotal());
        }

        if ($filter->getEndTotal() !== null) {
            $qb->andHaving('total <= :endTotal')->setParameter('endTotal', $filter->getEndTotal());
        }

        if ($filter->getStartAveragePerMonth() !== null) {
            $qb->andHaving('averagePerMonth >= :startAveragePerMonth')->setParameter('startAveragePerMonth', $filter->getStartAveragePerMonth());
        }

        if ($filter->getEndAveragePerMonth() !== null) {
            $qb->andHaving('averagePerMonth <= :endAveragePerMonth')->setParameter('endAveragePerMonth', $filter->getEndAveragePerMonth());
        }
}

计数

public function countUserCategories(Filter $filter = null)
{
        $subQuery = $this->getCategoriesQueryBuilder($filter);
        $params = $subQuery->getParameters();

        return $this->_em->getConnection()->createQueryBuilder()
            ->select('COUNT(src.id)')
            ->from('(' . $subQuery . ')', 'src')
            ->setParameters($params)
            ->execute()
            ->fetchColumn(0);
}

public function findUserCategories(Filter $filter = null, Sort $sort = null, Pagination $pagination = null)
{
        return $this->getCategoriesQueryBuilder($filter, $sort, $pagination)->execute()->fetchAll();
}
doctrine-orm doctrine symfony4 query-builder doctrine-query
1个回答
0
投票

您可以使用原始查询作为子查询来计算。

public function countUserCategories(Filter $filter = null)
{
    $querysub = $this->getCategoriesQuery($filter);

    $query = $this->getDoctrine()->getConnection()->createQueryBuilder();

    $query
        ->select('COUNT(*)')
        ->from('(' . $querysub->getSQL() . ')', 'ttt');

    $i = 0;
    foreach ($querysub->getParameters() as $parameter) {
        $query->setParameter($i++, $parameter->getValue(), $parameter->getType());
    }

    $count = $query->execute()->fetchColumn(0);

    return $count;
}

如果参数按查询顺序设置,则此方法有效。

© www.soinside.com 2019 - 2024. All rights reserved.