From cd09a7c7dde95296243c53bdbfe131e61c005750 Mon Sep 17 00:00:00 2001 From: "mariano.iglesias" Date: Sun, 1 Jul 2007 03:43:05 +0000 Subject: [PATCH] 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 --- cake/libs/model/model.php | 28 +----- cake/libs/set.php | 40 ++++++-- cake/tests/cases/libs/model/model.test.php | 109 +++++++++++++++++++++ cake/tests/cases/libs/set.test.php | 69 +++++++++++++ 4 files changed, 211 insertions(+), 35 deletions(-) diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index fa0dda6a1..d6ecb4f95 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -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. diff --git a/cake/libs/set.php b/cake/libs/set.php index a866200bc..d46adb6a2 100644 --- a/cake/libs/set.php +++ b/cake/libs/set.php @@ -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) { diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index dfbd769d9..8adf692c1 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -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(); diff --git a/cake/tests/cases/libs/set.test.php b/cake/tests/cases/libs/set.test.php index 375513639..534a3914c 100644 --- a/cake/tests/cases/libs/set.test.php +++ b/cake/tests/cases/libs/set.test.php @@ -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); } }