public function bootRunTime(Object $object, Configs $configs) { $contentsObjectName = $object->getId() . ucfirst($this->getFieldDefinition()->getId()); $contentsObject = $object->getBundle()->getObject($contentsObjectName); if (!$contentsObject) { $contentsObject = new Object(); $contentsObject->setId($contentsObjectName); if ($object->getWorkspace()) { $contentsObject->setWorkspace(true); } $contentsObject->setAutoCrud(false); $contentsObject->setSearchable(false); $contentsObject->setExcludeFromREST(true); $contentsObject->setNested(true); $contentsObject->setNestedRootAsObject(true); $contentsObject->setNestedRootObject($object->getKey()); $contentsObject->setNestedRootObjectField('foreignId'); $contentsObject->setTable($object->getTable() . '_' . Tools::camelcase2Underscore($this->getFieldDefinition()->getId())); $contentsObject->setStorageService($object->getStorageService()); } $fields = ['id' => ['type' => 'number', 'autoIncrement' => true, 'primaryKey' => true], 'foreignId' => ['type' => 'number'], 'slotId' => ['type' => 'number'], 'sort' => ['type' => 'number'], 'content' => ['type' => 'textarea'], 'template' => ['type' => 'view'], 'type' => ['type' => 'text'], 'hide' => ['type' => 'checkbox'], 'unsearchable' => ['type' => 'checkbox'], 'access_from' => ['type' => 'datetime'], 'access_to' => ['type' => 'datetime'], 'access_from_groups' => ['type' => 'text']]; foreach ($fields as $k => $def) { if (!$contentsObject->getField($k)) { $def['id'] = $k; $field = new Field($def, $object->getJarves()); $contentsObject->addField($field); $configs->addReboot(sprintf('[ContentElements] Added field %s to %s', $k, $contentsObject->getKey())); } } if (!$contentsObject->hasRelation('ForeignObject')) { $relation = new RelationDefinition(); $relation->setName('ForeignObject'); $relation->setType(AbstractStorage::MANY_TO_ONE); $relation->setForeignObjectKey($object->getKey()); $relation->setRefName(ucfirst($this->getFieldDefinition()->getId())); $reference = new RelationReferenceDefinition(); $primaryFields = $object->getPrimaryKeys(); if (1 < count($primaryFields)) { throw new ModelBuildException(sprintf('FieldType `ContentElements` can not be used on the object `%s` with composite PrimaryKey', $object->getId())); } if (0 === count($primaryFields)) { throw new ModelBuildException(sprintf('FieldType `ContentElements` can not be used on the object `%s` with no PrimaryKey', $object->getId())); } $columns = $primaryFields[0]->getFieldType()->getColumns(); if (1 < count($columns)) { throw new ModelBuildException(sprintf('FieldType `ContentElements` can not be used on the object `%s` with composite PrimaryKey', $object->getId())); } $reference->setForeignColumn($columns[0]); $field = $contentsObject->getField('foreignId'); $columns = $field->getFieldType()->getColumns(); $reference->setLocalColumn($columns[0]); $relation->setReferences([$reference]); $contentsObject->addRelation($relation); $configs->addReboot(sprintf('[ContentElements] Added relation ForeignObject to %s', $contentsObject->getKey())); } if (!$contentsObject->getBundle()) { $object->getBundle()->addObject($contentsObject); } if (!$object->hasRelation($this->getFieldDefinition()->getId())) { $relation = new RelationDefinition(); $relation->setName(ucfirst($this->getFieldDefinition()->getId())); $relation->setType(AbstractStorage::ONE_TO_MANY); $relation->setForeignObjectKey($contentsObject->getKey()); $relation->setRefName('ForeignObject'); $reference = new RelationReferenceDefinition(); $primaryFields = $object->getPrimaryKeys(); $columns = $primaryFields[0]->getFieldType()->getColumns(); $reference->setLocalColumn($columns[0]); $field = $contentsObject->getField('foreignId'); $columns = $field->getFieldType()->getColumns(); $reference->setForeignColumn($columns[0]); $relation->setReferences([$reference]); $object->addRelation($relation); $configs->addReboot(sprintf('[ContentElements] Added relation %s to %s', ucfirst($this->getFieldDefinition()->getId()), $object->getKey())); } }
protected function addForeignKey(Object $object, RelationDefinitionInterface $relation, &$xmlTable) { $relationName = $relation->getName(); $foreignObject = $this->objects->getDefinition($relation->getForeignObjectKey()); if (!$foreignObject) { throw new ModelBuildException(sprintf('Foreign object `%s` does not exist in relation `%s`', $relation->getForeignObjectKey(), $relation->getName())); } if ($object->getStorageService() !== $foreignObject->getStorageService()) { throw new ModelBuildException(sprintf('Can not create a relation between two different dataModels. Got `%s` but `%s` is needed.', $foreignObject->getStorageService(), $object->getStorageService())); } $pluralizer = new StandardEnglishPluralizer(); $foreignPhpName = ucfirst($pluralizer->getSingularForm(lcfirst($relationName))); $foreigns = $xmlTable->xpath('foreign-key[@phpName=\'' . $foreignPhpName . '\']'); if ($foreigns) { $foreignKey = current($foreigns); } else { $foreignKey = $xmlTable->addChild('foreign-key'); } $foreignKey['phpName'] = $foreignPhpName; $foreignKey['foreignTable'] = $foreignObject->getTable(); if ($refName = $relation->getRefName()) { $foreignKey['refPhpName'] = ucfirst($pluralizer->getSingularForm(lcfirst($refName))); } $foreignKey['onDelete'] = $relation->getOnDelete(); $foreignKey['onUpdate'] = $relation->getOnUpdate(); if (!$relation->getWithConstraint()) { $foreignKey['skipSql'] = 'true'; } $references = $foreignKey->xpath("reference[not(@custom='true')]"); foreach ($references as $i => $ref) { unset($references[$i][0]); } foreach ($relation->getReferences() as $reference) { $localName = Tools::camelcase2Underscore($reference->getLocalColumn()->getName()); $references = $foreignKey->xpath('reference[@local=\'' . $localName . '\']'); if ($references) { $xmlReference = current($references); } else { $xmlReference = $foreignKey->addChild('reference'); } $xmlReference['local'] = $localName; $xmlReference['foreign'] = Tools::camelcase2Underscore($reference->getForeignColumn()->getName()); } if ($foreignObject->getWorkspace()) { if (!$object->getWorkspace()) { $columns = $xmlTable->xpath('column[@name=\'workspace_id\']'); if (!$columns) { $newCol = $xmlTable->addChild('column'); $newCol['name'] = 'workspace_id'; $newCol['type'] = 'INTEGER'; $newCol['defaultValue'] = '1'; } } $localName = 'workspace_id'; $references = $foreignKey->xpath('reference[@local=\'' . $localName . '\']'); if ($references) { $xmlReference = current($references); } else { $xmlReference = $foreignKey->addChild('reference'); } $xmlReference['local'] = $localName; $xmlReference['foreign'] = 'workspace_id'; } }
public function testObjectItemArray() { $xml = ' <object id="Item"> <label>title</label> <table>test_item</table> <labelField>title</labelField> <nested>false</nested> <multiLanguage>false</multiLanguage> <workspace>true</workspace> <domainDepended>false</domainDepended> <treeFixedIcon>false</treeFixedIcon> <fields> <field id="id" type="number" primaryKey="true" autoIncrement="true"> </field> <field id="title" type="text"> </field> <field id="category" type="object"> <object>test/itemCategory</object> <objectRelation>nToM</objectRelation> </field> <field id="oneCategory" type="object"> <object>test/itemCategory</object> <objectRelation>nTo1</objectRelation> </field> </fields> </object>'; $object = new Object($xml, $this->getJarves()); $array = $object->toArray(); $this->assertEquals('Item', $object->getId()); $this->assertEquals('title', $object->getLabel()); $this->assertEquals('test_item', $object->getTable()); $this->assertTrue($object->getWorkspace()); $this->assertCount(4, $object->getFields()); $this->assertEquals('Item', $array['id']); $this->assertEquals('title', $array['label']); $this->assertEquals('test_item', $array['table']); $this->assertTrue($array['workspace']); $this->assertCount(4, $array['fields']); }