Skip to content

Commit

Permalink
Fixed Marshaller::merge() when passing data that needs to be converted
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Apr 18, 2014
1 parent faf7b18 commit a2159a8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/ORM/Marshaller.php
Expand Up @@ -244,16 +244,24 @@ public function merge(EntityInterface $entity, array $data, array $include = [])
$data = $data[$tableName];
}

$schema = $this->_table->schema();
$properties = [];
foreach ($data as $key => $value) {
$columnType = $schema->columnType($key);
$original = $entity->get($key);

if (isset($propertyMap[$key])) {
$assoc = $propertyMap[$key]['association'];
$nested = $propertyMap[$key]['nested'];
$value = $this->_mergeAssociation($original, $assoc, $value, $nested);
} elseif ($original == $value) {
continue;
} elseif ($columnType) {
$converter = Type::build($columnType);
$value = $converter->marshal($value);
if ($original == $value) {
continue;
}
}

$properties[$key] = $value;
}

Expand Down
23 changes: 23 additions & 0 deletions tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -907,4 +907,27 @@ public function testMergeManyWithAppend() {
$this->assertEquals('Changed 2', $result[0]->comment);
}

/**
* Tests merge with data types that need to be marshalled
*
* @return void
*/
public function testMergeComplexType() {
$entity = new Entity(
['comment' => 'My Comment text'],
['markNew' => false, 'markClean' => true]
);
$data = [
'created' => [
'year' => '2014',
'month' => '2',
'day' => 14
]
];
$marshall = new Marshaller($this->comments);
$result = $marshall->merge($entity, $data);
$this->assertInstanceOf('DateTime', $entity->created);
$this->assertEquals('2014-02-14', $entity->created->format('Y-m-d'));
}

}

0 comments on commit a2159a8

Please sign in to comment.