2017-12-20 25 views
0

이 sql 쿼리를 심포니 쿼리 빌더 구문에 어떻게 작성할 수 있습니까?doctrine query builder를 사용하여이 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' 

하지만, 나는 오류 MSG가없는,하지만 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(); 

답변

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(); 
} 
  • 는, 과제에 =:= : (잘못을) 공백을 추가하지 마십시오 (오른쪽)
  • 이 코드를 확인 제대로, 일부 결장에서 어떤 결장을 어떻게 제거했는지 알 수 있습니다.
+0

ReynierPM의 조언을 보내 주셔서 감사합니다. innerjoin에 대한 나의 실수 : "nature.id"대신 ": nature" – YornLetard

+0

환영합니다. 기꺼이 도와주세요! – ReynierPM