/**
  * Adds a belongs to field to the statement
  * @param zibo\library\orm\definition\field\BelongsToField $field
  * @return null;
  */
 private function addBelongsTo(BelongsToField $field)
 {
     $name = $field->getName();
     $relationModel = $this->meta->getRelationModel($name);
     $hasRelations = $relationModel->getMeta()->hasRelations();
     $useJoin = false;
     if ($this->recursiveDepth === 1 || $this->recursiveDepth !== 0 && !$hasRelations) {
         $useJoin = true;
     }
     if (!$useJoin && ($this->recursiveDepth === null || $this->recursiveDepth > 1)) {
         $this->recursiveBelongsToFields[$name] = $field;
     }
     if (!$useJoin) {
         if ($field->isLocalized()) {
             $this->addField($this->tables[self::ALIAS_SELF_LOCALIZED], $name);
             $this->localize = true;
         } else {
             $this->addField($this->tables[self::ALIAS_SELF], $name);
         }
         return;
     }
     $this->addFieldJoin($field);
     $relationTable = $this->meta->getRelationTable($name);
     $relationLocalizedTable = $this->meta->getRelationLocalizedTable($name);
     $relationFields = $this->meta->getRelationFields($name);
     foreach ($relationFields as $relationField) {
         if ($relationField instanceof HasField) {
             continue;
         }
         if ($relationField->isLocalized()) {
             $this->addField($relationLocalizedTable, $relationField->getName(), $name);
         } else {
             $this->addField($relationTable, $relationField->getName(), $name);
         }
     }
 }
Beispiel #2
0
 /**
  * Parses a belongs to field in the meta
  * @param zibo\library\orm\definition\field\BelongsToField $field
  * @return null
  * @param zibo\library\orm\ModelManager $modelManager Instance of the model manager
  */
 private function parseBelongsToField(BelongsToField $field, ModelManager $modelManager)
 {
     $name = $field->getName();
     $modelName = $this->getName();
     $relationModelName = $field->getRelationModelName();
     try {
         $relationModel = $modelManager->getModel($relationModelName);
     } catch (OrmException $exception) {
         throw new ModelException('No relation model found for field ' . $name . ' in ' . $modelName);
     }
     $relation = new RelationMeta();
     $relation->setIsRelationWithSelf($relationModelName == $modelName);
     $this->relations[$name] = $relation;
     $this->belongsTo[$name] = $field;
     $relationFields = $relationModel->getMeta()->getRelation($modelName);
     if (!$relationFields[ModelTable::BELONGS_TO] && !$relationFields[ModelTable::HAS_MANY] && !$relationFields[ModelTable::HAS_ONE]) {
         return;
     }
     if ($relationFields[ModelTable::HAS_MANY]) {
         $relationType = ModelTable::HAS_MANY;
     } else {
         if ($relationFields[ModelTable::HAS_ONE]) {
             $relationType = ModelTable::HAS_ONE;
         } else {
             return;
         }
     }
     $relationField = array_shift($relationFields[$relationType]);
     if ($relationField->isLocalized()) {
         $linkModelName = $field->getLinkModelName();
         if (empty($linkModelName)) {
             throw new ModelException('No link model found for field ' . $name . ' in ' . $modelName);
         }
         $relation->setLinkModelName($linkModelName);
     }
 }