Esempio n. 1
0
 /**
  * Returns the fields to serialize
  * @return array Array with field names
  */
 public function __sleep()
 {
     $fields = parent::__sleep();
     $fields[] = 'relationModelName';
     if ($this->isDependant) {
         $fields[] = 'isDependant';
     }
     if ($this->linkModelName) {
         $fields[] = 'linkModelName';
     }
     if ($this->foreignKeyName) {
         $fields[] = 'foreignKeyName';
     }
     return $fields;
 }
Esempio n. 2
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);
     }
 }
Esempio n. 3
0
 /**
  * Copy the properties of one field to another
  * @param zibo\library\orm\definition\field\ModelField $source
  * @param zibo\library\orm\definition\field\ModelField $destination
  * @return null
  */
 private function copyField(ModelField $source, ModelField $destination)
 {
     $destination->setLabel($source->getLabel());
     $destination->setIsLocalized($source->isLocalized());
     $validators = $source->getValidators();
     foreach ($validators as $validator) {
         $destination->addValidator($validator);
     }
 }
Esempio n. 4
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);
     }
 }
Esempio n. 5
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();
 }
Esempio n. 6
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;
 }
Esempio n. 7
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;
 }
Esempio n. 8
0
 /**
  * Checks if all the necessairy information from the module field is in the builder field
  * @param string $name Name of the field
  * @param zibo\library\orm\model\field\ModelField $moduleField
  * @param zibo\library\orm\model\field\ModelField $builderField
  * @return null
  * @throws zibo\library\orm\exception\ModelIncompleteException when the model misses information defined in the module model
  */
 private function checkModuleField($fieldName, ModelField $moduleField, ModelField $builderField)
 {
     if ($moduleField->isLocalized() != $builderField->isLocalized()) {
         throw new ModelIncompleteException('Field ' . $fieldName . ' has not the same localized flag as defined in the module model');
     }
     $moduleValidators = $moduleField->getValidators();
     $builderValidators = $moduleField->getValidators();
     foreach ($moduleValidators as $moduleValidator) {
         $validatorFound = false;
         foreach ($builderValidators as $builderValidator) {
             if ($moduleValidator->equals($builderValidator)) {
                 $validatorFound = true;
                 break;
             }
         }
         if (!$validatorFound) {
             throw new ModelIncompleteException('Field ' . $fieldName . ' does not contain the validator ' . $moduleValidator->getName() . ' as defined in the module model');
         }
     }
     if ($moduleField instanceof PropertyField) {
         if (!$builderField instanceof PropertyField) {
             throw new ModelIncompleteException('Field ' . $fieldName . ' should be a property as defined in the module model');
         }
         if ($moduleField->getType() != $builderField->getType()) {
             throw new ModelIncompleteException('Property ' . $fieldName . ' should be of the type ' . $moduleField->getType() . ' as defined in the module model');
         }
         return;
     }
     $relationModelName = $moduleField->getRelationModelName();
     if ($relationModelName != $builderField->getRelationModelName()) {
         throw new ModelIncompleteException('Field ' . $fieldName . ' should have model ' . $relationModelName . ' as relation model as defined in the module model');
     }
     $moduleLinkModelName = $moduleField->getLinkModelName();
     $builderLinkModelName = $builderField->getLinkModelName();
     if ($moduleLinkModelName && (!$builderLinkModelName || $moduleLinkModelName != $builderLinkModelName)) {
         throw new ModelIncompleteException('Field ' . $fieldName . ' should have model ' . $moduleLinkModelName . ' as link model as defined in the module model');
     }
     if ($moduleField instanceof BelongsToField) {
         if (!$builderField instanceof BelongsToField) {
             throw new ModelIncompleteException('Field ' . $fieldName . ' should be a belongsTo relation as defined in the module model');
         }
     }
     if ($moduleField instanceof HasOneField) {
         if (!$builderField instanceof HasOneField) {
             throw new ModelIncompleteException('Field ' . $fieldName . ' should be a hasOne relation as defined in the module model');
         }
     }
     if ($moduleField instanceof HasManyField) {
         if (!$builderField instanceof HasManyField) {
             throw new ModelIncompleteException('Field ' . $fieldName . ' should be a hasMany relation as defined in the module model');
         }
     }
 }