04.12 php saved
dardosordi
Tags add more
autofield, behavior, cakeexplorer, concat, fullname and two fields  
Note
borrowed from http://cakeexplorer.wordpress.com/2007/09/10/autofield-behavior-or-how-to-add-additional-columns-to-your-models/

just formated it
  1. <?php
  2.  
  3. /**
  4. * Autofield behavior for cakePHP
  5. * comments, bug reports are welcome skie AT mail DOT ru
  6. * @author Yevgeny Tomenko aka SkieDr
  7. * @version 1.0.0.0
  8. * configuration is
  9. * mask is sprintf like mask
  10. *
  11. * array (
  12. *        'newFieldName' => array('fields'=>array('field1','field2'), 'mask'=>'%s,%s'),
  13. *        'newFieldName2' => array('fields'=>array('field1','field3'), 'mask'=>'%s: %s')
  14. *      );
  15. */
  16. class AutoFieldBehavior extends ModelBehavior{
  17.  
  18.     var $settings = null;
  19.     //var $_model = null;
  20.  
  21.     function setup(&$model, $config = array()) {
  22.         $this->autoFieldSetup(&$model, $config);
  23.     }
  24.  
  25.     function autoFieldSetup(&$model, $config = array()) {
  26.         //$config = array('enabled'=>true);
  27.         foreach ($config as $newField => $fieldDsc) {
  28.             if (isset($fieldDsc['fields'])) {
  29.                 foreach ($fieldDsc['fields'] as $field) {
  30.                     if(!$model->hasField($field)) {
  31.                         //user_error(''Model {$model->name} does not have a field called {$field}'', E_USER_ERROR);
  32.                     }
  33.                 }
  34.             }
  35.  
  36.             if (!isset($config[$newField]['fields'])) {
  37.                 $config[$newField]['fields']=array();
  38.             }
  39.  
  40.             if (!isset($config[$newField]['mask'])) {
  41.                 $config[$newField]['mask']='';
  42.             }
  43.         }         $this->settings[$model->name] = $config;
  44.     }
  45.  
  46.  
  47.  
  48.     /**
  49.      * After  find method. Called after all find
  50.      *
  51.      * Add aditional fields
  52.      *
  53.      * @param AppModel $model
  54.      * @return boolean True to continue, false to abort the save
  55.      */
  56.     function afterFind(&$model$results ) {
  57.         $config=$this->settings[$model->name];
  58.         if (is_array($results)) {
  59.             $i = 0;
  60.  
  61.             while(isset($results[$i][$model->name]) && is_array($results[$i][$model->name])) {
  62.                 if(count($results[$i][$model->name])>0) {
  63.                     foreach ($config as $newField => $fieldDsc) {
  64.                         $fieldVals=array();
  65.                         foreach ($fieldDsc['fields'] as $field) {
  66.                             if (isset($results[$i][$model->name][$field])) {
  67.                                 $fieldVals[]=$results[$i][$model->name][$field];
  68.                             } else {
  69.                                 $fieldVals[]='';
  70.                             }
  71.                         }
  72.                         $results[$i][$model->name][$newField]=vsprintf($fieldDsc['mask'],$fieldVals);
  73.                     }
  74.                 }
  75.                 $i++;
  76.             }
  77.         }
  78.         return $results;
  79.     }
  80. }
  81. ?>
Parsed in 0.104 seconds, using GeSHi 1.0.7.14

Modify this Paste