Merge pull request #549 from ahmedmhmd/master

Ensure non-empty properties under fields mappings in case of auto-mapped objects exists along with manual ones
This commit is contained in:
Tim Nagel 2014-04-20 11:52:03 +10:00
commit 1748e9af18
2 changed files with 51 additions and 1 deletions

View file

@ -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();

View file

@ -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.
*/