Enhancing Set::map() so that it doesn't override manual setting of _name_. Test case enhanced to make sure it does so.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7669 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
john 2008-09-26 21:11:51 +00:00
parent f8f4bcc16c
commit 6ad3cfa0ef
2 changed files with 89 additions and 3 deletions

View file

@ -194,12 +194,16 @@ class Set extends Object {
$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;
if(!isset($out[$key]->_name_)) {
$out[$key]->_name_ = $primary;
}
}
}
} elseif (is_array($value)) {
if ($primary === true) {
$out->_name_ = $key;
if(!isset($out->_name_)) {
$out->_name_ = $key;
}
$primary = false;
foreach($value as $key2 => $value2) {
$out->{$key2} = Set::__map($value2, true);
@ -208,7 +212,9 @@ class Set extends Object {
if (!is_numeric($key)) {
$out->{$key} = Set::__map($value, true, $key);
if (is_object($out->{$key}) && !is_numeric($key)) {
$out->{$key}->_name_ = $key;
if(!isset($out->{$key}->_name_)) {
$out->{$key}->_name_ = $key;
}
}
} else {
$out->{$key} = Set::__map($value, true);

View file

@ -1883,6 +1883,86 @@ class SetTest extends CakeTestCase {
$expected->Piece = array($piece, $piece2);
$this->assertIdentical($expected, $result);
//Same data, but should work if _name_ has been manually defined:
$data = array(
'User' => array(
'id' => 1,
'email' => 'user@example.com',
'first_name' => 'John',
'last_name' => 'Smith',
'_name_' => 'FooUser',
),
'Piece' => array(
array(
'id' => 1,
'title' => 'Moonlight Sonata',
'composer' => 'Ludwig van Beethoven',
'_name_' => 'FooPiece',
'PiecesUser' => array(
'id' => 1,
'created' => '2008-01-01 00:00:00',
'modified' => '2008-01-01 00:00:00',
'piece_id' => 1,
'user_id' => 2,
'_name_' => 'FooPiecesUser',
)
),
array(
'id' => 2,
'title' => 'Moonlight Sonata 2',
'composer' => 'Ludwig van Beethoven',
'_name_' => 'FooPiece',
'PiecesUser' => array(
'id' => 2,
'created' => '2008-01-01 00:00:00',
'modified' => '2008-01-01 00:00:00',
'piece_id' => 2,
'user_id' => 2,
'_name_' => 'FooPiecesUser',
)
)
)
);
$result = Set::map($data);
$expected = new stdClass();
$expected->_name_ = 'FooUser';
$expected->id = 1;
$expected->email = 'user@example.com';
$expected->first_name = 'John';
$expected->last_name = 'Smith';
$piece = new stdClass();
$piece->id = 1;
$piece->title = 'Moonlight Sonata';
$piece->composer = 'Ludwig van Beethoven';
$piece->_name_ = 'FooPiece';
$piece->PiecesUser = new stdClass();
$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_ = 'FooPiecesUser';
$piece2 = new stdClass();
$piece2->id = 2;
$piece2->title = 'Moonlight Sonata 2';
$piece2->composer = 'Ludwig van Beethoven';
$piece2->_name_ = 'FooPiece';
$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_ = 'FooPiecesUser';
$expected->Piece = array($piece, $piece2);
$this->assertIdentical($expected, $result);
}
/**
* testPushDiff method