Esempio n. 1
0
 /**
  * Sets a field to this model
  * @param zibo\library\orm\definition\field\ModelField $field
  * @return null
  * @throws zibo\library\orm\exception\ModelException when the field has the same link as another field in this model
  */
 public function setField(ModelField $field)
 {
     if ($field->isLocalized() && !$this->isLocalized) {
         $this->isLocalized = true;
     }
     $name = $field->getName();
     $addIndex = false;
     if ($field instanceof BelongsToField) {
         $field->setDefaultValue(0);
         $addIndex = true;
     } elseif ($field instanceof HasField) {
         if ($field instanceof HasOneField) {
             $type = self::HAS_ONE;
         } else {
             $type = self::HAS_MANY;
         }
         $relationFields = $this->getRelationFields($field->getRelationModelName(), $type);
         $numRelationFields = count($relationFields);
         if ($numRelationFields > 0) {
             $linkModelName = $field->getLinkModelName();
             if ($linkModelName) {
                 foreach ($relationFields as $relationFieldName => $relationField) {
                     if ($relationFieldName == $name) {
                         continue;
                     }
                     if ($relationField->getLinkModelName() == $linkModelName) {
                         throw new ModelException('Can\'t add ' . $name . ' to ' . $this->name . ': ' . $field->getRelationModelName() . ' is already linked with link model ' . $linkModelName . ' through the ' . $relationFieldName . ' field, check the link models.');
                     }
                 }
             }
         }
     }
     $this->fields[$name] = $field;
     if ($addIndex && !$this->hasIndex($name)) {
         $index = new Index($name, array($field));
         $this->addIndex($index);
     }
 }