Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow _parent mapping

  • Loading branch information...
commit 23f591bf0190493c6f06ab66480181a13044a895 1 parent a901304
Julien Muetton authored
View
7 DependencyInjection/Configuration.php
@@ -267,6 +267,13 @@ protected function getMappingsNode()
->end()
->end()
->end()
+ ->arrayNode('_parent')
+ ->treatNullLike(array())
+ ->children()
+ ->scalarNode('type')->end()
+ ->scalarNode('identifier')->defaultValue('id')->end()
+ ->end()
+ ->end()
->arrayNode('properties')
->useAttributeAsKey('name')
->prototype('array')
View
16 Transformer/ModelToElasticaAutoTransformer.php
@@ -45,10 +45,14 @@ public function transform($object, array $fields)
$document = new \Elastica_Document($identifier);
foreach ($fields as $key => $mapping) {
$property = new PropertyPath($key);
- if (isset($mapping['type']) && in_array($mapping['type'], array('nested', 'object'))) {
- $submapping = $mapping['properties'];
+ if (isset($mapping['_parent'])) {
+ $parent = $property->getValue($object);
+ $identifierProperty = new PropertyPath($mapping['_parent']['identifier']);
+ $document->setParent($identifierProperty->getValue($parent));
+ } else if (isset($mapping['type']) && in_array($mapping['type'], array('nested', 'object'))) {
+ $submapping = $mapping['properties'];
$subcollection = $property->getValue($object);
- $document->add($key, $this->transformNested($subcollection, $submapping));
+ $document->add($key, $this->transformNested($subcollection, $submapping, $document));
} else if (isset($mapping['type']) && $mapping['type'] == 'multi_field') {
throw new \Exception('Please implement me !');
} else if (isset($mapping['type']) && $mapping['type'] == 'attachment') {
@@ -70,13 +74,15 @@ public function transform($object, array $fields)
*
* @param array $objects the object to convert
* @param array $fields the keys we want to have in the returned array
+ * @param Elastica_Document $parent the parent document
* @return array
*/
- protected function transformNested($objects, array $fields)
+ protected function transformNested($objects, array $fields, $parent)
{
$documents = array();
foreach($objects as $object) {
- $documents[] = $this->transform($object, $fields)->getData();
+ $document = $this->transform($object, $fields);
+ $documents[] = $document->getData();
}
return $documents;
Please sign in to comment.
Something went wrong with that request. Please try again.