From fab42fa0ceef2c5c5966f8cbf06480aac13cbfbe Mon Sep 17 00:00:00 2001 From: Patrick McAndrew Date: Fri, 24 May 2013 12:27:37 +0100 Subject: [PATCH] Properities should not be required for object as elastica can automap --- README.md | 8 +++++++ .../ModelToElasticaAutoTransformerTest.php | 24 +++++++++++++++++++ .../ModelToElasticaAutoTransformer.php | 2 +- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 555f5b1..fac11d1 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,8 @@ per type. ### Declaring `nested` or `object` +Note that object can autodetect properties + fos_elastica: clients: default: { host: localhost, port: 9200 } @@ -204,6 +206,12 @@ per type. properties: date: { boost: 5 } content: ~ + user: + type: "object" + approver: + type: "object" + properties: + date: { boost: 5 } ### Populate the types diff --git a/Tests/Transformer/ModelToElasticaAutoTransformerTest.php b/Tests/Transformer/ModelToElasticaAutoTransformerTest.php index cbcb6bd..080cbfe 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'); @@ -268,6 +273,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 38bd065..ebeacfb 100644 --- a/Transformer/ModelToElasticaAutoTransformer.php +++ b/Transformer/ModelToElasticaAutoTransformer.php @@ -72,7 +72,7 @@ class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterf continue; } - 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. */