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

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

我怎么能在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();
symfony join doctrine-orm
1个回答
0
投票

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

use Doctrine\ORM\Query\Expr\Join;

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();
}
  • 不要在作业上添加空格= :(错误),=:(右)
  • 正确检查你的代码,注意我在某些部分删除了一些冒号;因为没有意义将它们放在那里。
© www.soinside.com 2019 - 2024. All rights reserved.