diff --git a/README.md b/README.md index 9792139..6c9a018 100644 --- a/README.md +++ b/README.md @@ -193,6 +193,8 @@ If you do this wrong, you will see a `RoutingMissingException` as elasticsearch ### Declaring `nested` or `object` +Note that object can autodetect properties + fos_elastica: clients: default: { host: localhost, port: 9200 } @@ -213,6 +215,12 @@ If you do this wrong, you will see a `RoutingMissingException` as elasticsearch properties: date: { boost: 5 } content: ~ + user: + type: "object" + approver: + type: "object" + properties: + date: { boost: 5 } #### Doctrine ORM and `object` mappings diff --git a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php index d2c181a..646d1a4 100644 --- a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php +++ b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php @@ -102,6 +102,11 @@ class POPO ); } + public function getObj() + { + return array('foo' => 'foo', 'bar' => 'foo', 'id' => 1); + } + public function getUpper() { return (object) array('id' => 'parent', 'name' => 'a random name'); @@ -272,6 +277,25 @@ class ModelToElasticaAutoTransformerTest extends \PHPUnit_Framework_TestCase ), $data['sub']); } + public function testObjectDoesNotRequireProperties() + { + $transformer = $this->getTransformer(); + $document = $transformer->transform(new POPO(), array( + 'obj' => array( + 'type' => 'object' + ) + )); + $data = $document->getData(); + + $this->assertTrue(array_key_exists('obj', $data)); + $this->assertInternalType('array', $data['obj']); + $this->assertEquals(array( + 'foo' => 'foo', + 'bar' => 'foo', + 'id' => 1 + ), $data['obj']); + } + public function testParentMapping() { $transformer = $this->getTransformer(); diff --git a/Transformer/ModelToElasticaAutoTransformer.php b/Transformer/ModelToElasticaAutoTransformer.php index 6e01907..e960dc5 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'))) { + if (isset($mapping['type']) && in_array($mapping['type'], array('nested', 'object')) && isset($mapping['properties'])) { /* $value is a nested document or object. Transform $value into * an array of documents, respective the mapped properties. */