Example #1
0
 /**
  * Computes the given aggregates and returns an array of answers
  *
  * An answer will be null if the repository is unable to answer it.
  *
  * @param \Rhubarb\Stem\Aggregates\Aggregate[] $aggregates
  * @param \Rhubarb\Stem\Collections\Collection $collection
  *
  * @return array
  */
 public function calculateAggregates($aggregates, Collection $collection)
 {
     $propertiesToAutoHydrate = [];
     if (!$this->canFilterExclusivelyByRepository($collection, $namedParams, $propertiesToAutoHydrate)) {
         return null;
     }
     $relationships = SolutionSchema::getAllRelationshipsForModel($this->getModelClass());
     $propertiesToAutoHydrate = array_unique($propertiesToAutoHydrate);
     $joins = [];
     $joinColumns = [];
     foreach ($propertiesToAutoHydrate as $joinRelationship) {
         /**
          * @var OneToMany $relationship
          */
         $relationship = $relationships[$joinRelationship];
         $targetModelName = $relationship->getTargetModelName();
         $targetModelClass = SolutionSchema::getModelClass($targetModelName);
         /**
          * @var Model $targetModel
          */
         $targetModel = new $targetModelClass();
         $targetSchema = $targetModel->getSchema();
         $columns = $targetSchema->getColumns();
         foreach ($columns as $columnName => $column) {
             $joinColumns[$targetModelName . $columnName] = "`{$joinRelationship}`.`{$columnName}`";
             $joinOriginalToAliasLookup[$targetModelName . "." . $columnName] = $targetModelName . $columnName;
             if (!isset($joinColumnsByModel[$targetModelName])) {
                 $joinColumnsByModel[$targetModelName] = [];
             }
             $joinColumnsByModel[$targetModelName][$targetModelName . $columnName] = $columnName;
         }
         $joins[] = "LEFT JOIN `{$targetSchema->schemaName}` AS `{$joinRelationship}` ON `{$this->schema->schemaName}`.`" . $relationship->getSourceColumnName() . "` = `{$joinRelationship}`.`" . $relationship->getTargetColumnName() . "`";
     }
     $joinString = "";
     if (sizeof($joins)) {
         $joinString = " " . implode(" ", $joins);
         $joinClauses = [];
         foreach ($joinColumns as $aliasName => $columnName) {
             $joinClauses[] = "`" . str_replace('.', '`.`', $columnName) . "` AS `" . $aliasName . "`";
         }
     }
     $clauses = [];
     $clausePositions = [];
     $results = [];
     $i = -1;
     $c = -1;
     $relationships = [];
     foreach ($aggregates as $aggregate) {
         $i++;
         $clause = $aggregate->aggregateWithRepository($this, $relationships);
         if ($clause != "") {
             $c++;
             $clauses[] = str_replace('.', '`.`', $clause);
             $clausePositions[$c] = $i;
         } else {
             $results[$i] = null;
         }
     }
     if (sizeof($clauses)) {
         $schema = $this->getSchema();
         $namedParams = [];
         $propertiesToAutoHydrate = [];
         $groupClause = "";
         if ($joinString) {
             $groupClause = " GROUP BY `{$schema->schemaName}`.`{$schema->uniqueIdentifierColumnName}`";
         }
         $sql = "SELECT " . implode(", ", $clauses) . " FROM `{$schema->schemaName}`" . $joinString;
         $filter = $collection->getFilter();
         if ($filter !== null) {
             $filterSql = $filter->filterWithRepository($this, $namedParams, $propertiesToAutoHydrate);
             if ($filterSql != "") {
                 $sql .= " WHERE " . $filterSql;
             }
         }
         $sql .= $groupClause;
         $row = array_values(self::returnFirstRow($sql, $namedParams));
         foreach ($clausePositions as $rowPosition => $resultPosition) {
             $results[$resultPosition] = $row[$rowPosition];
         }
     }
     return $results;
 }
 public function testSuperseededModelIsReturnedWhenUsingPreviousNamespacedClassName()
 {
     SolutionSchema::registerSchema("SchemaA", __NAMESPACE__ . "\\SchemaA");
     $class = SolutionSchema::getModelClass(__NAMESPACE__ . "\\ModelA");
     $this->assertEquals(__NAMESPACE__ . "\\ModelA", $class);
     SolutionSchema::registerSchema("SchemaB", __NAMESPACE__ . "\\SchemaB");
     $class = SolutionSchema::getModelClass(__NAMESPACE__ . "\\ModelA");
     $this->assertEquals(__NAMESPACE__ . "\\ModelB", $class);
 }
Example #3
0
 public function __construct($modelClassName)
 {
     $this->modelClassName = SolutionSchema::getModelClass($modelClassName);
 }
Example #4
0
 public function getCollection()
 {
     $class = SolutionSchema::getModelClass($this->targetModelName);
     return new Collection($class);
 }
 /**
  * Fetches a collection for this relationship, using the model class' find() method to allow for default filters on
  * the collection.
  *
  * @param string $collectionClassName
  * @param \Rhubarb\Stem\Filters\Filter|null $filter
  * @return \Rhubarb\Stem\Collections\Collection
  */
 protected function getRelationshipCollection($collectionClassName, $filter = null)
 {
     $class = SolutionSchema::getModelClass($collectionClassName);
     return $class::find($filter);
 }