Skip to content
Permalink
Browse files

Basic implementation of Marshaller::many()

Add tests for many to one marshalling.
  • Loading branch information...
markstory committed Dec 13, 2013
1 parent f3b4fde commit a7a4e5a780134645c003957bfe57fa8b6e16f408
Showing with 56 additions and 16 deletions.
  1. +23 −12 Cake/ORM/Marshaller.php
  2. +33 −4 Cake/Test/TestCase/ORM/MarshallerTest.php
@@ -14,6 +14,7 @@
*/
namespace Cake\ORM;
use Cake\ORM\Association;
use Cake\ORM\Table;
use Cake\Utility\Hash;
@@ -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);
@@ -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;
}
}
@@ -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);
@@ -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() {

0 comments on commit a7a4e5a

Please sign in to comment.
You can’t perform that action at this time.