Adding optgroup support for 2D arrays in FormHelper::select(); adding grouping ability to Model::generateList()

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3326 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2006-07-30 23:31:04 +00:00
parent ab419f3fe1
commit a75f33f430
3 changed files with 29 additions and 4 deletions

View file

@ -70,6 +70,10 @@ selectoption = "<option value="%s" %s>%s</option>"
; Tag template for a closing select tag.
selectend = "</select>"
; Tag template for a option group tag.
optiongroup = "<optgroup label="%s"%s>"
optiongroupend = "</optgroup>"
; Tag template for a password tag.
password = "<input type="password" name="data[%s][%s]" %s/>"

View file

@ -1506,12 +1506,13 @@ class Model extends Overloadable {
* @param int $limit SQL LIMIT clause, for calculating items per page
* @param string $keyPath A string path to the key, i.e. "{n}.Post.id"
* @param string $valuePath A string path to the value, i.e. "{n}.Post.title"
* @param string $groupPath A string path to a value to group the elements by, i.e. "{n}.Post.category_id"
* @return array An associative array of records, where the id is the key, and the display field is the value
*/
function generateList($conditions = null, $order = null, $limit = null, $keyPath = null, $valuePath = null) {
function generateList($conditions = null, $order = null, $limit = null, $keyPath = null, $valuePath = null, $groupPath = null) {
$db =& ConnectionManager::getDataSource($this->useDbConfig);
if ($keyPath == null && $valuePath == null && $this->hasField($this->displayField)) {
if ($keyPath == null && $valuePath == null && $groupPath == null && $this->hasField($this->displayField)) {
$fields = array($this->primaryKey, $this->displayField);
} else {
$fields = null;
@ -1531,9 +1532,26 @@ class Model extends Overloadable {
$vals = $db->getFieldValue($result, $valuePath);
if (!empty($keys) && !empty($vals)) {
$out = array();
if ($groupPath != null) {
$group = $db->getFieldValue($result, $groupPath);
if (!empty($group)) {
$c = count($keys);
for ($i = 0; $i < $c; $i++) {
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;
}
/**
* Escapes the field name and prepends the model name. Escaping will be done according to the current database driver's rules.

View file

@ -240,10 +240,13 @@ class FormHelper extends Helper {
foreach($elements as $name => $title) {
$htmlOptions = array();
if (is_array($title) && (!isset($title['name']) || !isset($title['value']))) {
$select[] = sprintf($this->tags['optiongroup'], $name, '');
$select = am($select, $this->selectOptions($title, $selected));
$select[] = $this->tags['optiongroupend'];
$name = null;
} elseif (is_array($title)) {
$select = am($select, $this->selectOptions());
$htmlOptions = $title;
$name = $title['value'];
$title = $title['name'];