Esempio n. 1
0
 public function setInformation(array $info)
 {
     parent::setInformation($info);
     $this->cm->attribute = isset($info['key_from']) ? $info['key_from'] : 'id';
     $this->cm->column = $this->cm->t->columnRealName($this->cm->attribute);
     $this->lm->attribute = isset($info['key_to']) ? $info['key_to'] : call_user_func(Cfg::get('buildForeignKey'), $this->cm->t->modelBaseName);
     $this->lm->column = $this->lm->t->columnRealName($this->lm->attribute);
 }
Esempio n. 2
0
 public function setInformation(array $info)
 {
     parent::setInformation($info);
     $this->cm->attribute = isset($info['key_from']) ? $info['key_from'] : 'id';
     $this->cm->column = (array) $this->cm->t->columnRealName($this->cm->attribute);
     $this->lm->attribute = isset($info['key_to']) ? $info['key_to'] : 'id';
     $this->lm->column = (array) $this->lm->t->columnRealName($this->lm->attribute);
     $this->jm = new \StdClass();
     if (isset($info['join_model'])) {
         $this->jm->class = $s = $info['join_model'] . Cfg::get('modelClassSuffix');
         $this->jm->t = $s::table();
         $this->jm->table = $this->jm->t->name;
         $this->jm->from = (array) $this->jm->t->columnRealName(isset($info['join_from']) ? $info['join_from'] : call_user_func(Cfg::get('buildForeignKey'), $this->cm->t->modelBaseName));
         $this->jm->to = (array) $this->jm->t->columnRealName(isset($info['join_to']) ? $info['join_to'] : call_user_func(Cfg::get('buildForeignKey'), $this->lm->t->modelBaseName));
     } else {
         $this->jm->class = null;
         $this->jm->table = isset($info['join_table']) ? $info['join_table'] : $this->cm->table . '_' . $this->lm->table;
         $this->jm->from = (array) (isset($info['join_from']) ? $info['join_from'] : decamelize($this->cm->t->modelBaseName) . '_id');
         $this->jm->to = (array) (isset($info['join_to']) ? $info['join_to'] : decamelize($this->lm->t->modelBaseName) . '_id');
     }
 }
Esempio n. 3
0
 /**
  * Add a condition over a relation.
  *
  * This is not the same as perform a join. An involved table will be used
  * and conditions given by the relation object will be added. But no JOIN
  * will be performed.
  *
  * This method is particularly useful to connect tables between subquery and
  * main query.
  *
  * Important:
  * ----------
  * Current Model (CM) of relation is the table to involve ; Linked Model
  * (LM) is the current builder's root model.
  *
  * @param Relation $rel
  * @param string   $lmAlias An alias for the linked model.
  */
 public function whereRelation(Relation $rel, $lmAlias = null)
 {
     if ($lmAlias === null) {
         $lmAlias = self::DEFAULT_ROOT_ALIAS;
     }
     $lmTable = $rel->cm->t;
     $this->setInvolvedTable($lmAlias, $lmTable);
     // Make the join between both tables:
     $sep = Cfg::get('queryOptionRelationSeparator');
     if ($rel instanceof ManyMany) {
         $mdTable = $rel->getMiddleTableName();
         $mdAlias = $rel->getMiddleTableAlias();
         $mdAlias = $lmAlias === self::DEFAULT_ROOT_ALIAS ? $mdAlias : $lmAlias . '.' . $mdAlias;
         // Join middle table.
         $jc = new JoinClause($mdTable, $mdAlias);
         $jc->on($this->getRootAlias(), $rel->lm->column, $mdAlias, $rel->jm->to);
         $this->setJoinClause($mdAlias, $jc);
         // Where on relation.
         $lmCols = $rel->cm->column;
         $mdCols = $rel->jm->from;
         $this->whereCol($mdAlias, $mdCols, '=', $lmAlias, $lmCols);
     } else {
         // $lmAttr: attribute of the model to link. (The relation CM attr)
         // $cmAttr: current builder root model attribute. (The relation LM attr)
         foreach ($rel->getJoinAttributes() as $lmAttr => $cmAttr) {
             $this->whereAttr($cmAttr, $lmAlias . $sep . $lmAttr);
         }
     }
     return $this;
 }
Esempio n. 4
0
 /**
  * Associate preloaded linked models to correct current models.
  *
  */
 protected function _associateLinkedModels(array $cmInstances, array $lmInstances, Relation $relation)
 {
     $cmAttr = $relation->getCmAttributes();
     $lmAttr = $relation->getLmAttributes();
     $rName = $relation->name;
     foreach ($cmInstances as $cm) {
         foreach ($lmInstances as $lm) {
             if ($cm->get($cmAttr) === $lm->get($lmAttr)) {
                 if ($relation->isToMany()) {
                     $tmp = $cm->{$rName};
                     $tmp[] = $lm;
                     $cm->{$rName} = $tmp;
                 } else {
                     $cm->{$rName} = $lm;
                     break;
                 }
             }
         }
     }
 }
Esempio n. 5
0
 /**
  * Get or set the relation identified by its name.
  *
  * If second parameter is null, it works like a getter.
  * It works like a setter if second parameter is not null.
  *
  * @param $name     The name of the relation.
  * @param $relation The relation to set instead
  *
  * @return object A Relation object.
  * @throw SimpleAR\Exception if used as getter and $relationName is unknown.
  */
 public static function relation($name, Relation $relation = null)
 {
     if ($relation === null) {
         if (!isset(static::$_relations[$name])) {
             throw new Exception('Relation "' . $name . '" does not exist for class "' . get_called_class() . '".');
         }
         // Avoid array accesses.
         $val =& static::$_relations[$name];
         // If relation is not yet initlialized, do it.
         if (!$val instanceof Relation) {
             $val = Relation::forge($name, $val, get_called_class());
         }
         // Return the Relation object.
         return $val;
     } else {
         static::$_relations[$name] = $relation;
     }
 }