예제 #1
0
 /**
  * @expectedException zibo\library\orm\exception\OrmException
  */
 public function testRegisterModelWithLocalizationDoesntRegisterLocalizedLinkModelWhenBothLinksAreLocalized()
 {
     $field1 = new HasManyField('field1', 'Model2');
     $field1->setIsLocalized(true);
     $model1 = $this->createModel('model1', array($field1));
     $field2 = new BelongsToField('field2', 'Model1');
     $field2->setIsLocalized(true);
     $model2 = $this->createModel('model2', array($field2));
     $this->register->registerModel($model1);
     $this->register->registerModel($model2);
     $localizedModel = $this->register->getModel('model1' . LocalizedModel::MODEL_SUFFIX . 'model2');
 }
예제 #2
0
 /**
  * Processes the next action of this step
  * return string Name of the next step
  */
 public function next()
 {
     try {
         $this->wizard->validate();
     } catch (ValidationException $exception) {
         Zibo::getInstance()->runEvent(Zibo::EVENT_LOG, $exception->getMessage(), $exception->getTraceAsString());
         return null;
     }
     $modelTable = $this->wizard->getVariable(BuilderWizard::VARIABLE_MODEL_TABLE);
     $fieldName = $this->wizard->getValue(self::FIELD_FIELD_NAME);
     if (!$fieldName) {
         $this->wizard->setVariable(BuilderWizard::VARIABLE_FIELD);
         if ($this->wizard->getLimitField()) {
             return FinishStep::NAME;
         }
         return DataFormatStep::NAME;
     }
     $field = null;
     $newField = null;
     if ($modelTable->hasField($fieldName)) {
         $field = $modelTable->getField($fieldName);
     }
     $fieldType = $this->wizard->getValue(self::FIELD_FIELD_TYPE);
     if ($fieldType == self::TYPE_PROPERTY) {
         $propertyType = $this->wizard->getValue(self::FIELD_PROPERTY_TYPE);
         $propertyDefault = $this->wizard->getValue(self::FIELD_PROPERTY_DEFAULT);
         $newField = new PropertyField($fieldName, $propertyType, $propertyDefault);
         if ($field) {
             $this->copyField($field, $newField);
         }
     } else {
         $relationType = $this->wizard->getValue(self::FIELD_RELATION_TYPE);
         $relationModel = $this->wizard->getValue(self::FIELD_RELATION_MODEL);
         switch ($relationType) {
             case ModelTable::BELONGS_TO:
                 $newField = new BelongsToField($fieldName, $relationModel);
                 break;
             case ModelTable::HAS_ONE:
                 $newField = new HasOneField($fieldName, $relationModel);
                 break;
             case ModelTable::HAS_MANY:
                 $newField = new HasManyField($fieldName, $relationModel);
                 $relationOrder = $this->wizard->getValue(self::FIELD_RELATION_ORDER);
                 if ($relationOrder) {
                     $newField->setRelationOrder($relationOrder);
                 }
                 break;
         }
         if ($field) {
             $this->copyField($field, $newField);
         }
         $relationLinkModel = $this->wizard->getValue(self::FIELD_RELATION_LINK_MODEL);
         if ($relationLinkModel && $relationLinkModel != self::AUTOMATIC) {
             $newField->setLinkModelName($relationLinkModel);
         }
         $relationForeignKey = $this->wizard->getValue(self::FIELD_RELATION_FOREIGN_KEY);
         if ($relationForeignKey && $relationForeignKey != self::AUTOMATIC) {
             $newField->setForeignKeyName($relationForeignKey);
         }
         $relationIsDependant = $this->wizard->getValue(self::FIELD_RELATION_IS_DEPENDANT);
         if (!$relationIsDependant) {
             $relationIsDependant = false;
         }
         $newField->setIsDependant($relationIsDependant);
     }
     $fieldLabel = $this->wizard->getValue(self::FIELD_FIELD_LABEL);
     if ($fieldLabel) {
         $newField->setLabel($fieldLabel);
     }
     $isLocalized = $this->wizard->getValue(self::FIELD_IS_LOCALIZED);
     if ($isLocalized === null) {
         $isLocalized = false;
     }
     $newField->setIsLocalized($isLocalized);
     try {
         $this->setFieldToModelTable($modelTable, $newField);
     } catch (Exception $exception) {
         Zibo::getInstance()->runEvent(Zibo::EVENT_LOG, $exception->getMessage(), $exception->getTraceAsString());
         return null;
     }
     $limitField = $this->wizard->getLimitField();
     if ($limitField && $limitField !== true) {
         return FinishStep::NAME;
     }
     $this->setNextField($fieldName, $modelTable->getFields());
     return self::NAME;
 }
