Added a new $altKey parameter to normalizeFindParams

Refactored the unit test for it and added more samples

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6018 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
the_undefined 2007-11-18 06:14:27 +00:00
parent 266d7ba738
commit b3afc463ac
2 changed files with 73 additions and 22 deletions

View file

@ -2062,7 +2062,7 @@ class Model extends Overloadable {
return false;
}
function normalizeFindParams($type, $data, $r = array(), $_this = null) {
function normalizeFindParams($type, $data, $altType = null, $r = array(), $_this = null) {
if ($_this == null) {
$_this = $this;
$root = true;
@ -2082,15 +2082,23 @@ class Model extends Overloadable {
if (!empty($children)) {
if ($_this->name == $name) {
$r = am($r, $this->normalizeFindParams($type, $children, $r, $_this));
$r = am($r, $this->normalizeFindParams($type, $children, $altType, $r, $_this));
} else {
$r[$name] = $this->normalizeFindParams($type, $children, @$r[$name], $_this->{$name});;
if (!$_this->getAssociated($name)) {
$r[$altType][$name] = $children;
} else {
$r[$name] = $this->normalizeFindParams($type, $children, $altType, @$r[$name], $_this->{$name});;
}
}
} else {
if ($_this->getAssociated($name)) {
$r[$name] = array($type => null);
} else {
$r[$type][] = $name;
if ($altType != null) {
$r[$type][] = $name;
} else {
$r[$type] = $name;
}
}
}
}

View file

@ -461,27 +461,70 @@ class ModelTest extends CakeTestCase {
}
function testNormalizeFindParams() {
$this->model =& new Article();
$result = $this->model->normalizeFindParams('fields', array(
'title', 'body', 'published',
'Article.id', 'User', 'Comment.id', 'Comment.comment', 'Comment.User.password', 'Comment.Article',
'Tag' => array('id', 'tag')
)
);
$expected = array(
'Article' => array(
'fields' => array('title', 'body', 'published', 'id'),
'User' => array('fields' => null),
'Comment' => array(
'fields' => array('id', 'comment'),
'User' => array('fields' => array('password')),
'Article' => array('fields' => null)
$samples = array(
0 => array(
'model' => new Article(),
'params' => array(
'fields',
array(
'title', 'body', 'published',
'Article.id', 'User', 'Comment.id', 'Comment.comment', 'Comment.User.password', 'Comment.Article',
'Tag' => array('id', 'tag'),
'User.name' => 'Jimmy',
),
'conditions'
),
'Tag' => array('fields' => array('id', 'tag'))
'expected' => array(
'Article' => array(
'fields' => array('title', 'body', 'published', 'id'),
'User' => array('fields' => null, 'conditions' => array('name' => 'Jimmy')),
'Comment' => array(
'fields' => array('id', 'comment'),
'User' => array('fields' => array('password')),
'Article' => array('fields' => null)
),
'Tag' => array('fields' => array('id', 'tag'))
)
)
),
1 => array(
'params' => array(
'fields',
array(
'User.id' => 2
),
'conditions'
),
'expected' => array(
'Article' => array(
'User' => array(
'conditions' => array('id' => 2)
)
)
)
),
2 => array(
'params' => array(
'limit',
array('Comment' => 5),
),
'expected' => array(
'Article' => array(
'Comment' => array(
'limit' => 5
)
)
)
)
);
$this->assertEqual($result, $expected);
foreach ($samples as $i => $sample) {
if (isset($sample['model'])) {
$Model =& $sample['model'];
}
$results = call_user_func_array(array(&$Model, 'normalizeFindParams'), $sample['params']);
$this->assertEqual($results, $sample['expected'], '%s - Sample #'.$i);
}
}