From 8adcacfd7b47938acaef304fab8ba3927d17fb8c Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sat, 12 Jul 2014 09:49:06 +0200 Subject: [PATCH] Implemented fieldList for marshalling associations --- src/ORM/Marshaller.php | 12 +++------- tests/TestCase/ORM/MarshallerTest.php | 33 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/ORM/Marshaller.php b/src/ORM/Marshaller.php index 9a58b80cb97..8d57ce3f48a 100644 --- a/src/ORM/Marshaller.php +++ b/src/ORM/Marshaller.php @@ -71,13 +71,9 @@ protected function _buildPropertyMap($options) { $key = $nested; $nested = []; } - $nested = isset($nested['associated']) ? $nested['associated'] : []; $assoc = $this->_table->association($key); if ($assoc) { - $map[$assoc->property()] = [ - 'association' => $assoc, - 'nested' => $nested - ]; + $map[$assoc->property()] = ['association' => $assoc] + $nested + ['associated' => []]; } } return $map; @@ -110,8 +106,7 @@ public function one(array $data, array $options = []) { $columnType = $schema->columnType($key); if (isset($propertyMap[$key])) { $assoc = $propertyMap[$key]['association']; - $nested = ['associated' => $propertyMap[$key]['nested']]; - $value = $this->_marshalAssociation($assoc, $value, $nested); + $value = $this->_marshalAssociation($assoc, $value, $propertyMap[$key]); } elseif ($columnType) { $converter = Type::build($columnType); $value = $converter->marshal($value); @@ -267,8 +262,7 @@ public function merge(EntityInterface $entity, array $data, array $options = []) if (isset($propertyMap[$key])) { $assoc = $propertyMap[$key]['association']; - $nested = ['associated' => $propertyMap[$key]['nested']]; - $value = $this->_mergeAssociation($original, $assoc, $value, $nested); + $value = $this->_mergeAssociation($original, $assoc, $value, $propertyMap[$key]); } elseif ($columnType) { $converter = Type::build($columnType); $value = $converter->marshal($value); diff --git a/tests/TestCase/ORM/MarshallerTest.php b/tests/TestCase/ORM/MarshallerTest.php index 39c851b8b9a..c32e5a5d8ba 100644 --- a/tests/TestCase/ORM/MarshallerTest.php +++ b/tests/TestCase/ORM/MarshallerTest.php @@ -1081,4 +1081,37 @@ public function testMergeManyFieldList() { $this->assertEquals($expected, $entities[0]->toArray()); } +/** + * test marshalling association data while passing a fieldList + * + * @return void + */ + public function testAssociatoinsFieldList() { + $data = [ + 'title' => 'My title', + 'body' => 'My content', + 'author_id' => 1, + 'user' => [ + 'username' => 'mark', + 'password' => 'secret', + 'foo' => 'bar' + ] + ]; + $marshall = new Marshaller($this->articles); + $result = $marshall->one($data, [ + 'fieldList' => ['title', 'body', 'user'], + 'associated' => [ + 'Users' => ['fieldList' => ['username', 'foo']] + ] + ]); + + $this->assertEquals($data['title'], $result->title); + $this->assertEquals($data['body'], $result->body); + $this->assertNull($result->author_id); + + $this->assertInstanceOf('Cake\ORM\Entity', $result->user); + $this->assertEquals($data['user']['username'], $result->user->username); + $this->assertNull($result->user->password); + } + }