Skip to content

Commit

Permalink
Basic implementation of Marshaller::many()
Browse files Browse the repository at this point in the history
Add tests for many to one marshalling.
  • Loading branch information
markstory committed Dec 13, 2013
1 parent f3b4fde commit a7a4e5a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
35 changes: 23 additions & 12 deletions Cake/ORM/Marshaller.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\ORM;

use Cake\ORM\Association;
use Cake\ORM\Table;
use Cake\Utility\Hash;

Expand Down Expand Up @@ -55,17 +56,19 @@ public function __construct(Table $table, $safe = false) {
* Hydrate one entity and its associated data.
*
* @param array $data The data to hydrate.
* @param array $associations The associations to include.
* @param array $include The associations to include.
* @return Cake\ORM\Entity
*/
public function one(array $data, $associations = []) {
$associations = Hash::normalize($associations);
$class = $this->_table->entityClass();
$entity = new $class();
public function one(array $data, $include = []) {
$include = Hash::normalize($include);

$entityClass = $this->_table->entityClass();

$entity = new $entityClass();
foreach ($data as $key => $value) {
if (array_key_exists($key, $associations)) {
if (array_key_exists($key, $include)) {
$assoc = $this->_table->association($key);
$value = $this->_marshalAssociation($assoc, $value, $associations[$key]);
$value = $this->_marshalAssociation($assoc, $value, $include[$key]);
$entity->set($assoc->property(), $value);
} else {
$entity->set($key, $value);
Expand All @@ -79,21 +82,29 @@ public function one(array $data, $associations = []) {
*
* @return mixed
*/
protected function _marshalAssociation($assoc, $value, $associations) {
protected function _marshalAssociation($assoc, $value, $include) {
$targetTable = $assoc->target();
$marshaller = $targetTable->marshaller();
// TODO switch to one based on association type.
return $marshaller->one($value, (array)$associations);
if ($assoc->type() === Association::ONE_TO_ONE) {
return $marshaller->one($value, (array)$include);
} else {
return $marshaller->many($value, (array)$include);
}
}

/**
* Hydrate many entities and their associated data.
*
* @param array $data The data to hydrate.
* @param array $associations The associations to include.
* @param array $include The associations to include.
* @return array An array of hydrated records.
*/
public function many(array $data, $associations = []) {
public function many(array $data, $include = []) {
$output = [];
foreach ($data as $record) {
$output[] = $this->one($record, $include);
}
return $output;
}

}
37 changes: 33 additions & 4 deletions Cake/Test/TestCase/ORM/MarshallerTest.php
Expand Up @@ -103,11 +103,11 @@ public function testOneAssociationsSingle() {
$this->assertEquals($data['body'], $result->body);
$this->assertEquals($data['author_id'], $result->author_id);

$this->assertNull($result->comments);
$this->assertInternalType('array', $result->Comments);
$this->assertCount(2, $result->Comments);
$this->assertInternalType('array', $result->Comments[0]);
$this->assertInternalType('array', $result->Comments[1]);
$this->assertEquals($data['Comments'], $result->Comments);

$this->assertNull($result->Users);
$this->assertInstanceOf('Cake\ORM\Entity', $result->user);
$this->assertEquals($data['Users']['username'], $result->user->username);
$this->assertEquals($data['Users']['password'], $result->user->password);
Expand All @@ -119,7 +119,36 @@ public function testOneAssociationsSingle() {
* @return void
*/
public function testOneAssociationsMany() {
$this->markTestIncomplete('not done');
$data = [
'title' => 'My title',
'body' => 'My content',
'author_id' => 1,
'Comments' => [
['comment' => 'First post', 'user_id' => 2],
['comment' => 'Second post', 'user_id' => 2],
],
'Users' => [
'username' => 'mark',
'password' => 'secret'
]
];
$marshall = new Marshaller($this->articles);
$result = $marshall->one($data, ['Comments']);

$this->assertEquals($data['title'], $result->title);
$this->assertEquals($data['body'], $result->body);
$this->assertEquals($data['author_id'], $result->author_id);

$this->assertNull($result->Comments);
$this->assertInternalType('array', $result->comments);
$this->assertCount(2, $result->comments);
$this->assertInstanceOf('Cake\ORM\Entity', $result->comments[0]);
$this->assertInstanceOf('Cake\ORM\Entity', $result->comments[1]);
$this->assertEquals($data['Comments'][0]['comment'], $result->comments[0]->comment);

$this->assertNull($result->user);
$this->assertInternalType('array', $result->Users);
$this->assertEquals($data['Users'], $result->Users);
}

public function testOneDeepAssociations() {
Expand Down

0 comments on commit a7a4e5a

Please sign in to comment.