Skip to content

Commit

Permalink
Implemented fieldList for marshalling associations
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 12, 2014
1 parent 627fd4d commit 8adcacf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/ORM/Marshaller.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
33 changes: 33 additions & 0 deletions tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -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);
}

}

0 comments on commit 8adcacf

Please sign in to comment.