Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用具有多个类的静态transactionCounter管理事务是否正确?_Php_Mysql_Database_Pdo_Insert - Fatal编程技术网

Php 使用具有多个类的静态transactionCounter管理事务是否正确?

Php 使用具有多个类的静态transactionCounter管理事务是否正确?,php,mysql,database,pdo,insert,Php,Mysql,Database,Pdo,Insert,我对使用中描述的数据库类感兴趣 但该类有一个问题,它使用了一个非静态的$transactionCount属性,这意味着如果我有多个类开始事务,它将导致多个提交,并将使我的代码最终出错,请看下面的示例(不可运行,只是为了理解): 正如您在这段代码的注释中所看到的,$transactionCount的值在是否为静态的情况下是非常不同的,但是这个问题的重点是关于静态属性,如果在执行过程中它的值可能会被其他代码线程/函数或外部调用更改(例如)createFieldToForm。你能给我推荐什么吗?这个类

我对使用中描述的数据库类感兴趣

但该类有一个问题,它使用了一个非静态的
$transactionCount
属性,这意味着如果我有多个类开始事务,它将导致多个提交,并将使我的代码最终出错,请看下面的示例(不可运行,只是为了理解):


正如您在这段代码的注释中所看到的,
$transactionCount
的值在是否为静态的情况下是非常不同的,但是这个问题的重点是关于静态属性,如果在执行过程中它的值可能会被其他代码线程/函数或外部调用更改(例如)
createFieldToForm
。你能给我推荐什么吗?

这个类不是来自PHP手册,而是来自手册页面的注释。这使得它故意不可靠的来源

要解决你的问题,只需摆脱所有这些“交易”。因为只对单个查询使用事务是没有意义的


因此,您很可能既不需要事务,也不需要计数器,或者根本不需要此类。

此类不是来自PHP手册,而是来自手册页面的注释。这使得它故意不可靠的来源

要解决你的问题,只需摆脱所有这些“交易”。因为只对单个查询使用事务是没有意义的


因此,很可能您既不需要事务,也不需要计数器,也不需要这个类。

它实际上只是一个类似ORM的悲伤版本,或者无论如何。它实际上只是一个类似ORM的悲伤版本,或者无论如何。
class Field extends Database {
    public function createFieldToForm( $fieldName, $formId )
    {
         // static transactionCount = 0
         // transactionCount = 0
         $this->beginTransaction();   
//           .... createFieldToForm code ....
         $last_id_new_fieldName = $this->insert( $fieldName );
         $formFields = new FormFields();
         $formFields->assignFieldToForm( $last_id_new_fieldName, $formId );
         $this->commit();
    }

    public function insert( $fieldName )
    {
         try {
              // static transactionCount = 1
              // transactionCount = 1
              $this->beginTransaction();
              $dbh = Database::getDatabaseConnection();
              $dbh->prepare( "INSERT INTO form(fieldname) VALUES (:fieldname)" );
            //   ... bind ...
              $dbh->execute();
              $this->commit();
              return $dbh->lastInsertId();
         }
         catch (PDOException $e) {
             $this->rollback();
         }
    }
}

class FormFields extends Database {
    public function assignFieldToForm( $idFieldName, $formId )
    {
       // static transactionCount = 1
              // transactionCount = 0
       $this->beginTransaction();
//              ...  assign the created field to the form ....
       $this->insert( $idFieldName, $formId );
       $this->commit();
    }

    public function insert( $idFieldName, $formId )
    {
         try {
 // static transactionCount = 2
              // transactionCount = 1
              $this->beginTransaction();
              $dbh = Database::getDatabaseConnection();
              $dbh->prepare( "INSERT INTO form_fields(idfield,formid) VALUES (:idfield,:formid)" );
            //   ... bind ...
              $dbh->execute();
              $this->commit();
         }
         catch (PDOException $e) {
             $this->rollback();
         }
    }
}

const FORM_USER_INFORMATION = 3;
$fieldPhone = new Field();
$fieldPhone->createFieldToForm( "phone_number", FORM_USER_INFORMATION  );