04.12
php
saved
dardosordi
Note
borrowed from http://cakeexplorer.wordpress.com/2007/09/10/autofield-behavior-or-how-to-add-additional-columns-to-your-models/
just formated it
borrowed from http://cakeexplorer.wordpress.com/2007/09/10/autofield-behavior-or-how-to-add-additional-columns-to-your-models/
just formated it
- <?php
- /**
- * Autofield behavior for cakePHP
- * comments, bug reports are welcome skie AT mail DOT ru
- * @author Yevgeny Tomenko aka SkieDr
- * @version 1.0.0.0
- * configuration is
- * mask is sprintf like mask
- *
- * array (
- * 'newFieldName' => array('fields'=>array('field1','field2'), 'mask'=>'%s,%s'),
- * 'newFieldName2' => array('fields'=>array('field1','field3'), 'mask'=>'%s: %s')
- * );
- */
- class AutoFieldBehavior extends ModelBehavior{
- var $settings = null;
- //var $_model = null;
- $this->autoFieldSetup(&$model, $config);
- }
- //$config = array('enabled'=>true);
- foreach ($config as $newField => $fieldDsc) {
- foreach ($fieldDsc['fields'] as $field) {
- if(!$model->hasField($field)) {
- //user_error(''Model {$model->name} does not have a field called {$field}'', E_USER_ERROR);
- }
- }
- }
- }
- $config[$newField]['mask']='';
- }
- } $this->settings[$model->name] = $config;
- }
- /**
- * After find method. Called after all find
- *
- * Add aditional fields
- *
- * @param AppModel $model
- * @return boolean True to continue, false to abort the save
- */
- function afterFind(&$model, $results ) {
- $config=$this->settings[$model->name];
- $i = 0;
- foreach ($config as $newField => $fieldDsc) {
- foreach ($fieldDsc['fields'] as $field) {
- $fieldVals[]=$results[$i][$model->name][$field];
- } else {
- $fieldVals[]='';
- }
- }
- }
- }
- $i++;
- }
- }
- return $results;
- }
- }
- ?>
Parsed in 0.104 seconds, using GeSHi 1.0.7.14