Skip to content

Commit

Permalink
Recursively transform entities into arrays.
Browse files Browse the repository at this point in the history
toArray() should recursively transform entities, otherwise you can end
up with a mixture of array's and entity objects.
  • Loading branch information
markstory committed Nov 19, 2013
1 parent 9618de1 commit de1e22f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
15 changes: 14 additions & 1 deletion Cake/ORM/Entity.php
Expand Up @@ -273,12 +273,25 @@ public function unsetProperty($property) {
* Returns an array with all the properties that have been set
* to this entity
*
* This method will recursively transform entities assigned to properties
* into arrays as well.
*
* @return array
*/
public function toArray() {
$result = [];
foreach ($this->_properties as $property => $value) {
$result[$property] = $this->get($property);
$value = $this->get($property);
if (is_array($value) && isset($value[0]) && $value[0] instanceof self) {
$result[$property] = [];
foreach ($value as $k => $entity) {
$result[$property][$k] = $entity->toArray();
}
} elseif ($value instanceof self) {
$result[$property] = $value->toArray();
} else {
$result[$property] = $value;
}
}
return $result;
}
Expand Down
51 changes: 45 additions & 6 deletions Cake/Test/TestCase/ORM/EntityTest.php
Expand Up @@ -430,7 +430,7 @@ public function testJsonSerialize() {
* @return void
*/
public function testExtract() {
$entity = new \Cake\ORM\Entity([
$entity = new Entity([
'id' => 1,
'title' => 'Foo',
'author_id' => 3
Expand All @@ -454,7 +454,7 @@ public function testExtract() {
* @return void
*/
public function testDirty() {
$entity = new \Cake\ORM\Entity([
$entity = new Entity([
'id' => 1,
'title' => 'Foo',
'author_id' => 3
Expand All @@ -476,7 +476,7 @@ public function testDirty() {
* @return void
*/
public function testDirtyChangingProperties() {
$entity = new \Cake\ORM\Entity([
$entity = new Entity([
'title' => 'Foo',
]);
$entity->dirty('title', false);
Expand All @@ -498,7 +498,7 @@ public function testDirtyChangingProperties() {
* @return void
*/
public function testExtractDirty() {
$entity = new \Cake\ORM\Entity([
$entity = new Entity([
'id' => 1,
'title' => 'Foo',
'author_id' => 3
Expand All @@ -516,7 +516,7 @@ public function testExtractDirty() {
* @return void
*/
public function testClean() {
$entity = new \Cake\ORM\Entity([
$entity = new Entity([
'id' => 1,
'title' => 'Foo',
'author_id' => 3
Expand All @@ -537,7 +537,7 @@ public function testClean() {
* @return void
*/
public function testIsNew() {
$entity = new \Cake\ORM\Entity([
$entity = new Entity([
'id' => 1,
'title' => 'Foo',
'author_id' => 3
Expand Down Expand Up @@ -591,4 +591,43 @@ public function testConstructorWithMarkNew() {
$entity->__construct(['a' => 'b', 'c' => 'd'], ['markNew' => true]);
}

/**
* Test toArray method.
*
* @return void
*/
public function testToArray() {
$data = ['name' => 'James', 'age' => 20, 'phones' => ['123', '457']];
$entity = new Entity($data);

$this->assertEquals($data, $entity->toArray());
}

/**
* Test toArray recursive.
*/
public function testToArrayRecursive() {
$data = ['id' => 1, 'name' => 'James', 'age' => 20, 'phones' => ['123', '457']];
$user = new Entity($data);
$comments = [
new Entity(['user_id' => 1, 'body' => 'Comment 1']),
new Entity(['user_id' => 1, 'body' => 'Comment 2']),
];
$user->comments = $comments;
$user->profile = new Entity(['email' => 'mark@example.com']);

$expected = [
'id' => 1,
'name' => 'James',
'age' => 20,
'phones' => ['123', '457'],
'profile' => ['email' => 'mark@example.com'],
'comments' => [
['user_id' => 1, 'body' => 'Comment 1'],
['user_id' => 1, 'body' => 'Comment 2'],
]
];
$this->assertEquals($expected, $user->toArray());
}

}

0 comments on commit de1e22f

Please sign in to comment.