Skip to content
This repository has been archived by the owner on Jan 1, 2020. It is now read-only.

Commit

Permalink
parent is optional for orm
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed May 15, 2015
1 parent b3a7ad3 commit c489a3d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
1 change: 0 additions & 1 deletion documentation/reference.md
Expand Up @@ -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.

47 changes: 31 additions & 16 deletions src/Midgard/CreatePHP/Mapper/DoctrineOrmMapper.php
Expand Up @@ -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}
*
Expand Down

0 comments on commit c489a3d

Please sign in to comment.