fixing find('list') calls with a fields array that doesn't contain the model alias, closes #4851

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7158 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
DarkAngelBGE 2008-06-10 17:27:12 +00:00
parent e221e9fb61
commit 70aa290f5b
2 changed files with 33 additions and 0 deletions

View file

@ -1862,12 +1862,29 @@ class Model extends Overloadable {
if (!is_array($query['fields'])) {
$query['fields'] = String::tokenize($query['fields']);
}
if (count($query['fields']) == 1) {
if (strpos($query['fields'][0], '.') === false) {
$query['fields'][0] = $this->alias . '.' . $query['fields'][0];
}
$list = array("{n}.{$this->alias}.{$this->primaryKey}", '{n}.' . $query['fields'][0], null);
$query['fields'] = array("{$this->alias}.{$this->primaryKey}", $query['fields'][0]);
} elseif (count($query['fields']) == 3) {
for ($i = 0; $i < 3; $i++) {
if (strpos($query['fields'][$i], '.') === false) {
$query['fields'][$i] = $this->alias . '.' . $query['fields'][$i];
}
}
$list = array('{n}.' . $query['fields'][0], '{n}.' . $query['fields'][1], '{n}.' . $query['fields'][2]);
} else {
for ($i = 0; $i < 2; $i++) {
if (strpos($query['fields'][$i], '.') === false) {
$query['fields'][$i] = $this->alias . '.' . $query['fields'][$i];
}
}
$list = array('{n}.' . $query['fields'][0], '{n}.' . $query['fields'][1], null);
}
}

View file

@ -987,6 +987,22 @@ class ModelTest extends CakeTestCase {
$expected = array(1 => 'First Post', 2 => 'Second Post', 3 => 'Third Post');
$this->assertEqual($result, $expected);
$result = $TestModel->find('list', array('fields' => 'title'));
$expected = array(1 => 'First Post', 2 => 'Second Post', 3 => 'Third Post');
$this->assertEqual($result, $expected);
$result = $TestModel->find('list', array('fields' => array('title', 'id')));
$expected = array('First Post' => '1', 'Second Post' => '2', 'Third Post' => '3');
$this->assertEqual($result, $expected);
$result = $TestModel->find('list', array('fields' => array('title', 'id', 'created')));
$expected = array(
'2007-03-18 10:39:23' => array('First Post' => '1'),
'2007-03-18 10:41:23' => array('Second Post' => '2'),
'2007-03-18 10:43:23' => array('Third Post' => '3'),
);
$this->assertEqual($result, $expected);
$result = $TestModel->find('list', array('fields' => array('Post.body')));
$expected = array(1 => 'First Post Body', 2 => 'Second Post Body', 3 => 'Third Post Body');
$this->assertEqual($result, $expected);