Inheritance: implements Jarves\Admin\FieldTypes\RelationDefinitionInterface
Ejemplo n.º 1
0
 public function bootRunTime(Object $object, Configs $configs)
 {
     if (!$object->hasRelation($this->getFieldDefinition()->getId())) {
         $relation = new RelationDefinition();
         $relation->setName($this->getFieldDefinition()->getId());
         $relation->setType(AbstractStorage::ONE_TO_MANY);
         $relation->setForeignObjectKey('jarves/content');
         $relation->setRefName($object->getId());
         $object->addRelation($relation);
         $configs->addReboot('Added auto relation because of PageContents type.');
     }
     if (!$object->hasField('layout')) {
         $field = new Field();
         $field->setId('layout');
         $field->setType('text');
         $object->addField($field);
         $configs->addReboot('PageContents needs `layout` field.');
     }
     if (!$object->hasField('theme')) {
         $field = new Field();
         $field->setId('theme');
         $field->setType('text');
         $object->addField($field);
         $configs->addReboot('PageContents needs `theme field.');
     }
 }
Ejemplo n.º 2
0
 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()));
     }
 }
Ejemplo n.º 3
0
 /**
  * @param Configs $configs
  * @return RelationDefinition|null
  * @throws \Jarves\Exceptions\ModelBuildException
  */
 protected function getRelation(Configs $configs)
 {
     $field = $this->getFieldDefinition();
     $columns = [];
     $foreignObjectDefinition = $configs->getObject($field->getObject());
     if (!$foreignObjectDefinition) {
         throw new ModelBuildException(sprintf('ObjectKey `%s` not found for field `%s` in object `%s`', $field->getObject(), $field->getId(), $field->getObjectDefinition()->getId()));
     }
     $relation = new RelationDefinition();
     $relation->setName($field->getId());
     $relation->setType($field->getObjectRelation());
     $relation->setForeignObjectKey($field->getObject());
     $relation->setWithConstraint($field->getObjectRelationWithConstraint());
     if ($refName = $field->getObjectRefRelationName()) {
         $relation->setRefName($refName);
     }
     foreach ($foreignObjectDefinition->getPrimaryKeys() as $pk) {
         $fieldColumns = $pk->getFieldType()->getColumns();
         $columns = array_merge($columns, $fieldColumns);
     }
     if (!$columns) {
         return null;
     }
     $references = [];
     foreach ($columns as $column) {
         $reference = new RelationReferenceDefinition();
         $localColumn = clone $column;
         $localColumn->setName($field->getId() . ucfirst($column->getName()));
         $reference->setLocalColumn($localColumn);
         $reference->setForeignColumn($column);
         $references[] = $reference;
     }
     $relation->setReferences($references);
     return $relation;
 }