Example #1
0
 protected function parseFinder(Sql $sql, Finder $finder, $alias, &$aliases, &$conditions)
 {
     $entity = $finder->getName();
     $parent = $finder->getParentName();
     $next = $finder->getNextName();
     $parentAlias = $parent ? $aliases[$parent] : null;
     $aliases[$entity] = $alias;
     $parsedConditions = $this->parseConditions($conditions, $finder, $alias);
     if (!empty($parsedConditions)) {
         $conditions[] = $parsedConditions;
     }
     if (is_null($parentAlias)) {
         return $sql->from($entity);
     } elseif ($finder->isRequired()) {
         $sql->innerJoin($entity);
     } else {
         $sql->leftJoin($entity);
     }
     if ($alias !== $entity) {
         $sql->as($alias);
     }
     $aliasedPk = "{$alias}." . $this->findPrimaryKey($entity);
     $aliasedParentPk = "{$parentAlias}." . $this->findPrimaryKey($parent);
     if ($entity === "{$parent}_{$next}") {
         return $sql->on(array("{$alias}.{$parent}_id" => $aliasedParentPk));
     } else {
         return $sql->on(array("{$parentAlias}.{$entity}_id" => $aliasedPk));
     }
 }
Example #2
0
 protected function parseCollection(Sql $sql, Collection $collection, $alias, &$aliases, &$conditions)
 {
     $entity = $collection->getName();
     $parent = $collection->getParentName();
     $next = $collection->getNextName();
     $parentAlias = $parent ? $aliases[$parent] : null;
     $aliases[$entity] = $alias;
     $parsedConditions = $this->parseConditions($conditions, $collection, $alias);
     if (!empty($parsedConditions)) {
         $conditions[] = $parsedConditions;
     }
     if (is_null($parentAlias)) {
         return $sql->from($entity);
     } elseif ($collection->isRequired()) {
         $sql->innerJoin($entity);
     } else {
         $sql->leftJoin($entity);
     }
     if ($alias !== $entity) {
         $sql->as($alias);
     }
     $aliasedPk = $alias . '.' . $this->getStyle()->primaryFromTable($entity);
     $aliasedParentPk = $parentAlias . '.' . $this->getStyle()->primaryFromTable($parent);
     if ($entity === $this->getStyle()->manyFromLeftRight($parent, $next) || $entity === $this->getStyle()->manyFromLeftRight($next, $parent)) {
         return $sql->on(array($alias . '.' . $this->getStyle()->foreignFromTable($parent) => $aliasedParentPk));
     } else {
         return $sql->on(array($parentAlias . '.' . $this->getStyle()->foreignFromTable($entity) => $aliasedPk));
     }
 }
Example #3
0
 protected function parseCollection(Sql $sql, Collection $collection, $alias, &$aliases, &$conditions)
 {
     $s = $this->getStyle();
     $entity = $collection->getName();
     $parent = $collection->getParentName();
     $next = $collection->getNextName();
     $parentAlias = $parent ? $aliases[$parent] : null;
     $aliases[$entity] = $alias;
     $conditions = $this->parseConditions($conditions, $collection, $alias) ?: $conditions;
     //No parent collection means it's the first table in the query
     if (is_null($parentAlias)) {
         $sql->from($entity);
         $this->parseMixins($sql, $collection, $entity);
         return;
     } else {
         if ($collection->isRequired()) {
             $sql->innerJoin($entity);
         } else {
             $sql->leftJoin($entity);
         }
         $this->parseMixins($sql, $collection, $entity);
         if ($alias !== $entity) {
             $sql->as($alias);
         }
         $aliasedPk = $alias . '.' . $s->identifier($entity);
         $aliasedParentPk = $parentAlias . '.' . $s->identifier($parent);
         if ($this->hasComposition($entity, $next, $parent)) {
             $onName = $alias . '.' . $s->remoteIdentifier($parent);
             $onAlias = $aliasedParentPk;
         } else {
             $onName = $parentAlias . '.' . $s->remoteIdentifier($entity);
             $onAlias = $aliasedPk;
         }
         return $sql->on(array($onName => $onAlias));
     }
 }