Skip to content
Permalink
Browse files

Implemented fieldList for marshalling associations

  • Loading branch information...
lorenzo committed Jul 12, 2014
1 parent 627fd4d commit 8adcacfd7b47938acaef304fab8ba3927d17fb8c
Showing with 36 additions and 9 deletions.
  1. +3 −9 src/ORM/Marshaller.php
  2. +33 −0 tests/TestCase/ORM/MarshallerTest.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);
@@ -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.
You can’t perform that action at this time.