CakePHP Combine List Fields
Please Support:
SuperList
Info:
- Version: 1.0
- Requirements: cakePHP 1.2.x
- Article: SuperList Discussion and Examples
Watch:
Get it:
A common problem with find('list') is not being able to combine multiple fields with a specific format for your drop down boxes. Not to fret, superlist comes to the rescue.
Based on teknoid's post: http://teknoid.wordpress.com/2008/09/04/findlist-with-three-or-combined-fields/
I've extended his superlist to allow for custom passed in formats and isn't limited to only combining two fields.
/app/app_model.php
Example Usage:
Based on teknoid's post: http://teknoid.wordpress.com/2008/09/04/findlist-with-three-or-combined-fields/
I've extended his superlist to allow for custom passed in formats and isn't limited to only combining two fields.
/app/app_model.php
- switch ($type) {
- case 'superlist':
- return parent::find('list', $options);
- }
- $options['separator'] = ' ';
- }
- $options['format'] = '%s';
- for($i = 2; $i<$total_fields;$i++){
- $options['format'] .= "{$options['separator']}%s";
- }
- }
- $options['recursive'] = -1;
- $list = parent::find('all', $options);
- $formatVals[0] = $options['format'];
- for($i = 1; $i < $total_fields; $i++){
- }
- return Set::combine(
- $list,
- "{n}.{$this->alias}.{$this->primaryKey}",
- $formatVals
- );
- break;
- default:
- return parent::find($type, $options);
- break;
- }
- }
Example Usage:
- 'separator' => ', '
- ));
- //Output:
- //[User.id] => User.last_name, User.first_name
- 'format' => '%s, %s'
- ));
- //Output: same as above
- //[User.id] => User.last_name, User.first_name