Skip to content
Permalink
Browse files

Recursively transform entities into arrays.

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 de1e22fd87fc299e9e9b9e15086b42dd2c564eb1
Showing with 59 additions and 7 deletions.
  1. +14 −1 Cake/ORM/Entity.php
  2. +45 −6 Cake/Test/TestCase/ORM/EntityTest.php
@@ -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;
}
@@ -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
@@ -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
@@ -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);
@@ -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
@@ -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
@@ -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
@@ -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.
You can’t perform that action at this time.