From dde620edb477b2d192721c79f61413856fc7bf9e Mon Sep 17 00:00:00 2001 From: gwoo Date: Thu, 28 Aug 2008 18:16:40 +0000 Subject: [PATCH] fixing bug in Set:map() handling of recursive records git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7531 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/set.php | 33 ++++++++++++------ cake/tests/cases/libs/set.test.php | 56 +++++++++++++++++++++++------- 2 files changed, 66 insertions(+), 23 deletions(-) diff --git a/cake/libs/set.php b/cake/libs/set.php index 2089dd298..2b9f1c41f 100644 --- a/cake/libs/set.php +++ b/cake/libs/set.php @@ -187,18 +187,31 @@ class Set extends Object { if (is_object($out)) { $out = get_object_vars($out); } - $out[$key] = Set::__map($value, $class, true); - } elseif ($primary === true && is_array($value)) { - $out->_name_ = $key; - $primary = false; - foreach($value as $key2 => $value2) { - $out->{$key2} = Set::__map($value2, true); + $out[$key] = Set::__map($value, $class); + if (is_object($out[$key])) { + if ($primary !== true && is_array($value) && Set::countDim($value, true) == 2) { + $out[$key]->_name_ = $primary; + } + } + } elseif (is_array($value)) { + if ($primary === true) { + $out->_name_ = $key; + $primary = false; + foreach($value as $key2 => $value2) { + $out->{$key2} = Set::__map($value2, true); + } + } else { + if (!is_numeric($key)) { + $out->{$key} = Set::__map($value, true, $key); + if (is_object($out->{$key}) && !is_numeric($key)) { + $out->{$key}->_name_ = $key; + } + } else { + $out->{$key} = Set::__map($value, true); + } } } else { - $out->{$key} = Set::__map($value, $class); - if (is_object($out->{$key})) { - $out->{$key}->_name_ = $key; - } + $out->{$key} = $value; } } } else { diff --git a/cake/tests/cases/libs/set.test.php b/cake/tests/cases/libs/set.test.php index fd4a7d222..1d1c7a38e 100644 --- a/cake/tests/cases/libs/set.test.php +++ b/cake/tests/cases/libs/set.test.php @@ -1694,11 +1694,10 @@ class SetTest extends CakeTestCase { $expected->Author->updated = "2007-03-17 01:18:31"; $expected->Author->test = "working"; $expected->Author->_name_ = 'Author'; - $this->assertIdentical($expected, $result); - + //Case where extra HABTM fields come back in a result - $result = Set::map(array( + $data = array( 'User' => array( 'id' => 1, 'email' => 'user@example.com', @@ -1711,39 +1710,70 @@ class SetTest extends CakeTestCase { 'title' => 'Moonlight Sonata', 'composer' => 'Ludwig van Beethoven', 'PiecesUser' => array( - 'id' => 2, + 'id' => 1, 'created' => '2008-01-01 00:00:00', 'modified' => '2008-01-01 00:00:00', 'piece_id' => 1, 'user_id' => 2, ) ), + array( + 'id' => 2, + 'title' => 'Moonlight Sonata 2', + 'composer' => 'Ludwig van Beethoven', + 'PiecesUser' => array( + 'id' => 2, + 'created' => '2008-01-01 00:00:00', + 'modified' => '2008-01-01 00:00:00', + 'piece_id' => 2, + 'user_id' => 2, + ) + ) ) - )); - + ); + + $result = Set::map($data); + $expected = new stdClass(); $expected->_name_ = 'User'; $expected->id = 1; $expected->email = 'user@example.com'; $expected->first_name = 'John'; $expected->last_name = 'Smith'; - + $piece = new stdClass(); - $piece->_name_ = 'Piece'; $piece->id = 1; $piece->title = 'Moonlight Sonata'; $piece->composer = 'Ludwig van Beethoven'; - + $piece->PiecesUser = new stdClass(); - $piece->PiecesUser->_name_ = 'PiecesUser'; - $piece->PiecesUser->id = 2; + $piece->PiecesUser->id = 1; $piece->PiecesUser->created = '2008-01-01 00:00:00'; $piece->PiecesUser->modified = '2008-01-01 00:00:00'; $piece->PiecesUser->piece_id = 1; $piece->PiecesUser->user_id = 2; + $piece->PiecesUser->_name_ = 'PiecesUser'; + + $piece->_name_ = 'Piece'; + + + $piece2 = new stdClass(); + $piece2->id = 2; + $piece2->title = 'Moonlight Sonata 2'; + $piece2->composer = 'Ludwig van Beethoven'; + + $piece2->PiecesUser = new stdClass(); + $piece2->PiecesUser->id = 2; + $piece2->PiecesUser->created = '2008-01-01 00:00:00'; + $piece2->PiecesUser->modified = '2008-01-01 00:00:00'; + $piece2->PiecesUser->piece_id = 2; + $piece2->PiecesUser->user_id = 2; + $piece2->PiecesUser->_name_ = 'PiecesUser'; + + $piece2->_name_ = 'Piece'; + + $expected->Piece = array($piece, $piece2); - $expected->Piece = array($piece); - $this->assertIdentical($expected, $result); } /**