Skip to content
Permalink
Browse files

Making find('threaded') work with hydrated results

  • Loading branch information...
lorenzo committed Sep 15, 2013
1 parent 8eff391 commit 74ed36c2380ae20cd94b3149efe944d831f1530d
Showing with 30 additions and 5 deletions.
  1. +4 −4 lib/Cake/ORM/Entity.php
  2. +26 −1 lib/Cake/Test/TestCase/ORM/TableTest.php
@@ -6,7 +6,7 @@ class Entity implements \ArrayAccess {
protected $_properties = [];
public function __get($property) {
public function &__get($property) {
return $this->get($property);
}
@@ -29,12 +29,12 @@ public function set($properties = [], $useSetters = true) {
return $this;
}
public function get($property) {
public function &get($property) {

This comment has been minimized.

Copy link
@jrbasso

jrbasso Sep 17, 2013

Member

It seems dangerous. If the property is an array and user mess with that array will mess with the entity as well. Ie,

$data = $entity->get('some_array');
$data['extra_data'] = 'starting the mess';
// At this point the entity properties has been change too

Other point is the check if method exists, it makes the user returns the getXxx by reference too.

This comment has been minimized.

Copy link
@lorenzo

lorenzo Sep 17, 2013

Author Member

No, you would need to assign the variable by reference to get that behavior. If you do, then it means it was completely intentional.

This comment has been minimized.

Copy link
@jrbasso

jrbasso Sep 18, 2013

Member

It's true. Sorry.

$method = 'get' . ucFirst($property);
if (method_exists($this, $method)) {
$value = $this->{$method}();
} else {
$value = $this->_properties[$property];
$value =& $this->_properties[$property];
}
return $value;
}
@@ -69,7 +69,7 @@ public function offsetExists($offset) {
* @return void
*/
public function offsetGet($offset) {
public function &offsetGet($offset) {
return $this->get($offset);
}
@@ -599,8 +599,10 @@ public function testFindThreaded() {
]
]
];
$results = $table->find('threaded')
$results = $table->find('all')
->select(['id', 'parent_id', 'name'])
->hydrate(false)
->threaded()
->toArray();
$this->assertEquals($expected, $results);
}
@@ -653,4 +655,27 @@ public function testStackingFinders() {
$this->assertSame($query, $result);
}
/**
* Tests find('threaded') with hydrated results
*
* @return void
*/
public function testFindThreadedHydrated() {
$table = new Table(['table' => 'categories', 'connection' => $this->connection]);
$results = $table->find('all')
->hydrate(true)
->threaded()
->select(['id', 'parent_id', 'name'])
->toArray();
$this->assertEquals(1, $results[0]->id);
$expected = [
'id' => 8,
'parent_id' => 2,
'name' => 'Category 1.1.2',
'children' => []
];
$this->assertEquals($expected, $results[0]->children[0]->children[1]->toArray());
}
}

0 comments on commit 74ed36c

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