예제 #3
0
 /**
  * Deletes or clears the values of the provided has many field in the provided data.
  * @param string $fieldName Name of the has many field
  * @param zibo\library\orm\definition\field\HasManyField $field Definition of the has many field
  * @param mixed $data Data obiect
  * @return null
  */
 private function deleteHasMany($fieldName, HasManyField $field, $data)
 {
     $model = $this->meta->getRelationModel($fieldName);
     if (!$this->meta->isHasManyAndBelongsToMany($fieldName)) {
         if ($field->isDependant()) {
             if ($data->{$fieldName}) {
                 foreach ($data->{$fieldName} as $record) {
                     $model->delete($record);
                 }
             }
         } else {
             $this->clearHasMany($this->meta->getRelationModelTable($fieldName), $data->id, true);
         }
         return;
     }
     $linkModelTable = $this->meta->getRelationLinkModelTable($fieldName);
     if ($field->isDependant()) {
         foreach ($data->{$fieldName} as $record) {
             $model->delete($record);
         }
         $keepRecord = false;
     } else {
         $fields = $linkModelTable->getFields();
         $keepRecord = count($fields) != 3;
     }
     $this->clearHasMany($linkModelTable, $data->id, $keepRecord);
 }
예제 #4
0
 /**
  * Get the ModelField from a relation field element
  * @param DOMElement $fieldElement field element in the model element
  * @param zibo\library\filesystem\File $file the file which is being read
  * @param string $modelName the model which is currently being processed
  * @param string $fieldName the field which is currently being processed
  * @param string $relationModelName the name of the model for which this field is a relation
  * @return zibo\library\orm\definition\field\ModelField
  * @throws zibo\library\orm\exception\OrmException when an invalid relation type has been defined
  */
 protected function getRelationFieldFromElement(DOMElement $fieldElement, File $file, $modelName, $fieldName, $relationModelName)
 {
     $relationType = $fieldElement->hasAttribute(self::ATTRIBUTE_RELATION) ? $fieldElement->getAttribute(self::ATTRIBUTE_RELATION) : self::DEFAULT_FIELD_RELATION;
     switch ($relationType) {
         case self::RELATION_BELONGS_TO:
             $field = new BelongsToField($fieldName, $relationModelName);
             break;
         case self::RELATION_HAS_ONE:
             $field = new HasOneField($fieldName, $relationModelName);
             break;
         case self::RELATION_HAS_MANY:
             $field = new HasManyField($fieldName, $relationModelName);
             $relationOrder = $fieldElement->hasAttribute(self::ATTRIBUTE_RELATION_ORDER) ? $fieldElement->getAttribute(self::ATTRIBUTE_RELATION_ORDER) : null;
             $field->setRelationOrder($relationOrder);
             $indexOn = $fieldElement->hasAttribute(self::ATTRIBUTE_INDEX_ON) ? $fieldElement->getAttribute(self::ATTRIBUTE_INDEX_ON) : null;
             $field->setIndexOn($indexOn);
             $linkModelName = $fieldElement->hasAttribute(self::ATTRIBUTE_LINK_MODEL) ? $fieldElement->getAttribute(self::ATTRIBUTE_LINK_MODEL) : null;
             $field->setLinkModelName($linkModelName);
             break;
         default:
             throw new OrmException("{$fieldName} of {$modelName} has an invalid relation ({$relationType}) in {$file->getPath()}");
             break;
     }
     $dependant = $fieldElement->hasAttribute(self::ATTRIBUTE_DEPENDANT) ? Boolean::getBoolean($fieldElement->getAttribute(self::ATTRIBUTE_DEPENDANT)) : false;
     $field->setIsDependant($dependant);
     $foreignKey = $fieldElement->hasAttribute(self::ATTRIBUTE_FOREIGN_KEY) ? $fieldElement->getAttribute(self::ATTRIBUTE_FOREIGN_KEY) : null;
     if ($foreignKey) {
         $field->setForeignKeyName($foreignKey);
     }
     return $field;
 }
예제 #5
0
 /**
  * Gets the link model name for a many to many relation
  * @param zibo\library\orm\definition\field\HasManyField $field1
  * @param zibo\library\orm\definition\field\HasManyField $field2
  * @param zibo\library\orm\model\meta\ModelMeta $modelMeta Meta of the model of the field
  * @param string $fieldName Field for the link model
  * @return string Name of the link model
  * @throws zibo\library\orm\exception\ModelException when field1 and field2 have the link model set but they are not the same
  */
 private function getManyToManyLinkModelName(HasManyField $field1, HasManyField $field2, ModelMeta $modelMeta, $fieldName)
 {
     $linkModelName1 = $field1->getLinkModelName();
     $linkModelName2 = $field2->getLinkModelName();
     if ($linkModelName1 && $linkModelName2) {
         if ($linkModelName1 != $linkModelName2) {
             throw new ModelException('Link model names of ' . $field1->getName() . ' and ' . $field2->getName() . ' are not equal');
         }
         return $linkModelName1;
     }
     if ($linkModelName1) {
         return $linkModelName1;
     }
     if ($linkModelName2) {
         return $linkModelName2;
     }
     $modelName1 = $field1->getRelationModelName();
     $modelName2 = $field2->getRelationModelName();
     $linkModelName = $this->generateLinkModelName($modelName1, $modelName2);
     return $this->getUniqueLinkModelName($modelMeta, $fieldName, $linkModelName);
 }
예제 #6
0
 /**
  * @dataProvider providerSetRelationOrderThrowsExceptionWhenOrderIsInvalid
  * @expectedException zibo\ZiboException
  */
 public function testSetRelationOrderThrowsExceptionWhenOrderIsInvalid($value)
 {
     $field = new HasManyField('name', 'model');
     $field->setRelationOrder($value);
 }