コード例 #1
0
ファイル: EntityRepository.php プロジェクト: o3co/query
 /**
  * countBy 
  * 
  * @param array $criteria 
  * @access public
  * @return void
  */
 public function countBy(array $criteria)
 {
     if (!$this->getInterfaceCriteriaParser()) {
         if (method_exists(get_parent_class($this), 'conutBy')) {
             return parent::countBy($criteria);
         }
         throw new \Exception('EntityRepository does not support method "countBy". Please overwrite this method.');
     }
     $query = $this->createInterfaceQuery($criteria, array(), null, null);
     $nativeQuery = $query->getNativeQuery();
     // Check Composite Key or not
     if ($this->getClassMetadata()->isIdentifierComposite) {
         // Select COUNT
         if (!$nativeQuery->getHint(\Doctrine\ORM\Tools\Pagination\CountOutputWalker::HINT_DISTINCT)) {
             $nativeQuery->setHint(\Doctrine\ORM\Tools\Pagination\CountOutputWalker::HINT_DISTINCT, true);
         }
         $platform = $nativeQuery->getEntityManager()->getConnection()->getDatabasePlatform();
         // law of demeter win
         $rsm = new ResultSetMapping();
         $rsm->addScalarResult($platform->getSQLResultCasing('dctrn_count'), 'count');
         $nativeQuery->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\\ORM\\Tools\\Pagination\\CountOutputWalker');
         $nativeQuery->setResultSetMapping($rsm);
     } else {
         // Set Distinct
         if (!$nativeQuery->getHint(\Doctrine\ORM\Tools\Pagination\CountWalker::HINT_DISTINCT)) {
             $nativeQuery->setHint(\Doctrine\ORM\Tools\Pagination\CountWalker::HINT_DISTINCT, true);
         }
         // Select COUNT
         $nativeQuery->setHint(\Doctrine\ORM\Query::HINT_CUSTOM_TREE_WALKERS, array('Doctrine\\ORM\\Tools\\Pagination\\CountWalker'));
     }
     $nativeQuery->setFirstResult(null)->setMaxResults(null);
     // Convert to CountQuery
     return $nativeQuery->getSingleScalarResult();
 }