src/wcmf/lib/model/mapper/impl/AbstractRDBMapper.php
protected function getNextId() {
    try {
      $sequenceMapper = $this->getSequenceMapper();
      $sequenceTable = $sequenceMapper->getRealTableName();
      $sequenceConn = $sequenceMapper->getConnection();
      $tableName = strtolower($this->getRealTableName());

      // run id sequence in it's own transaction, if supported
      if (!$this->isFileDB) {
        $sequenceConn->beginTransaction();
      }
      if ($this->idSelectStmt == null) {
        $this->idSelectStmt = $sequenceConn->prepare("SELECT ".$this->quoteIdentifier("id").
                " FROM ".$this->quoteIdentifier($sequenceTable)." WHERE ".
                $this->quoteIdentifier("table")."=".$this->quoteValue($tableName));
      }
      if ($this->idInsertStmt == null) {
        $this->idInsertStmt = $sequenceConn->prepare("INSERT INTO ".
                $this->quoteIdentifier($sequenceTable)." (".$this->quoteIdentifier("id").
                ", ".$this->quoteIdentifier("table").") VALUES (1, ".
                $this->quoteValue($tableName).")");
      }
      if ($this->idUpdateStmt == null) {
        $this->idUpdateStmt = $sequenceConn->prepare("UPDATE ".$this->quoteIdentifier($sequenceTable).
                " SET ".$this->quoteIdentifier("id")."=(".$this->quoteIdentifier("id")."+1) WHERE ".
                $this->quoteIdentifier("table")."=".$this->quoteValue($tableName));
      }
      $this->idSelectStmt->execute();
      $rows = $this->idSelectStmt->fetchAll(PDO::FETCH_ASSOC);
      if (sizeof($rows) == 0) {
        $this->idInsertStmt->execute();
        $this->idInsertStmt->closeCursor();
        $rows = [['id' => 1]];
      }
      $id = $rows[0]['id'];
      $this->idUpdateStmt->execute();
      $this->idUpdateStmt->closeCursor();
      $this->idSelectStmt->closeCursor();
      if (!$this->isFileDB) {
        $sequenceConn->commit();
      }
      return $id;
    }
    catch (\Exception $ex) {
      self::$logger->error("The next id query caused the following exception:\n".$ex->getMessage());
      throw new PersistenceException("Error in persistent operation. See log file for details.");
    }
  }
protected function getNextId() {
    try {
      $sequenceMapper = $this->getSequenceMapper();
      $sequenceTable = $sequenceMapper->getRealTableName();
      $sequenceConn = $sequenceMapper->getConnection();
      $tableName = strtolower($this->getRealTableName());

      // run id sequence in it's own transaction, if supported
      if (!$this->isFileDB) {
        $sequenceConn->beginTransaction();
      }
      if ($this->idSelectStmt == null) {
        $this->idSelectStmt = $sequenceConn->prepare("SELECT ".$this->quoteIdentifier("id").
                " FROM ".$this->quoteIdentifier($sequenceTable)." WHERE ".
                $this->quoteIdentifier("table")."=".$this->quoteValue($tableName));
      }
      if ($this->idInsertStmt == null) {
        $this->idInsertStmt = $sequenceConn->prepare("INSERT INTO ".
                $this->quoteIdentifier($sequenceTable)." (".$this->quoteIdentifier("id").
                ", ".$this->quoteIdentifier("table").") VALUES (1, ".
                $this->quoteValue($tableName).")");
      }
      if ($this->idUpdateStmt == null) {
        $this->idUpdateStmt = $sequenceConn->prepare("UPDATE ".$this->quoteIdentifier($sequenceTable).
                " SET ".$this->quoteIdentifier("id")."=(".$this->quoteIdentifier("id")."+1) WHERE ".
                $this->quoteIdentifier("table")."=".$this->quoteValue($tableName));
      }
      $this->idSelectStmt->execute();
      $rows = $this->idSelectStmt->fetchAll(PDO::FETCH_ASSOC);
      if (sizeof($rows) == 0) {
        $this->idInsertStmt->execute();
        $this->idInsertStmt->closeCursor();
        $rows = [['id' => 1]];
      }
      $id = $rows[0]['id'];
      $this->idUpdateStmt->execute();
      $this->idUpdateStmt->closeCursor();
      $this->idSelectStmt->closeCursor();
      if (!$this->isFileDB) {
        $sequenceConn->commit();
      }
      return $id;
    }
    catch (\Exception $ex) {
      self::$logger->error("The next id query caused the following exception:\n".$ex->getMessage());
      throw new PersistenceException("Error in persistent operation. See log file for details.");
    }
  }
protected function getNextId ( ) { try { $ sequenceMapper = $ this -> getSequenceMapper ( ) ; $ sequenceTable = $ sequenceMapper -> getRealTableName ( ) ; $ sequenceConn = $ sequenceMapper -> getConnection ( ) ; $ tableName = strtolower ( $ this -> getRealTableName ( ) ) ; // run id sequence in it's own transaction, if supported if ( ! $ this -> isFileDB ) { $ sequenceConn -> beginTransaction ( ) ; } if ( $ this -> idSelectStmt == null ) { $ this -> idSelectStmt = $ sequenceConn -> prepare ( "SELECT " . $ this -> quoteIdentifier ( "id" ) . " FROM " . $ this -> quoteIdentifier ( $ sequenceTable ) . " WHERE " . $ this -> quoteIdentifier ( "table" ) . "=" . $ this -> quoteValue ( $ tableName ) ) ; } if ( $ this -> idInsertStmt == null ) { $ this -> idInsertStmt = $ sequenceConn -> prepare ( "INSERT INTO " . $ this -> quoteIdentifier ( $ sequenceTable ) . " (" . $ this -> quoteIdentifier ( "id" ) . ", " . $ this -> quoteIdentifier ( "table" ) . ") VALUES (1, " . $ this -> quoteValue ( $ tableName ) . ")" ) ; } if ( $ this -> idUpdateStmt == null ) { $ this -> idUpdateStmt = $ sequenceConn -> prepare ( "UPDATE " . $ this -> quoteIdentifier ( $ sequenceTable ) . " SET " . $ this -> quoteIdentifier ( "id" ) . "=(" . $ this -> quoteIdentifier ( "id" ) . "+1) WHERE " . $ this -> quoteIdentifier ( "table" ) . "=" . $ this -> quoteValue ( $ tableName ) ) ; } $ this -> idSelectStmt -> execute ( ) ; $ rows = $ this -> idSelectStmt -> fetchAll ( PDO :: FETCH_ASSOC ) ; if ( sizeof ( $ rows ) == 0 ) { $ this -> idInsertStmt -> execute ( ) ; $ this -> idInsertStmt -> closeCursor ( ) ; $ rows = [ [ 'id' => 1 ] ] ; } $ id = $ rows [ 0 ] [ 'id' ] ; $ this -> idUpdateStmt -> execute ( ) ; $ this -> idUpdateStmt -> closeCursor ( ) ; $ this -> idSelectStmt -> closeCursor ( ) ; if ( ! $ this -> isFileDB ) { $ sequenceConn -> commit ( ) ; } return $ id ; } catch ( \ Exception $ ex ) { self :: $ logger -> error ( "The next id query caused the following exception:\n" . $ ex -> getMessage ( ) ) ; throw new PersistenceException ( "Error in persistent operation. See log file for details." ) ; } }
Get a new id for inserting into the database
@return An id value.
Get a new id for inserting into the database