/** * @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; }
/** * All bundle configs have been loaded. * * @param Configs $configs */ public function boot(Configs $configs) { if ($this->objectAttributes) { foreach ($this->objectAttributes as $attribute) { $key = $attribute->getId(); try { $targetObject = $configs->getObject($attribute->getTarget()); } catch (BundleNotFoundException $e) { continue; } if (!$attribute->getTarget() || !$targetObject) { continue; } $field = $targetObject->getField($key); if (!$field) { //does not exists, so attach it $attribute->setAttribute(true); $targetObject->addField($attribute); $configs->addReboot(sprintf('Added field attribute %s to %s', $key, $targetObject->getId())); } } } if ($this->getObjects()) { foreach ($this->getObjects() as $object) { $object->bootRunTime($configs); } } }