Skip to content

Commit 5128373

Browse files
author
Joris van de Sande
committed
Don't use parent identifier for objects/nested documents
This fixes #357
1 parent d430b9a commit 5128373

File tree

2 files changed

+106
-34
lines changed

2 files changed

+106
-34
lines changed

Tests/Transformer/ModelToElasticaAutoTransformerTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ public function getUpperAlias()
121121
{
122122
return $this->getUpper();
123123
}
124+
125+
public function getObjWithoutIdentifier()
126+
{
127+
return (object) array('foo' => 'foo', 'bar' => 'foo');
128+
}
129+
130+
public function getSubWithoutIdentifier()
131+
{
132+
return array(
133+
(object) array('foo' => 'foo', 'bar' => 'foo'),
134+
(object) array('foo' => 'bar', 'bar' => 'bar'),
135+
);
136+
}
124137
}
125138

126139
class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase
@@ -379,6 +392,50 @@ public function testParentMappingWithCustomProperty()
379392
$this->assertEquals("parent", $document->getParent());
380393
}
381394

395+
public function testThatMappedObjectsDontNeedAnIdentifierField()
396+
{
397+
$transformer = $this->getTransformer();
398+
$document = $transformer->transform(new POPO(), array(
399+
'objWithoutIdentifier' => array(
400+
'type' => 'object',
401+
'properties' => array(
402+
'foo' => array(),
403+
'bar' => array()
404+
)
405+
),
406+
));
407+
$data = $document->getData();
408+
409+
$this->assertTrue(array_key_exists('objWithoutIdentifier', $data));
410+
$this->assertInternalType('array', $data['objWithoutIdentifier']);
411+
$this->assertEquals(array(
412+
'foo' => 'foo',
413+
'bar' => 'foo'
414+
), $data['objWithoutIdentifier']);
415+
}
416+
417+
public function testThatNestedObjectsDontNeedAnIdentifierField()
418+
{
419+
$transformer = $this->getTransformer();
420+
$document = $transformer->transform(new POPO(), array(
421+
'subWithoutIdentifier' => array(
422+
'type' => 'nested',
423+
'properties' => array(
424+
'foo' => array(),
425+
'bar' => array()
426+
),
427+
),
428+
));
429+
$data = $document->getData();
430+
431+
$this->assertTrue(array_key_exists('subWithoutIdentifier', $data));
432+
$this->assertInternalType('array', $data['subWithoutIdentifier']);
433+
$this->assertEquals(array(
434+
array('foo' => 'foo', 'bar' => 'foo'),
435+
array('foo' => 'bar', 'bar' => 'bar'),
436+
), $data['subWithoutIdentifier']);
437+
}
438+
382439
/**
383440
* @return ModelToElasticaAutoTransformer
384441
*/

Transformer/ModelToElasticaAutoTransformer.php

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -59,38 +59,7 @@ public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
5959
public function transform($object, array $fields)
6060
{
6161
$identifier = $this->propertyAccessor->getValue($object, $this->options['identifier']);
62-
$document = new Document($identifier);
63-
64-
foreach ($fields as $key => $mapping) {
65-
if ($key == '_parent') {
66-
$property = (null !== $mapping['property']) ? $mapping['property'] : $mapping['type'];
67-
$value = $this->propertyAccessor->getValue($object, $property);
68-
$document->setParent($this->propertyAccessor->getValue($value, $mapping['identifier']));
69-
continue;
70-
}
71-
72-
$value = $this->propertyAccessor->getValue($object, $key);
73-
74-
if (isset($mapping['type']) && in_array($mapping['type'], array('nested', 'object')) && isset($mapping['properties']) && !empty($mapping['properties'])) {
75-
/* $value is a nested document or object. Transform $value into
76-
* an array of documents, respective the mapped properties.
77-
*/
78-
$document->set($key, $this->transformNested($value, $mapping['properties']));
79-
continue;
80-
}
81-
82-
if (isset($mapping['type']) && $mapping['type'] == 'attachment') {
83-
// $value is an attachment. Add it to the document.
84-
if ($value instanceof \SplFileInfo) {
85-
$document->addFile($key, $value->getPathName());
86-
} else {
87-
$document->addFileContent($key, $value);
88-
}
89-
continue;
90-
}
91-
92-
$document->set($key, $this->normalizeValue($value));
93-
}
62+
$document = $this->transformObjectToDocument($object, $fields, $identifier);
9463

9564
return $document;
9665
}
@@ -108,13 +77,13 @@ protected function transformNested($objects, array $fields)
10877
if (is_array($objects) || $objects instanceof \Traversable || $objects instanceof \ArrayAccess) {
10978
$documents = array();
11079
foreach ($objects as $object) {
111-
$document = $this->transform($object, $fields);
80+
$document = $this->transformObjectToDocument($object, $fields);
11281
$documents[] = $document->getData();
11382
}
11483

11584
return $documents;
11685
} elseif (null !== $objects) {
117-
$document = $this->transform($objects, $fields);
86+
$document = $this->transformObjectToDocument($objects, $fields);
11887

11988
return $document->getData();
12089
}
@@ -148,4 +117,50 @@ protected function normalizeValue($value)
148117

149118
return $value;
150119
}
120+
121+
/**
122+
* Transforms the given object to an elastica document
123+
*
124+
* @param object $object the object to convert
125+
* @param array $fields the keys we want to have in the returned array
126+
* @param string $identifier the identifier for the new document
127+
* @return Document
128+
*/
129+
protected function transformObjectToDocument($object, array $fields, $identifier = '')
130+
{
131+
$document = new Document($identifier);
132+
133+
foreach ($fields as $key => $mapping) {
134+
if ($key == '_parent') {
135+
$property = (null !== $mapping['property']) ? $mapping['property'] : $mapping['type'];
136+
$value = $this->propertyAccessor->getValue($object, $property);
137+
$document->setParent($this->propertyAccessor->getValue($value, $mapping['identifier']));
138+
continue;
139+
}
140+
141+
$value = $this->propertyAccessor->getValue($object, $key);
142+
143+
if (isset($mapping['type']) && in_array($mapping['type'], array('nested', 'object')) && isset($mapping['properties']) && !empty($mapping['properties'])) {
144+
/* $value is a nested document or object. Transform $value into
145+
* an array of documents, respective the mapped properties.
146+
*/
147+
$document->set($key, $this->transformNested($value, $mapping['properties']));
148+
continue;
149+
}
150+
151+
if (isset($mapping['type']) && $mapping['type'] == 'attachment') {
152+
// $value is an attachment. Add it to the document.
153+
if ($value instanceof \SplFileInfo) {
154+
$document->addFile($key, $value->getPathName());
155+
} else {
156+
$document->addFileContent($key, $value);
157+
}
158+
continue;
159+
}
160+
161+
$document->set($key, $this->normalizeValue($value));
162+
}
163+
164+
return $document;
165+
}
151166
}

0 commit comments

Comments
 (0)