diff --git a/documentation/reference.md b/documentation/reference.md index 5448db2..82d5b83 100644 --- a/documentation/reference.md +++ b/documentation/reference.md @@ -46,4 +46,3 @@ names. There are several drivers available: quick hack, but does not produce meaningful RDF. Look at the driver phpdoc for the exact syntax to use for configuration. - diff --git a/src/Midgard/CreatePHP/Mapper/DoctrineOrmMapper.php b/src/Midgard/CreatePHP/Mapper/DoctrineOrmMapper.php index 7d243dc..c1f127e 100644 --- a/src/Midgard/CreatePHP/Mapper/DoctrineOrmMapper.php +++ b/src/Midgard/CreatePHP/Mapper/DoctrineOrmMapper.php @@ -40,46 +40,61 @@ class DoctrineOrmMapper extends BaseDoctrineRdfMapper /** * {@inheritDoc} * - * The ORM implementation for preparing the object + * In addition to create the object, try to find the parent relation if one exists. */ public function prepareObject(TypeInterface $type, $parent = null) { $object = parent::prepareObject($type); + $config = $type->getConfig(); + $needParent = isset($config['parent_required']) && (bool) $config['parent_required']; + if (null == $parent) { - throw new RuntimeException('You need a parent to create new objects'); + if ($needParent) { + throw new RuntimeException(sprintf('Parent is required for object of type %s', get_class($object))); + } + return $object; } - /** @var \Doctrine\ORM\Mapping\ClassMetadata $metaData */ + /** @var ClassMetadata $metaData */ $metaData = $this->om->getClassMetaData(get_class($object)); $parentMappingField = $this->findParentMapping($parent, $metaData); + if (false === $parentMappingField) { + if ($needParent) { + throw new RuntimeException(sprintf('No mapping for parent class %s found in metadata of %s', ClassUtils::getClass($parent), $metaData->getName())); + } + + return $object; + } + $metaData->setFieldValue($object, $parentMappingField, $parent); - + return $object; } - /** - * find the parent object's property which holds the collection entries + * Find the parent object's property which holds the collection entries + * + * @param object $parent The parent entity. + * @param ClassMetadata $metaData Metadata of the object that is being created. * - * @param $parent - * @param ClassMetadata $metaData metadata from the collection entry's entity - * @param $object - * @return string - * @throws RunTimeException + * @return string|boolean The name of the parent mapping field or false if none is found. */ - protected function findParentMapping($parent, ClassMetadata $metaData, $object = null ) + protected function findParentMapping($parent, ClassMetadata $metaData) { - $parentClass = ClassUtils::getRealClass(get_class($parent)); + $parentClass = ClassUtils::getClass($parent); foreach ($metaData->associationMappings as $mapping) { - if ($mapping['targetEntity'] == $parentClass) { + if ($mapping['targetEntity'] == $parentClass + && $metaData->isSingleValuedAssociation($mapping['fieldName']) + ) { return $mapping['fieldName']; } } - throw new RuntimeException(sprintf('No mapping for parent class %s found in metadata of %s', $parentClass, $metaData->getName())); + + return false; } - + /** * {@inheritDoc} *