Exemplo n.º 1
0
 /**
  * Creates a form field for the provided model property field
  * @param zibo\library\html\form\field\FieldFactory $fieldFactory Instance of the field factory
  * @param zibo\library\orm\definition\field\ModelField $field Definition of the model field
  * @param mixed $value Value for the form field
  * @return zibo\library\html\form\field\Field
  */
 protected function createFormFieldFromModelField(FieldFactory $fieldFactory, ModelField $field, $value)
 {
     $fieldName = $field->getName();
     try {
         return $fieldFactory->createField($field->getType(), $fieldName, $value);
     } catch (ZiboException $e) {
         return $fieldFactory->createField(FieldFactory::TYPE_STRING, $fieldName, $value);
     }
 }
Exemplo n.º 2
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);
     }
 }
Exemplo n.º 3
0
 /**
  * Parses a property field in the meta
  * @param zibo\library\orm\definition\field\ModelField $field
  * @return null
  */
 private function parsePropertyField(ModelField $field)
 {
     $this->properties[$field->getName()] = $field;
 }
Exemplo n.º 4
0
 /**
  * Deletes the links for the provided many to many field
  * @param integer $id Primary key of the data which is being saved
  * @param string $fieldName Name of the has many field
  * @param zibo\library\orm\definition\field\ModelField $foreignKey Definition of the foreign key field in the link model
  * @return null
  */
 private function deleteOldHasManyAndBelongsToMany($id, $fieldName, $foreignKey)
 {
     $linkModel = $this->meta->getRelationLinkModel($fieldName);
     $condition = new SimpleCondition(new FieldExpression($foreignKey->getName()), new ScalarExpression($id), Condition::OPERATOR_EQUALS);
     $statement = new DeleteStatement();
     $statement->addTable(new TableExpression($linkModel->getName()));
     $statement->addCondition($condition);
     $this->executeStatement($statement);
     $linkModel->clearCache();
 }
Exemplo n.º 5
0
 /**
  * Create a xml element with the definition of a model field
  * @param zibo\library\xml\dom\Document $dom
  * @param zibo\library\orm\definition\field\ModelField $field
  * @return DOMElement an xml element which defines the model field
  */
 protected function getElementFromField(Document $dom, ModelField $field)
 {
     $element = $dom->createElement(self::TAG_FIELD);
     $element->setAttribute(self::ATTRIBUTE_NAME, $field->getName());
     if ($field instanceof RelationField) {
         $element->setAttribute(self::ATTRIBUTE_MODEL, $field->getRelationModelName());
         if ($field instanceof BelongsToField) {
             $element->setAttribute(self::ATTRIBUTE_RELATION, self::RELATION_BELONGS_TO);
         } elseif ($field instanceof HasOneField) {
             $element->setAttribute(self::ATTRIBUTE_RELATION, self::RELATION_HAS_ONE);
         } elseif ($field instanceof HasManyField) {
             $element->setAttribute(self::ATTRIBUTE_RELATION, self::RELATION_HAS_MANY);
             $linkModel = $field->getLinkModelName();
             if ($linkModel != null) {
                 $element->setAttribute(self::ATTRIBUTE_LINK_MODEL, $linkModel);
             }
             $indexOn = $field->getIndexOn();
             if ($indexOn) {
                 $element->setAttribute(self::ATTRIBUTE_INDEX_ON, $indexOn);
             }
         }
         if ($field->isDependant()) {
             $element->setAttribute(self::ATTRIBUTE_DEPENDANT, 'true');
         }
         $foreignKey = $field->getForeignKeyName();
         if ($foreignKey) {
             $element->setAttribute(self::ATTRIBUTE_FOREIGN_KEY, $foreignKey);
         }
     } else {
         $element->setAttribute(self::ATTRIBUTE_TYPE, $field->getType());
         $default = $field->getDefaultValue();
         if ($default != null) {
             $element->setAttribute(self::ATTRIBUTE_DEFAULT, $default);
         }
         if ($field->IsUnique()) {
             $element->setAttribute(self::ATTRIBUTE_UNIQUE, 'true');
         }
     }
     if ($field->isLocalized()) {
         $element->setAttribute(self::ATTRIBUTE_LOCALIZED, 'true');
     }
     $label = $field->getLabel();
     if ($label) {
         $element->setAttribute(self::ATTRIBUTE_LABEL, $label);
     }
     $validators = $field->getValidators();
     foreach ($validators as $validator) {
         $validatorElement = $dom->createElement(self::TAG_VALIDATION);
         $validatorElement->setAttribute(self::ATTRIBUTE_NAME, $validator->getName());
         $options = $validator->getOptions();
         foreach ($options as $key => $value) {
             $parameterElement = $dom->createElement(self::TAG_PARAMETER);
             $parameterElement->setAttribute(self::ATTRIBUTE_NAME, $key);
             $parameterElement->setAttribute(self::ATTRIBUTE_VALUE, $value);
             $validatorElement->appendChild($parameterElement);
         }
         $element->appendChild($validatorElement);
     }
     return $element;
 }