Make the property param optional

This commit is contained in:
Antonio J. García Lagar 2013-06-19 13:57:15 +02:00
commit 7f3cfa49fb
4 changed files with 36 additions and 4 deletions

View file

@ -511,7 +511,7 @@ class Configuration implements ConfigurationInterface
return $node;
}
/**
* Returns the array node used for "_parent".
*/
@ -523,7 +523,7 @@ class Configuration implements ConfigurationInterface
$node
->children()
->scalarNode('type')->end()
->scalarNode('property')->end()
->scalarNode('property')->defaultValue(null)->end()
->scalarNode('identifier')->defaultValue('id')->end()
->end()
;

View file

@ -182,6 +182,12 @@ per type.
content: ~
_parent: { type: "post", property: "post", identifier: "id" }
The parent filed declaration has the following values:
* `type`: The parent type.
* `property`: The property in the child entity where to look for the parent entity. It may be ignored if is equal to the parent type.
* `identifier`: The property in the parent entity which have the parent identifier. Defaults to `id`.
### Declaring `nested` or `object`
fos_elastica:

View file

@ -106,6 +106,11 @@ class POPO
{
return (object) array('id' => 'parent', 'name' => 'a random name');
}
public function getUpperAlias()
{
return $this->getUpper();
}
}
class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
@ -288,6 +293,26 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals("a random name", $document->getParent());
}
public function testParentMappingWithNullProperty()
{
$transformer = $this->getTransformer();
$document = $transformer->transform(new POPO(), array(
'_parent' => array('type' => 'upper', 'property'=>null, 'identifier' => 'id'),
));
$this->assertEquals("parent", $document->getParent());
}
public function testParentMappingWithCustomProperty()
{
$transformer = $this->getTransformer();
$document = $transformer->transform(new POPO(), array(
'_parent' => array('type' => 'upper', 'property'=>'upperAlias', 'identifier' => 'id'),
));
$this->assertEquals("parent", $document->getParent());
}
/**
* @return ModelToElasticaAutoTransformer
*/

View file

@ -63,11 +63,12 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf
foreach ($fields as $key => $mapping) {
if ($key == '_parent') {
$value = $this->propertyAccessor->getValue($object, $mapping['property']);
$property = (null !== $mapping['property'])?$mapping['property']:$mapping['type'];
$value = $this->propertyAccessor->getValue($object, $property);
$document->setParent($this->propertyAccessor->getValue($value, $mapping['identifier']));
continue;
}
$value = $this->propertyAccessor->getValue($object, $key);
if (isset($mapping['type']) && in_array($mapping['type'], array('nested', 'object'))) {