Example #1
0
 /**
  * @override
  */
 public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
 {
     $dqlAlias = $this->collectionPathExpression->identificationVariable;
     $parts = $this->collectionPathExpression->parts;
     $assocField = array_pop($parts);
     $qComp = $sqlWalker->getQueryComponent(implode('.', array_merge((array) $dqlAlias, $parts)));
     $assoc = $qComp['metadata']->associationMappings[$assocField];
     $sql = '';
     if ($assoc->isOneToMany()) {
         $targetClass = $sqlWalker->getEntityManager()->getClassMetadata($assoc->targetEntityName);
         $targetAssoc = $targetClass->associationMappings[$assoc->mappedByFieldName];
         $targetTableAlias = $sqlWalker->getSqlTableAlias($targetClass->primaryTable['name']);
         $sourceTableAlias = $sqlWalker->getSqlTableAlias($qComp['metadata']->primaryTable['name'], $dqlAlias);
         $whereSql = '';
         foreach ($targetAssoc->targetToSourceKeyColumns as $targetKeyColumn => $sourceKeyColumn) {
             $whereSql .= ($whereSql == '' ? ' WHERE ' : ' AND ') . $targetTableAlias . '.' . $sourceKeyColumn . ' = ' . $sourceTableAlias . '.' . $targetKeyColumn;
         }
         $sql = '(SELECT COUNT(' . "{$targetTableAlias}." . implode(", {$targetTableAlias}.", $targetAssoc->targetToSourceKeyColumns) . ') FROM ' . $targetClass->primaryTable['name'] . ' ' . $targetTableAlias . $whereSql . ')';
     } else {
         if ($assoc->isManyToMany()) {
             // TODO
             $targetTableAlias = $sqlWalker->getSqlTableAlias($assoc->joinTable['name']);
             $sourceTableAlias = $sqlWalker->getSqlTableAlias($qComp['metadata']->primaryTable['name'], $dqlAlias);
             $whereSql = '';
             foreach ($assoc->relationToSourceKeyColumns as $targetKeyColumn => $sourceKeyColumn) {
                 $whereSql .= ($whereSql == '' ? ' WHERE ' : ' AND ') . $targetTableAlias . '.' . $targetKeyColumn . ' = ' . $sourceTableAlias . '.' . $sourceKeyColumn;
             }
             $sql = '(SELECT COUNT(' . "{$targetTableAlias}." . implode(", {$targetTableAlias}.", $assoc->joinTableColumns) . ') FROM ' . $assoc->joinTable['name'] . ' ' . $targetTableAlias . $whereSql . ')';
         }
     }
     return $sql;
 }
Example #2
0
 /**
  * @override
  * @todo If the collection being counted is already joined, the SQL can be simpler (more efficient).
  */
 public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
 {
     $platform = $sqlWalker->getConnection()->getDatabasePlatform();
     $dqlAlias = $this->collectionPathExpression->identificationVariable;
     $assocField = $this->collectionPathExpression->field;
     $qComp = $sqlWalker->getQueryComponent($dqlAlias);
     $class = $qComp['metadata'];
     $assoc = $class->associationMappings[$assocField];
     $sql = 'SELECT COUNT(*) FROM ';
     if ($assoc['type'] == \Doctrine\ORM\Mapping\ClassMetadata::ONE_TO_MANY) {
         $targetClass = $sqlWalker->getEntityManager()->getClassMetadata($assoc['targetEntity']);
         $targetTableAlias = $sqlWalker->getSqlTableAlias($targetClass->table['name']);
         $sourceTableAlias = $sqlWalker->getSqlTableAlias($class->table['name'], $dqlAlias);
         $sql .= $targetClass->getQuotedTableName($platform) . ' ' . $targetTableAlias . ' WHERE ';
         $owningAssoc = $targetClass->associationMappings[$assoc['mappedBy']];
         $first = true;
         foreach ($owningAssoc['targetToSourceKeyColumns'] as $targetColumn => $sourceColumn) {
             if ($first) {
                 $first = false;
             } else {
                 $sql .= ' AND ';
             }
             $sql .= $targetTableAlias . '.' . $sourceColumn . ' = ' . $sourceTableAlias . '.' . $class->getQuotedColumnName($class->fieldNames[$targetColumn], $platform);
         }
     } else {
         // many-to-many
         $targetClass = $sqlWalker->getEntityManager()->getClassMetadata($assoc['targetEntity']);
         $owningAssoc = $assoc['isOwningSide'] ? $assoc : $targetClass->associationMappings[$assoc['mappedBy']];
         $joinTable = $owningAssoc['joinTable'];
         // SQL table aliases
         $joinTableAlias = $sqlWalker->getSqlTableAlias($joinTable['name']);
         $sourceTableAlias = $sqlWalker->getSqlTableAlias($class->table['name'], $dqlAlias);
         // join to target table
         $sql .= $targetClass->getQuotedJoinTableName($owningAssoc, $platform) . ' ' . $joinTableAlias . ' WHERE ';
         $joinColumns = $assoc['isOwningSide'] ? $joinTable['joinColumns'] : $joinTable['inverseJoinColumns'];
         $first = true;
         foreach ($joinColumns as $joinColumn) {
             if ($first) {
                 $first = false;
             } else {
                 $sql .= ' AND ';
             }
             $sourceColumnName = $class->getQuotedColumnName($class->fieldNames[$joinColumn['referencedColumnName']], $platform);
             $sql .= $joinTableAlias . '.' . $joinColumn['name'] . ' = ' . $sourceTableAlias . '.' . $sourceColumnName;
         }
     }
     return '(' . $sql . ')';
 }