Exemplo n.º 1
0
 /**
  * Save a guestbook entry
  *
  * @param  Default_Model_Guestbook $guestbook
  * @return void
  */
 public function save(App_Model_Abstract $model)
 {
     // echo get_class ( $model ),"[".$model->id."]";
     App_Log::get()->debug($this, '==================================================================================================================');
     App_Log::get()->debug($this, 'Model :' . get_class($model));
     App_Log::get()->debug($this, 'SAVE : begin');
     $data = array();
     $pk = '';
     $config = $this->getModelConfig();
     $id = '';
     $attrtmp = array();
     foreach ($config->prop as $prop) {
         if ($prop->proptype == 'view') {
             continue;
         }
         if ($prop->pk) {
             $pk = $prop;
         }
         $col = $prop->column;
         $name = $prop->name;
         $type = $prop->type;
         if (method_exists($model, "get{$name}")) {
             $val = $model->{"get{$name}"}();
             if (!isset($val)) {
                 //echo "notset ".$name,'=',$val,'<br/>';
                 continue;
             }
             //echo "set ".$name,'=',$val,'<br/>';
             //echo $name," type ",$type,'<br/>';
             //echo $val ,$type,'<br/>';
             if ($val == self::DIRECTIVE_SETTONULL or $val == self::DIRECTIVE_SETTONULLDATE or $val == 'NULL') {
                 $data[$col] = NULL;
             } elseif ($val instanceof DateTime) {
                 if ($val != NULL) {
                     if (APPLICATION_ENV == 'production') {
                         $data[$col] = $val->format($this->_datetime_format);
                     } else {
                         $data[$col] = $val->format($this->_datetime_format);
                     }
                     $attrtmp[] = "{$col}=" . $val->format($this->_datetime_format);
                     App_Log::get()->debug($this, "datetime val {$col} =  : " . $val->format($this->_datetime_format));
                 }
             } elseif ($type == 'datetime' or $type == 'date') {
                 if (trim($val) == '') {
                     $data[$col] = NULL;
                 } else {
                     if ($this->date_check($val)) {
                         $val = $this->format_date($val, $this->_datetime_format);
                     }
                     $data[$col] = $val;
                 }
             } elseif ($type == 'currency' or $type == 'monney') {
                 if (trim($val) == '') {
                     $data[$col] = NULL;
                 } else {
                     $data[$col] = App_Formatter::toDecimal($val);
                 }
             } elseif ($type == 'int' or $type == 'integer') {
                 if ($val == 0) {
                     $data[$col] = 0;
                 } elseif (trim($val) == '') {
                     $data[$col] = NULL;
                 } else {
                     $data[$col] = (int) $val;
                 }
             } elseif ($type == 'decimal' or $type == 'decimal') {
                 if (trim($val) == '') {
                     $data[$col] = NULL;
                 } else {
                     $data[$col] = App_Formatter::toDecimal($val);
                 }
             } elseif (is_string($val)) {
                 $data[$col] = $this->_iconv('UTF-8', 'TIS-620', $val);
                 $attrtmp[] = "{$col}=" . $val;
                 App_Log::get()->debug($this, "string val {$col} =  : {$val}");
             } else {
                 if ($val != NULL) {
                     $data[$col] = $val;
                     //echo "name else = ".$col;
                     //App_Log::get ()->debug ( $this, "else val $col =  : $val" );
                 }
             }
         }
     }
     //die($val);
     $this->_attribuevalue = join(",", $attrtmp);
     $pkAtr = $pk->name;
     $pkCol = $pk->column;
     // echo "[".$model->id."]";
     //echo "get{$pkAtr}";
     //die( $pkAtr);
     $id = trim($model->{$pkAtr});
     //call_user_method("get{$pkAtr}",$model);
     //die($id);
     //if(isset( $model->{$pkAtr}) ){
     //$id = call_user_method("get$pkAtr",$model);
     //$id = $model->$pkAtr; //$model->{"get$pkAtr"}();
     //}
     App_Log::get()->debug($this, 'val $pkAtr =  : ' . $pkAtr);
     //print_r($pk);
     //echo $pk->autoIncrement;
     App_Log::get()->debug($this, 'val $pk->autoIncrement =  : ' . $pk->autoIncrement);
     //print_r($data);
     //print_r($data);
     if ($pk->autoIncrement == 'false') {
         //echo('autoIncrement false');
         //exit();
         if ($id != '') {
             //die("id != null");
             $classname = $this->_model;
             //die($classname);
             App_Log::get()->debug($this, "{$classname}");
             $c = new $classname();
             //echo
             $result = $c->find($id);
             //echo $c->code;
             //exit();
             //App_Log::get()->debug($this,"befor if [$result] ");
             //die($c->$pkAtr) ;
             if (trim($c->{$pkAtr}) != '') {
                 App_Log::get()->debug($this, 'Begin update with autoIncrement false : ' . " {$pkCol} = {$id} ");
                 unset($data[$pkCol]);
                 $this->update($data, array(" {$pkCol} LIKE ? " => $id));
                 $this->_lastInsertId = $id;
                 $this->_saveMethod = 'update';
                 //echo (__CLASS__).'YYY update';
                 App_Log::get()->debug($this, 'End update : ');
             } else {
                 App_Log::get()->debug($this, 'Begin inseart with autoIncrement false: ');
                 $this->_lastInsertId = $this->insert($data);
                 //echo "263=",$this->_lastInsertId;
                 //$this->_lastInsertId = $this->getDbTable()->getAdapter()->lastInsertId();
                 $this->_saveMethod = 'insert';
                 //echo (__CLASS__). 'YYY insert';
                 App_Log::get()->debug($this, 'End inseart : ');
             }
         } else {
             // die('UPDATE');
             throw new Exception("Can not Inseart : autoIncrement false id = [{$id}] ");
         }
     } else {
         //echo('!autoIncrement');
         App_Log::get()->debug($this, 'val $id =  : ' . $id);
         if ($id != '') {
             unset($data[$pkCol]);
             //print_r($data);
             //App_Log::get ()->debug ( $this, 'Begin update : ' . " $pkCol = $id " );
             $this->update($data, array(" {$pkCol} = ? " => $id));
             $this->_lastInsertId = $id;
             $this->_saveMethod = 'update';
             //echo (__CLASS__).'XXX update';
             //App_Log::get ()->debug ( $this, 'End update : ' );
         } else {
             //echo('count = 0');
             App_Log::get()->debug($this, 'Begin inseart : ');
             $this->_lastInsertId = $this->insert($data);
             //$this->_lastInsertId = $this->getDbTable()->getAdapter()->lastInsertId();
             $this->_saveMethod = 'insert';
             //echo (__CLASS__).'XXX insert';
             App_Log::get()->debug($this, 'End inseart : ');
         }
     }
     App_Log::get()->debug($this, 'SAVE : complete');
 }