From a386ffefe332d23e7ac9bcfcb71e667302a49d47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20J=2E=20Garc=C3=ADa=20Lagar?= Date: Mon, 17 Jun 2013 21:19:11 +0200 Subject: [PATCH] Fix parent mapping --- DependencyInjection/Configuration.php | 28 ++++++++++++++----- DependencyInjection/FOSElasticaExtension.php | 6 +++- README.md | 2 +- Resetter.php | 6 ++-- Tests/ResetterTest.php | 13 +++++++-- .../ModelToElasticaAutoTransformerTest.php | 12 +++----- .../ModelToElasticaAutoTransformer.php | 12 ++++---- 7 files changed, 48 insertions(+), 31 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 79cadf2..da63a2d 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -287,6 +287,7 @@ class Configuration implements ConfigurationInterface ->append($this->getSourceNode()) ->append($this->getBoostNode()) ->append($this->getRoutingNode()) + ->append($this->getParentNode()) ->end() ; @@ -338,13 +339,7 @@ class Configuration implements ConfigurationInterface ->scalarNode('index_options')->end() ->scalarNode('ignore_above')->end() ->scalarNode('position_offset_gap')->end() - ->arrayNode('_parent') - ->treatNullLike(array()) - ->children() - ->scalarNode('type')->end() - ->scalarNode('identifier')->defaultValue('id')->end() - ->end() - ->end(); + ; if (isset($nestings['fields'])) { $this->addNestedFieldConfig($node, $nestings, 'fields'); @@ -516,4 +511,23 @@ class Configuration implements ConfigurationInterface return $node; } + + /** + * Returns the array node used for "_parent". + */ + protected function getParentNode() + { + $builder = new TreeBuilder(); + $node = $builder->root('_parent'); + + $node + ->children() + ->scalarNode('type')->end() + ->scalarNode('property')->end() + ->scalarNode('identifier')->defaultValue('id')->end() + ->end() + ; + + return $node; + } } diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index c28fb99..d83b604 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -214,6 +214,11 @@ class FOSElasticaExtension extends Extension $typeName = sprintf('%s/%s', $indexName, $name); $this->typeFields[$typeName] = $type['mappings']; } + if (isset($type['_parent'])) { + $this->indexConfigs[$indexName]['config']['mappings'][$name]['_parent'] = array('type' => $type['_parent']['type']); + $typeName = sprintf('%s/%s', $indexName, $name); + $this->typeFields[$typeName]['_parent'] = $type['_parent']; + } if (isset($type['persistence'])) { $this->loadTypePersistenceIntegration($type['persistence'], $container, $typeDef, $indexName, $name); } @@ -476,5 +481,4 @@ class FOSElasticaExtension extends Extension $container->setAlias('fos_elastica.manager', sprintf('fos_elastica.manager.%s', $defaultManagerService)); } - } diff --git a/README.md b/README.md index e575f88..47789b7 100644 --- a/README.md +++ b/README.md @@ -178,9 +178,9 @@ per type. types: comment: mappings: - post: {_parent: { type: "post", identifier: "id" } } date: { boost: 5 } content: ~ + _parent: { type: "post", property: "post", identifier: "id" } ### Declaring `nested` or `object` diff --git a/Resetter.php b/Resetter.php index 7ef2895..9a9da0b 100644 --- a/Resetter.php +++ b/Resetter.php @@ -74,10 +74,8 @@ class Resetter { $mapping = Mapping::create($indexConfig['properties']); - foreach($indexConfig['properties'] as $type) { - if (!empty($type['_parent']) && $type['_parent'] !== '~') { - $mapping->setParam('_parent', array('type' => $type['_parent']['type'])); - } + if (isset($indexConfig['_parent'])) { + $mapping->setParam('_parent', array('type' => $indexConfig['_parent']['type'])); } return $mapping; diff --git a/Tests/ResetterTest.php b/Tests/ResetterTest.php index 1a7190e..18cbe06 100644 --- a/Tests/ResetterTest.php +++ b/Tests/ResetterTest.php @@ -34,9 +34,16 @@ class ResetterTest extends \PHPUnit_Framework_TestCase 'index' => $this->getMockElasticaIndex(), 'config' => array( 'mappings' => array( - 'a' => array('properties' => array( - 'field_1' => array('_parent' => array('type' => 'b', 'identifier' => 'id')), - 'field_2' => array())), + 'a' => array( + 'properties' => array( + 'field_2' => array() + ), + '_parent' => array( + 'type' => 'b', + 'property' => 'b', + 'identifier' => 'id' + ), + ), 'b' => array('properties' => array()), ), ), diff --git a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php index cbcb6bd..8b6b3ae 100644 --- a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php +++ b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php @@ -272,10 +272,8 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase { $transformer = $this->getTransformer(); $document = $transformer->transform(new POPO(), array( - 'upper' => array( - '_parent' => array('type' => 'upper', 'identifier' => 'id'), - ) - )); + '_parent' => array('type' => 'upper', 'property'=>'upper', 'identifier' => 'id'), + )); $this->assertEquals("parent", $document->getParent()); } @@ -284,10 +282,8 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase { $transformer = $this->getTransformer(); $document = $transformer->transform(new POPO(), array( - 'upper' => array( - '_parent' => array('type' => 'upper', 'identifier' => 'name'), - ) - )); + '_parent' => array('type' => 'upper', 'property'=>'upper', 'identifier' => 'name'), + )); $this->assertEquals("a random name", $document->getParent()); } diff --git a/Transformer/ModelToElasticaAutoTransformer.php b/Transformer/ModelToElasticaAutoTransformer.php index 38bd065..5d8701a 100644 --- a/Transformer/ModelToElasticaAutoTransformer.php +++ b/Transformer/ModelToElasticaAutoTransformer.php @@ -62,15 +62,13 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf $document = new Document($identifier); foreach ($fields as $key => $mapping) { - $value = $this->propertyAccessor->getValue($object, $key); - - if (isset($mapping['_parent']['identifier'])) { - /* $value is the parent. Read its identifier and set that as the - * document's parent. - */ - $document->setParent($this->propertyAccessor->getValue($value, $mapping['_parent']['identifier'])); + if ($key == '_parent') { + $value = $this->propertyAccessor->getValue($object, $mapping['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'))) { /* $value is a nested document or object. Transform $value into