diff --git a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php index 7f2595c..6f2aaea 100644 --- a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php +++ b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php @@ -92,6 +92,19 @@ class POPO { return $this->file; } + + public function getSub() + { + return array( + (object) array('foo' => 'foo', 'bar' => 'foo', 'id' => 1), + (object) array('foo' => 'bar', 'bar' => 'bar', 'id' => 2), + ); + } + + public function getUpper() + { + return (object) array('id' => 'parent', 'name' => 'a random name'); + } } class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase @@ -215,4 +228,66 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase base64_encode(file_get_contents(__DIR__ . '/../fixtures/attachment.odt')), $data['fileContents'] ); } + + public function testNestedMapping() + { + $transformer = new ModelToElasticaAutoTransformer(); + $document = $transformer->transform(new POPO(), array( + 'sub' => array( + 'type' => 'nested', + 'properties' => array('foo' => '~') + ) + )); + $data = $document->getData(); + + $this->assertTrue(array_key_exists('sub', $data)); + $this->assertInternalType('array', $data['sub']); + $this->assertEquals(array( + array('foo' => 'foo'), + array('foo' => 'bar') + ), $data['sub']); + } + + public function tesObjectMapping() + { + $transformer = new ModelToElasticaAutoTransformer(); + $document = $transformer->transform(new POPO(), array( + 'sub' => array( + 'type' => 'object', + 'properties' => array('bar') + ) + )); + $data = $document->getData(); + + $this->assertTrue(array_key_exists('sub', $data)); + $this->assertInternalType('array', $data['sub']); + $this->assertEquals(array( + array('bar' => 'foo'), + array('bar' => 'bar') + ), $data['sub']); + } + + public function testParentMapping() + { + $transformer = new ModelToElasticaAutoTransformer(); + $document = $transformer->transform(new POPO(), array( + 'upper' => array( + '_parent' => array('type' => 'upper', 'identifier' => 'id'), + ) + )); + + $this->assertEquals("parent", $document->getParent()); + } + + public function testParentMappingWithCustomIdentifier() + { + $transformer = new ModelToElasticaAutoTransformer(); + $document = $transformer->transform(new POPO(), array( + 'upper' => array( + '_parent' => array('type' => 'upper', 'identifier' => 'name'), + ) + )); + + $this->assertEquals("a random name", $document->getParent()); + } } diff --git a/Transformer/ModelToElasticaAutoTransformer.php b/Transformer/ModelToElasticaAutoTransformer.php index 17cbc30..172d31d 100644 --- a/Transformer/ModelToElasticaAutoTransformer.php +++ b/Transformer/ModelToElasticaAutoTransformer.php @@ -45,7 +45,7 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf $document = new \Elastica_Document($identifier); foreach ($fields as $key => $mapping) { $property = new PropertyPath($key); - if (isset($mapping['_parent'])) { + if (!empty($mapping['_parent'])) { $parent = $property->getValue($object); $identifierProperty = new PropertyPath($mapping['_parent']['identifier']); $document->setParent($identifierProperty->getValue($parent));