/** * 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; }
/** * 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; }
/** * @dataProvider providerSetRelationOrderThrowsExceptionWhenOrderIsInvalid * @expectedException zibo\ZiboException */ public function testSetRelationOrderThrowsExceptionWhenOrderIsInvalid($value) { $field = new HasManyField('name', 'model'); $field->setRelationOrder($value); }