Example #1
0
 /**
  * 更新paper表数据
  * 主要是更新Paper表中的行记录,通过比照name,更新description
  * @param unknown $data
  * ture 更新成功
  * false|Exception 更新失败
  */
 public static function updatePaperData()
 {
     $data = Paper::getLastedDataFromModel();
     $data_type = self::dataFormatCheck($data);
     if ($data_type != 5) {
         throw new Exception("input type is not available!");
     }
     $raw_data_count = Paper::count();
     if ($raw_data_count != count($data)) {
         throw new Exception("Please ensure the data[from table and from array] consistency");
     }
     try {
         $manager = new TxManager();
         $transaction = $manager->get();
         foreach ($data as $record) {
             $paper_table_record = Paper::findFirst(array("name = :name:", 'bind' => array('name' => strtoupper(trim($record['name'])))));
             if (isset($paper_table_record->id)) {
                 if (strcmp($paper_table_record->description, $record['description']) != 0) {
                     $paper = new Paper();
                     $paper->id = $paper_table_record->id;
                     $paper->description = $record['description'];
                     $paper->name = strtoupper(trim($record['name']));
                     if ($paper->update() == false) {
                         $transaction->rollback("Cannot update Paper data");
                     }
                 }
             }
         }
         $transaction->commit();
         return true;
     } catch (TxFailed $e) {
         throw new Exception("Failed, reason: " . $e->getMessage());
     }
 }