添加事务后,代码将不会运行

时间:2017-11-26 20:09:45

标签: php mysql pdo transactions

您好我的代码是没有事务执行但是事务没有运行。我的DB Class封装了我在BaseApp Class中编写的预准备语句,而BaseTransactApp Class又扩展了这些语句。

BasetransactApp ClassEqRqst Class扩展。 EqRqst ClassEqLeaseRequest Class扩展,2SQLSTATE[HY000]: General error用于使用有问题的代码调用特征中的代码。 有一次它显示错误$this->_pdo->setAttribute( PDO::ATTR_EMULATE_PREPARES , false); 当我没有设置

DB Class

我的 <?php class DB{ `enter code here` private static $_instance = null; private $_pdo, $_query, $_error = false, $_results, $x, $_count = 0; private function __construct(){ try{ $this->_pdo = new PDO('mysql:host=' .Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password')); $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->_pdo->setAttribute( PDO::ATTR_EMULATE_PREPARES , false); }catch(PDOException $e){ die($e->getMessage()); } } public static function getInstance(){ if(!isset(self::$_instance)){ self::$_instance = new DB(); } return self::$_instance; } public function query($sql, $params = array()){ $this->_error = false; if($this->_query = $this->_pdo->prepare($sql)){ $x = 1; if(count($params)){ foreach($params as $param){ $this->_query->bindValue($x,$param); $x++; } } if($this->_query->execute()){ $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ); $this->_count = $this->_query->rowCount(); }else{ $this->_error = true; } } return $this; } public function action($action,$table,$where = array()){ if(count($where) === 3){ $operators = array('=','>','<','>=','<='); $field = $where[0]; $operator = $where[1]; $value = $where[2]; if(in_array($operator , $operators)){ $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ? "; if(!$this->query($sql, array($value))->error()){ return $this; } } } return false; } public function update($table,$id,$fields){ $set = ''; $x = 1; foreach($fields as $name => $value){ $set .= "{$name} = ?"; if($x < count($fields)){ $set .= ', '; } $x++; } $sql = "UPDATE {$table} SET {$set} WHERE {$name} = '{$id}'"; echo $sql; if(!$this->query($sql,$fields)->error()){ echo "true"; return true; } return false; } public function get($table,$where){ return $this->action('SELECT *',$table,$where); } public function delete($table,$where){ return $this->action('DELETE',$table,$where); } public function insert($table,$fields = array()){ if(count($fields)){ echo 'this is the count '.count($fields); $x=1; if(!is_multi_array($fields)){ echo 'Not a multi array'.'<br>'; $keys = array_keys($fields); print_r($keys); $values = ''; } foreach($fields as $field => $fieldValue){ if(!is_array($fieldValue)){ echo $x; $values .="?"; if($x< count($fields)){ $values .= ', '; } if($x == count($fields)){ echo 'insert query is Executed'.'<br>'; echo $x; $sql ="INSERT INTO {$table} (`".implode('`,`',$keys). "`) VALUES ({$values})"; if(!$this->query($sql,$fields)->error()){ echo "true"; return true; } } $x++; }else{ if($x <= count($fields)){ echo 'recursive insert called'.'<br>'; echo "number of recursive cycles is ".$x; $this->insert($table,$fieldValue); if($x == count($fields)){ echo "Escaped"; exit(); } $x++; } } } } echo 'No no'; return false; } public function getLastId(){ return $this->_pdo->lastInsertId(); } public function beginTransaction(){ return $this->_pdo->beginTransaction(); } public function commit(){ return $this->_pdo->commit(); } public function rollBack(){ return $this->_pdo->rollBack(); } } ?>

BaseApp Class

IT由 protected $db, $data, $table; public function __construct(){ $this->db = DB::getInstance(); } public function getProp($prop){ return $this->$_prop; } public function setProp($objVar,$val){ $this->objVar = $val; } public function create($fields = array()){ if(!$this->db->insert($this->table, $fields) ){ throw new Exception('There was a problem creating an account.'); } } public function get($where = array()){ $dbResult = $this->db->get($this->table, $where); if($dbResult->count()){ $this->data = $dbResult->first(); return true; } return false; } public function update($fields = array(),$id = null){ if(!$this->db->update($this->table,$id,$fields)){ throw new Exception('There was a problem Updating'); } } public function delete($where = array()){ $this->db->delete($this->table,$where); } public function data(){ return $this->data; } public function getTable(){ return $this->table; } public function getLastInsertId(){ return $this->db->getLastId(); } } ?> 组成     

   public function __construct(){

    parent::__construct();
     if($this->db){
        echo 'WWWWWWWWWWWWWWWW';
     }

   }

    public function beginTransaction(){
        return $this->db->beginTransaction();
    }
    public function commit(){
        return $this->db->commit();
    }
    public function rollBack(){
        return $this->db->rollBack();
    } 
}

?>

由...延长     

    protected $EqRqst_ID,
              $DateOfRequest,
              $rqstState,
              $RequestedPackage = array(),
              $state_ID,
              $table = 'EqRqst';

    public function __construct(RqstState $state){
        parent::__construct();

        $this->rqstState = $state;

    }
   public function create($args = array()){
     BaseTransactApp::create($args);
   // $this->EqRqst_ID = $this->getLastInsertId();
   // echo $this->EqRqst_ID;
   }
    public function getRqstID(){
     return $this->EqRqst_ID;
    }
    public abstract function makeRequest($requestArg = array(),$elRqst = array(),$rqstPakg = array());



}

?>

然后     

    public function makeRequest($requestArg = array(),$eqLsRqst = array(),$rqstPakg = array()){

        try{

        $this->beginTransaction();
        parent::create($requestArg);
        $this->EqRqst_ID = parent::getLastInsertId();
        $this->create($eqLsRqst);
        $this->_RequestPkg->create(multi_array_merge(array('EqRqst_ID'=> $this->EqRqst_ID),$rqstPakg));
     $this->commit();
            }catch(Exception $e){
                $this->rollBack();
                echo $e->getMessage().;
            }
        //$this->EqRqst_ID = $this->getRqstID();
    }

此特征包含eqleaserequest使用的代码。此代码在没有事务的情况下执行良好但无法使用它们执行此操作     

    use traitRequest;

   //protected  $table = 'EqLeaseRequest';



    private $_Emp,
            $_Emp_ID,
            $_RequestPkg,
            $_Intended_Use,
            $_EqRqst,
            $_EqRqst_ID,
            $_Expected_Returndate;


     public $rqstID;

    public function __construct(RqstState $state,Employee $Emp,RequestPackage $RqstPkg){


        parent::__construct($state);
        $this->_Emp = $Emp;
        $this->_RequestPkg = $RqstPkg;



    }
   public function create($eqLsRqstArg = array()){
       $this->table = 'EqLeaseRequest';
       print_r(array_merge(array('EqRqst_ID'=> $this->EqRqst_ID),$eqLsRqstArg));
       BaseTransactApp::create(array_merge(array('EqRqst_ID'=>$this->EqRqst_ID),$eqLsRqstArg));
       //BaseTransactApp::create($eqLsRqstArg);



   }



    }


}


?>

此代码是我在注释掉事务代码时运行的问题,但没有使用它。 <执行

此类是使用TraitRequest的类     

{{1}}

1 个答案:

答案 0 :(得分:-1)

我能够通过更改Trait traitRequest中的代码找到解决方案。

解决方案如下

trait traitRequest {     public function makeRequest($ requestArg = array(),$ eqLsRqst = array(),$ rqstPakg = array()){

    $this->beginTransaction();
    parent::create($requestArg);
     if(parent::count()){
         $this->EqRqst_ID = parent::getLastInsertId();
         $this->create($eqLsRqst);
         if($this->count()){
              foreach($rqstPakg as $pkg){
                  $this->_RequestPkg->create(array_merge(array('EqRqst_ID'=> $this->EqRqst_ID),$pkg));
                    if(!$this->_RequestPkg->count()){
                 $this->rollBack(); 
            }

              }
              $this->commit();


         }else{
             echo 'Second Rollback';
          $this->rollBack();
         }
     }else{


        echo 'First Rollback';
          $this->rollBack();
     }
}

}

&GT;