From 7f3cfa49fb3f569d6a2d733221317992b54e17a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20J=2E=20Garc=C3=ADa=20Lagar?= Date: Wed, 19 Jun 2013 13:57:15 +0200 Subject: [PATCH] Make the property param optional --- DependencyInjection/Configuration.php | 4 +-- README.md | 6 +++++ .../ModelToElasticaAutoTransformerTest.php | 25 +++++++++++++++++++ .../ModelToElasticaAutoTransformer.php | 5 ++-- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index da63a2d..f9f633f 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -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() ; diff --git a/README.md b/README.md index 47789b7..0b6c3af 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php index 8b6b3ae..9990fa2 100644 --- a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php +++ b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php @@ -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 */ diff --git a/Transformer/ModelToElasticaAutoTransformer.php b/Transformer/ModelToElasticaAutoTransformer.php index 5d8701a..d99f301 100644 --- a/Transformer/ModelToElasticaAutoTransformer.php +++ b/Transformer/ModelToElasticaAutoTransformer.php @@ -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'))) {