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
This commit is contained in:
gwoo 2008-08-28 18:16:40 +00:00
parent 84b6d1b66c
commit dde620edb4
2 changed files with 66 additions and 23 deletions

View file

@ -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 {

View file

@ -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);
}
/**