Add formatted lists to your AppModel
This code will add formatted list functionallity to find you can easy replace the $this->Model->find('list'); with $this->Model->find('formattedlist', array('fields' => array('Model.id', 'Model.field1', 'Model.field2', 'Model.field3'), 'format' => '%s-%s %s')); and get option tag output of: . Even better part is being able to setup your own format for the output!
First you have to copy this to your app_model.php
Download code
Then you should now be able to use it like so:
Download code
That is all..
Download code
<?php
class AppModel extends Model {
function find($type, $options = array()) {
switch ($type) {
case 'formattedlist':
if(!isset($options['fields']) || count($options['fields']) < 3) {
return parent::find('list', $options);
}
$this->recursive = -1;
//setup formating
$format = '';
if(!isset($options['format'])) {
for($i = 0; $i < (count($options['fields']) - 1); $i++)
$format .= '%s ';
$format = substr($format,0,-1);
} else
$format = $options['format'];
//get data
$list = parent::find('all', $options);
// remove model alias from strings to only get field names
$tmpPath2[] = $format;
for($i = 1; $i <= (count($options['fields']) - 1); $i++) {
$field[$i] = str_replace($this->alias.'.', '', $options['fields'][$i]);
$tmpPath2[] = '{n}.'.$this->alias.'.'.$field[$i];
}
//do the magic?? read the code...
return Set::combine($list, '{n}.'.$this->alias.'.'.$this->primaryKey,
$tmpPath2);
break;
default:
return parent::find($type, $options);
break;
}
}
}
?>
Then you should now be able to use it like so:
Download code
$this->Model->find('formattedlist',
array(
'fields'=>array(
'Model.id', // allows start with the value="" tags field
'Model.field1', // then put them in order of how you want the format to output.
'Model.field2',
'Model.field3',
'Model.field4',
'Model.field5',
),
'format'=>'%s-%s%s %s%s'
)
);
That is all..
Comments
Comment
1 Brakes grouped find('list')
Question
2 breaking find('list')?
Comment
3 heh...
Maybe I was asleep...
sorry... didn't notice the method name
Forgive my unkind comment, and I promise I'll show a better public behavior in the future. ;-D