Example #1
0
 /**
  * @param bool $with_namespace
  *
  * @return string
  */
 public function getClassName($with_namespace = false)
 {
     $prefix = $with_namespace ? '\\' . $this->database->getNamespace() . '\\' : '';
     return $prefix . Wave\Inflector::camelize($this->table);
 }
Example #2
0
 /**
  * Returns the name of the relation.  It needs to be based on the column name as if there
  * is more than one relation to the same table, the relation won't have a unique name.
  * If the column ends with '_id', it will be removed.
  **/
 public function getName()
 {
     //$local_column
     switch ($this->type) {
         case self::ONE_TO_ONE:
             $name = $this->getReferencedTable()->getName();
             break;
         case self::MANY_TO_ONE:
             //in this case we need to name the relation based on the column, trimming off _id (if it exists)
             $name = $this->local_columns[0]->getName();
             if (substr($name, -3) === '_id') {
                 $name = substr($name, 0, -3);
             }
             break;
         case self::ONE_TO_MANY:
             //slightly more complex to remove collisions between m2m names
             $name = Wave\Inflector::pluralize($this->getReferencedTable()->getName());
             $ref_name = $this->referenced_columns[0]->getName();
             if (substr($ref_name, -3) === '_id') {
                 $ref_name = substr($ref_name, 0, -3);
             }
             if ($ref_name !== $this->getLocalTable()->getName()) {
                 $name .= '_' . $ref_name;
             }
             break;
         case self::MANY_TO_MANY:
             $columns = $this->target_relation->getLocalColumns();
             $name = $columns[0]->getMetadata('relation_name');
             if ($name === null) {
                 $name = $this->target_relation->getReferencedTable()->getName();
             }
             $name = Wave\Inflector::pluralize($name);
             break;
     }
     return Wave\Inflector::camelize($name);
 }
Example #3
0
 /**
  * Return the specified relation objects with the primary object
  *
  * @param string $relation The class name of the relation to join to the primary object.
  * @param string|null $alias [optional] Provide a custom alias to use for the joined object, or null to have one
  *                               generated. This property is passed by reference so it can be used to get a
  *                               handle on the generated alias.
  *
  * @return Query
  */
 public function with($relation, &$alias = null)
 {
     $from_class = $this->unaliasClass($this->from_alias);
     $relation_data = $from_class::_getRelation($relation);
     //if it's many to many, load the related class and flag the first join to be attached as relation data
     if ($relation_data['relation_type'] === Wave\DB\Relation::MANY_TO_MANY) {
         $this->leftJoin($relation_data['related_class'], $related_alias, $target_alias);
         //more iteration for multi-column relations
         foreach ($relation_data['related_columns'] as $index => $related_column) {
             $func = $index === 0 ? 'on' : 'and';
             $this->{$func}(sprintf('%s.%s = %s.%s', $related_alias, $this->escape($related_column), $this->from_alias, $this->escape($relation_data['local_columns'][$index])));
         }
         $this->leftJoin($relation_data['target_relation']['related_class'], $alias);
         foreach ($relation_data['target_relation']['related_columns'] as $index => $related_column) {
             $func = $index === 0 ? 'on' : 'and';
             $this->{$func}(sprintf('%s.%s = %s.%s', $alias, $this->escape($related_column), $related_alias, $this->escape($relation_data['target_relation']['local_columns'][$index])));
         }
         //go back and set the target alias to the joined row
         $target_alias = $alias;
     } else {
         //any other type of join is a simple table-table join
         $this->leftJoin($relation_data['related_class'], $alias);
         //more iteration for multi-column relations
         foreach ($relation_data['related_columns'] as $index => $related_column) {
             $func = $index === 0 ? 'on' : 'and';
             $this->{$func}(sprintf('%s.%s = %s.%s', $alias, $this->escape($related_column), $this->from_alias, $this->escape($relation_data['local_columns'][$index])));
         }
     }
     //this needs recording so the object can be added as a relation, not a join
     $this->with[$alias] = array('relation_type' => $relation_data['relation_type'], 'relation_name' => Wave\Inflector::singularize($relation));
     return $this;
 }