Fix parent mapping

This commit is contained in:
Antonio J. García Lagar 2013-06-17 21:19:11 +02:00
parent 9d5a0c2ed9
commit a386ffefe3
7 changed files with 48 additions and 31 deletions

View file

@ -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;
}
}

View file

@ -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));
}
}

View file

@ -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`

View file

@ -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;

View file

@ -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()),
),
),

View file

@ -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());
}

View file

@ -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