protected function createModel($name, array $fields) { $table = new ModelTable($name); foreach ($fields as $field) { $table->addField($field); } $meta = new ModelMeta($table); return new SimpleModel($meta); }
/** * Copies a model table into another * @param zibo\library\orm\definition\ModelTable $source * @param zibo\library\orm\definition\ModelTable $destination * @return null */ private function copyModel(ModelTable $source, ModelTable $destination) { $destination->setWillBlockDeleteWhenUsed($source->willBlockDeleteWhenUsed); $fields = $source->getFields(); foreach ($fields as $field) { if ($field->getName() == ModelTable::PRIMARY_KEY) { continue; } $destination->addField($field); } $indexes = $source->getIndexes(); foreach ($indexes as $index) { $destination->addIndex($index); } $dataFormats = $source->getDataFormats(); foreach ($dataFormats as $name => $format) { $destination->setDataFormat($name, $format); } }
public function testHasIndex() { $table = new ModelTable('table'); $field = new PropertyField('field', 'type'); $index = new Index('index', array($field)); $table->addField($field); $table->addIndex($index); $this->assertTrue($table->hasIndex('index')); $this->assertFalse($table->hasIndex('unexistant')); }
/** * 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)); }
/** * Creates a link model and registers it * @param string $linkModelName Name of the new link model * @param string $modelName1 Name of the first model * @param string $modelName2 Name of the second model * @return zibo\library\orm\model\Model The registered model */ private function createAndRegisterLinkModel($linkModelName, $modelName1, $modelName2) { $table = new ModelTable($linkModelName); $indexName = lcfirst($this->generateLinkModelName($modelName1, $modelName2)); $fieldName1 = lcfirst($modelName1); $fieldName2 = lcfirst($modelName2); if ($modelName1 == $modelName2) { $field1 = new BelongsToField($fieldName1 . '1', $modelName1); $field2 = new BelongsToField($fieldName1 . '2', $modelName2); } else { $field1 = new BelongsToField($fieldName1, $modelName1); $field2 = new BelongsToField($fieldName2, $modelName2); } $index = new Index($indexName, array($field1, $field2)); $table->addField($field1); $table->addField($field2); $table->addIndex($index); $linkModel = new SimpleModel(new ModelMeta($table)); $this->registerModel($linkModel, false); return $linkModel; }