如何使用doctrine查询生成器编写此sql查询?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用doctrine查询生成器编写此sql查询?相关的知识,希望对你有一定的参考价值。

我怎么能在symfony查询构建器语法上编写这个sql查询?

分析,地区,自然和花园是实体

SELECT * FROM `analysis`
    INNER JOIN sample ON sample.id = analysis.sample_id
    INNER JOIN region ON sample.region_id = region.id
    INNER JOIN nature ON sample.nature_id = nature.id
    INNER JOIN garden ON sample.garden_id = garden.id
    WHERE sample.stateProduct = 'Origin'
    AND analysis.status = '0'
    AND region.name = 'Jangsu'
    AND garden.name = 'North Tukvar'
    AND nature.name = 'Thé Vert'
    AND sample.sampleBio = '1'
    AND sample.supplierCountry = 'Inde'

我试过这种方式但是,我没有错误消息,但它与sql查询结果不一样。

public function countAnalysisByCriteria($stateProduct, $status, Nature $nature, Region $region, Garden $garden, $supplierName, $bio, $country, $startDate, $endDate){
        $qb = $this->createQueryBuilder('analysis')
            ->addSelect('count(analysis) as result')
            ->innerJoin('analysis.sample', 'sample', 'WITH', 'analysis.sample = sample')
            ->innerJoin('sample.nature', 'nature', 'WITH', 'sample.nature = :nature')
            ->innerJoin('sample.region', 'region', 'WITH', 'sample.region = :region')
            ->innerJoin('sample.garden', 'garden', 'WITH', 'sample.garden = :garden')

            ->groupBy('result');

               ->andWhere('sample.stateProduct = :stateProduct');
               ->setParameter('stateProduct', $stateProduct);
               ->andWhere('sample.nature = :nature');
               ->setParameter('nature', $nature);
               ->andWhere('sample.region = :region');
               ->setParameter('region', $region);
               ->andWhere('sample.garden = :garden');
               ->setParameter('garden', $garden);
               ->andWhere('sample.dateReception BETWEEN :startDate AND :endDate');
                $qb->setParameter('startDate', $startDate);
                $qb->setParameter('endDate', $endDate);
            }

        return $qb->getQuery()->getArrayResult();
答案

您的代码段完全错误,请尝试以下操作:

use DoctrineORMQueryExprJoin;

public function countAnalysisByCriteria(
    $stateProduct,
    $status,
    Nature $nature,
    Region $region,
    Garden $garden,
    $supplierName,
    $bio,
    $country,
    $startDate,
    $endDate
) {
    $qb = $this->createQueryBuilder();

    return $qb->select('count(analysis) as result')
        ->innerJoin('analysis.sample', 'sample', Join::WITH, 'analysis.sample = sample.id')
        ->innerJoin('sample.nature', 'nature', Join::WITH, 'sample.nature = nature.id')
        ->innerJoin('sample.region', 'region', Join::WITH, 'sample.region = region.id')
        ->innerJoin('sample.garden', 'garden', Join::WITH, 'sample.garden = garden.id')
        ->groupBy('result')
        ->andWhere('sample.stateProduct =:stateProduct')
        ->setParameter('stateProduct', $stateProduct)
        ->andWhere('sample.nature =:nature')
        ->setParameter('nature', $nature)
        ->andWhere('sample.region =:region')
        ->setParameter('region', $region)
        ->andWhere('sample.garden =:garden')
        ->setParameter('garden', $garden)
        ->andWhere('sample.dateReception BETWEEN :startDate AND :endDate')
        ->setParameter('startDate', $startDate)
        ->setParameter('endDate', $endDate)
        ->getQuery()
        ->getArrayResult();
}
  • 不要在作业上添加空格= :(错误),=:(右)
  • 正确检查你的代码,注意我在某些部分删除了一些冒号;因为没有意义将它们放在那里。

以上是关于如何使用doctrine查询生成器编写此sql查询?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Doctrine 查询生成器中进行多个 WHERE IN 列查询?

Doctrine Orm 2 使用本机 SQL 的递归查询

用于标量查询的 Doctrine 返回数组数组

Symfony2 & Doctrine:创建自定义 SQL 查询

如何在 SQL Server 的 Doctrine 2 中更改 DQL 查询中的 LockMode

如何使用实体框架和 linq 编写此 sql 查询