mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
Adding option to group elements in Set::combine and making Model::generateList use Set::combine. Adding tests for Model::generateList and more tests for Set::combine
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5370 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
61df674db5
commit
cd09a7c7dd
4 changed files with 211 additions and 35 deletions
|
@ -1782,33 +1782,7 @@ class Model extends Overloadable {
|
|||
$valuePath = '{n}.' . $this->name . '.' . $this->displayField;
|
||||
}
|
||||
|
||||
$keys = Set::extract($result, $keyPath);
|
||||
$vals = Set::extract($result, $valuePath);
|
||||
|
||||
if (!empty($keys) && !empty($vals)) {
|
||||
$out = array();
|
||||
|
||||
if ($groupPath != null) {
|
||||
$group = Set::extract($result, $groupPath);
|
||||
if (!empty($group)) {
|
||||
$c = count($keys);
|
||||
for ($i = 0; $i < $c; $i++) {
|
||||
if (!isset($group[$i])) {
|
||||
$group[$i] = 0;
|
||||
}
|
||||
if (!isset($out[$group[$i]])) {
|
||||
$out[$group[$i]] = array();
|
||||
}
|
||||
$out[$group[$i]][$keys[$i]] = $vals[$i];
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
|
||||
$return = array_combine($keys, $vals);
|
||||
return $return;
|
||||
}
|
||||
return null;
|
||||
return Set::combine($result, $keyPath, $valuePath, $groupPath);
|
||||
}
|
||||
/**
|
||||
* Escapes the field name and prepends the model name. Escaping will be done according to the current database driver's rules.
|
||||
|
|
|
@ -606,16 +606,23 @@ class Set extends Object {
|
|||
/**
|
||||
* Creates an associative array using a $path1 as the path to build its keys, and optionally
|
||||
* $path2 as path to get the values. If $path2 is not specified, all values will be initialized
|
||||
* to null (useful for Set::merge).
|
||||
* to null (useful for Set::merge). You can optionally group the values by what is obtained when
|
||||
* following the path specified in $groupPath.
|
||||
*
|
||||
* @param array $data Array from where to extract keys and values
|
||||
* @param mixed $path1 As an array, or as a dot-separated string.
|
||||
* @param mixed $path2 As an array, or as a dot-separated string.
|
||||
* @param string $groupPath As an array, or as a dot-separated string.
|
||||
* @return array Combined array
|
||||
* @access public
|
||||
*/
|
||||
function combine($data, $path1 = null, $path2 = null) {
|
||||
if (is_a($this, 'set') && is_string($data) && empty($path2)) {
|
||||
function combine($data, $path1 = null, $path2 = null, $groupPath = null) {
|
||||
if (is_a($this, 'set') && is_string($data) && is_string($path1) && is_string($path2)) {
|
||||
$groupPath = $path2;
|
||||
$path2 = $path1;
|
||||
$path1 = $data;
|
||||
$data = $this->get();
|
||||
} else if (is_a($this, 'set') && is_string($data) && empty($path2)) {
|
||||
$path2 = $path1;
|
||||
$path1 = $data;
|
||||
$data = $this->get();
|
||||
|
@ -625,18 +632,35 @@ class Set extends Object {
|
|||
$data = get_object_vars($data);
|
||||
}
|
||||
|
||||
$data1 = Set::extract($data, $path1);
|
||||
$keys = Set::extract($data, $path1);
|
||||
|
||||
if (!empty($path2)) {
|
||||
$data2 = Set::extract($data, $path2);
|
||||
$vals = Set::extract($data, $path2);
|
||||
} else {
|
||||
$count = count($data1);
|
||||
$count = count($keys);
|
||||
for($i=0; $i < $count; $i++) {
|
||||
$data2[$i] = null;
|
||||
$vals[$i] = null;
|
||||
}
|
||||
}
|
||||
|
||||
return array_combine($data1, $data2);
|
||||
if ($groupPath != null) {
|
||||
$group = Set::extract($data, $groupPath);
|
||||
if (!empty($group)) {
|
||||
$c = count($keys);
|
||||
for ($i = 0; $i < $c; $i++) {
|
||||
if (!isset($group[$i])) {
|
||||
$group[$i] = 0;
|
||||
}
|
||||
if (!isset($out[$group[$i]])) {
|
||||
$out[$group[$i]] = array();
|
||||
}
|
||||
$out[$group[$i]][$keys[$i]] = $vals[$i];
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
|
||||
return array_combine($keys, $vals);
|
||||
}
|
||||
|
||||
function reverse($object) {
|
||||
|
|
|
@ -608,6 +608,115 @@ class ModelTest extends CakeTestCase {
|
|||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
function testGenerateList() {
|
||||
$this->model =& new Article();
|
||||
$this->model->displayField = 'title';
|
||||
|
||||
$result = $this->model->generateList(null, 'Article.title ASC');
|
||||
$expected = array(
|
||||
1 => 'First Article',
|
||||
2 => 'Second Article',
|
||||
3 => 'Third Article'
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->model->generateList(null, 'Article.title ASC', null, '{n}.Article.id');
|
||||
$expected = array(
|
||||
1 => 'First Article',
|
||||
2 => 'Second Article',
|
||||
3 => 'Third Article'
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->model->generateList(null, 'Article.title ASC', null, '{n}.Article.id', '{n}.Article');
|
||||
$expected = array(
|
||||
1 => array(
|
||||
'id' => 1,
|
||||
'user_id' => 1,
|
||||
'title' => 'First Article',
|
||||
'body' => 'First Article Body',
|
||||
'published' => 'Y',
|
||||
'created' => '2007-03-18 10:39:23',
|
||||
'updated' => '2007-03-18 10:41:31'
|
||||
),
|
||||
2 => array(
|
||||
'id' => 2,
|
||||
'user_id' => 3,
|
||||
'title' => 'Second Article',
|
||||
'body' => 'Second Article Body',
|
||||
'published' => 'Y',
|
||||
'created' => '2007-03-18 10:41:23',
|
||||
'updated' => '2007-03-18 10:43:31'
|
||||
),
|
||||
3 => array(
|
||||
'id' => 3,
|
||||
'user_id' => 1,
|
||||
'title' => 'Third Article',
|
||||
'body' => 'Third Article Body',
|
||||
'published' => 'Y',
|
||||
'created' => '2007-03-18 10:43:23',
|
||||
'updated' => '2007-03-18 10:45:31'
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->model->generateList(null, 'Article.title ASC', null, '{n}.Article.id', '{n}.Article.title');
|
||||
$expected = array(
|
||||
1 => 'First Article',
|
||||
2 => 'Second Article',
|
||||
3 => 'Third Article'
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->model->generateList(null, 'Article.title ASC', null, '{n}.Article.id', '{n}.Article', '{n}.Article.user_id');
|
||||
$expected = array(
|
||||
1 => array(
|
||||
1 => array(
|
||||
'id' => 1,
|
||||
'user_id' => 1,
|
||||
'title' => 'First Article',
|
||||
'body' => 'First Article Body',
|
||||
'published' => 'Y',
|
||||
'created' => '2007-03-18 10:39:23',
|
||||
'updated' => '2007-03-18 10:41:31'
|
||||
),
|
||||
3 => array(
|
||||
'id' => 3,
|
||||
'user_id' => 1,
|
||||
'title' => 'Third Article',
|
||||
'body' => 'Third Article Body',
|
||||
'published' => 'Y',
|
||||
'created' => '2007-03-18 10:43:23',
|
||||
'updated' => '2007-03-18 10:45:31'
|
||||
)
|
||||
),
|
||||
3 => array(
|
||||
2 => array(
|
||||
'id' => 2,
|
||||
'user_id' => 3,
|
||||
'title' => 'Second Article',
|
||||
'body' => 'Second Article Body',
|
||||
'published' => 'Y',
|
||||
'created' => '2007-03-18 10:41:23',
|
||||
'updated' => '2007-03-18 10:43:31'
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->model->generateList(null, 'Article.title ASC', null, '{n}.Article.id', '{n}.Article.title', '{n}.Article.user_id');
|
||||
$expected = array(
|
||||
1 => array(
|
||||
1 => 'First Article',
|
||||
3 => 'Third Article'
|
||||
),
|
||||
3 => array(
|
||||
2 => 'Second Article'
|
||||
)
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
|
||||
function testFindField() {
|
||||
$this->model =& new User();
|
||||
|
||||
|
|
|
@ -280,6 +280,7 @@ class SetTest extends UnitTestCase {
|
|||
$a = array(
|
||||
array('User' => array(
|
||||
'id' => 2,
|
||||
'group_id' => 1,
|
||||
'Data' => array(
|
||||
'user' => 'mariano.iglesias',
|
||||
'name' => 'Mariano Iglesias'
|
||||
|
@ -287,6 +288,7 @@ class SetTest extends UnitTestCase {
|
|||
)),
|
||||
array('User' => array(
|
||||
'id' => 14,
|
||||
'group_id' => 2,
|
||||
'Data' => array(
|
||||
'user' => 'phpnut',
|
||||
'name' => 'Larry E. Masters'
|
||||
|
@ -294,6 +296,7 @@ class SetTest extends UnitTestCase {
|
|||
)),
|
||||
array('User' => array(
|
||||
'id' => 25,
|
||||
'group_id' => 1,
|
||||
'Data' => array(
|
||||
'user' => 'gwoo',
|
||||
'name' => 'The Gwoo'
|
||||
|
@ -334,6 +337,39 @@ class SetTest extends UnitTestCase {
|
|||
);
|
||||
$this->assertIdentical($result, $expected);
|
||||
|
||||
$result = Set::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
|
||||
$expected = array(
|
||||
1 => array(
|
||||
2 => array(
|
||||
'user' => 'mariano.iglesias',
|
||||
'name' => 'Mariano Iglesias'
|
||||
),
|
||||
25 => array(
|
||||
'user' => 'gwoo',
|
||||
'name' => 'The Gwoo'
|
||||
)
|
||||
),
|
||||
2 => array(
|
||||
14 => array(
|
||||
'user' => 'phpnut',
|
||||
'name' => 'Larry E. Masters'
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertIdentical($result, $expected);
|
||||
|
||||
$result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
|
||||
$expected = array(
|
||||
1 => array(
|
||||
2 => 'Mariano Iglesias',
|
||||
25 => 'The Gwoo'
|
||||
),
|
||||
2 => array(
|
||||
14 => 'Larry E. Masters'
|
||||
)
|
||||
);
|
||||
$this->assertIdentical($result, $expected);
|
||||
|
||||
$Set =& new Set($a);
|
||||
|
||||
$result = $Set->combine('{n}.User.id');
|
||||
|
@ -368,6 +404,39 @@ class SetTest extends UnitTestCase {
|
|||
25 => 'The Gwoo'
|
||||
);
|
||||
$this->assertIdentical($result, $expected);
|
||||
|
||||
$result = $Set->combine('{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
|
||||
$expected = array(
|
||||
1 => array(
|
||||
2 => array(
|
||||
'user' => 'mariano.iglesias',
|
||||
'name' => 'Mariano Iglesias'
|
||||
),
|
||||
25 => array(
|
||||
'user' => 'gwoo',
|
||||
'name' => 'The Gwoo'
|
||||
)
|
||||
),
|
||||
2 => array(
|
||||
14 => array(
|
||||
'user' => 'phpnut',
|
||||
'name' => 'Larry E. Masters'
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertIdentical($result, $expected);
|
||||
|
||||
$result = $Set->combine('{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
|
||||
$expected = array(
|
||||
1 => array(
|
||||
2 => 'Mariano Iglesias',
|
||||
25 => 'The Gwoo'
|
||||
),
|
||||
2 => array(
|
||||
14 => 'Larry E. Masters'
|
||||
)
|
||||
);
|
||||
$this->assertIdentical($result, $expected);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue