diff --git a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php index 646d1a4..1fa6a8e 100644 --- a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php +++ b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php @@ -107,6 +107,11 @@ class POPO return array('foo' => 'foo', 'bar' => 'foo', 'id' => 1); } + public function getNestedObject() + { + return array('key1' => (object)array('id' => 1, 'key1sub1' => 'value1sub1', 'key1sub2' => 'value1sub2')); + } + public function getUpper() { return (object) array('id' => 'parent', 'name' => 'a random name'); @@ -296,6 +301,51 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase ), $data['obj']); } + public function testObjectsMappingOfAtLeastOneAutoMappedObjectAndAtLeastOneManuallyMappedObject() + { + $transformer = $this->getTransformer(); + $document = $transformer->transform( + new POPO(), + array( + 'obj' => array('type' => 'object', 'properties' => array()), + 'nestedObject' => array( + 'type' => 'object', + 'properties' => array( + 'key1sub1' => array( + 'type' => 'string', + 'properties' => array() + ), + 'key1sub2' => array( + 'type' => 'string', + 'properties' => array() + ) + ) + ) + ) + ); + $data = $document->getData(); + + $this->assertTrue(array_key_exists('obj', $data)); + $this->assertTrue(array_key_exists('nestedObject', $data)); + $this->assertInternalType('array', $data['obj']); + $this->assertInternalType('array', $data['nestedObject']); + $this->assertEquals( + array( + 'foo' => 'foo', + 'bar' => 'foo', + 'id' => 1 + ), + $data['obj'] + ); + $this->assertEquals( + array( + 'key1sub1' => 'value1sub1', + 'key1sub2' => 'value1sub2' + ), + $data['nestedObject'][0] + ); + } + public function testParentMapping() { $transformer = $this->getTransformer(); diff --git a/Transformer/ModelToElasticaAutoTransformer.php b/Transformer/ModelToElasticaAutoTransformer.php index 3107d0a..9694a3b 100644 --- a/Transformer/ModelToElasticaAutoTransformer.php +++ b/Transformer/ModelToElasticaAutoTransformer.php @@ -71,7 +71,7 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf $value = $this->propertyAccessor->getValue($object, $key); - if (isset($mapping['type']) && in_array($mapping['type'], array('nested', 'object')) && isset($mapping['properties'])) { + if (isset($mapping['type']) && in_array($mapping['type'], array('nested', 'object')) && isset($mapping['properties']) && !empty($mapping['properties'])) { /* $value is a nested document or object. Transform $value into * an array of documents, respective the mapped properties. */