/** * Get the model from the model element * @param DOMElement $modelElement model element in the xml root element * @param zibo\library\filesystem\File $file the file which is being read * @return zibo\library\orm\Model Model instance created from the read model definition * @throws zibo\library\orm\exception\OrmException when the model element has no name attribute */ protected function getModelFromElement(DOMElement $modelElement, File $file) { $modelName = $modelElement->getAttribute(self::ATTRIBUTE_NAME); if ($modelName == null) { throw new OrmException('No ' . self::ATTRIBUTE_NAME . ' attribute found for ' . self::TAG_MODEL . ' tag in ' . $file->getPath()); } $group = $modelElement->getAttribute(self::ATTRIBUTE_GROUP) ? $modelElement->getAttribute(self::ATTRIBUTE_GROUP) : null; $modelClassName = $modelElement->hasAttribute(self::ATTRIBUTE_MODEL_CLASS) ? $modelElement->getAttribute(self::ATTRIBUTE_MODEL_CLASS) : ModelManager::DEFAULT_MODEL; $dataClassName = $modelElement->hasAttribute(self::ATTRIBUTE_DATA_CLASS) ? $modelElement->getAttribute(self::ATTRIBUTE_DATA_CLASS) : ModelMeta::CLASS_DATA; $isLogged = $modelElement->getAttribute(self::ATTRIBUTE_LOG) ? Boolean::getBoolean($modelElement->getAttribute(self::ATTRIBUTE_LOG)) : false; $willBlockDeleteWhenUsed = $modelElement->getAttribute(self::ATTRIBUTE_WILL_BLOCK_DELETE) ? Boolean::getBoolean($modelElement->getAttribute(self::ATTRIBUTE_WILL_BLOCK_DELETE)) : false; $modelTable = new ModelTable($modelName, $isLogged); $modelTable->setGroup($group); $modelTable->setWillBlockDeleteWhenUsed($willBlockDeleteWhenUsed); $fields = $this->getFieldsFromElement($modelElement, $file, $modelName); foreach ($fields as $field) { $modelTable->addField($field); } $this->setIndexesFromElement($modelElement, $modelTable); $this->setFormatsFromElement($modelElement, $modelTable); $modelMeta = new ModelMeta($modelTable, $dataClassName); $objectFactory = new ObjectFactory(); return $objectFactory->create($modelClassName, ModelManager::INTERFACE_MODEL, array($modelMeta)); }
/** * Registers the localized model of the provided model if needed * @param zibo\library\orm\model\Model $model * @return null */ private function registerLocalizedModel(Model $model) { if (!$model->getMeta()->isLocalized()) { return; } $localizedModelName = $model->getName() . LocalizedModel::MODEL_SUFFIX; $modelTable = $model->getMeta()->getModelTable(); $group = $modelTable->getGroup(); $dataField = new BelongsToField(LocalizedModel::FIELD_DATA, $model->getName()); $localeField = new PropertyField(LocalizedModel::FIELD_LOCALE, 'string'); $localeIndex = new Index(LocalizedModel::FIELD_LOCALE, array($localeField)); $dataLocaleIndex = new Index(LocalizedModel::FIELD_LOCALE . ucfirst(LocalizedModel::FIELD_DATA), array($localeField, $dataField)); $localizedModelTable = new ModelTable($localizedModelName, $modelTable->isLogged()); $localizedModelTable->addField($dataField); $localizedModelTable->addField($localeField); $localizedModelTable->addIndex($localeIndex); $localizedModelTable->addIndex($dataLocaleIndex); if ($group) { $localizedModelTable->setGroup($group); } $fields = $model->getMeta()->getFields(); foreach ($fields as $fieldName => $field) { if ($fieldName == ModelTable::PRIMARY_KEY || !$field->isLocalized()) { continue; } $field = clone $field; $field->setIsLocalized(false); $localizedModelTable->addField($field); } $localizedModel = new LocalizedModel(new ModelMeta($localizedModelTable)); $this->registerModel($localizedModel, false); }