transactional() public method

The function gets passed this Connection instance as an (optional) parameter. If an exception occurs during execution of the function or transaction commit, the transaction is rolled back and the exception re-thrown.
public transactional ( Closur\Closure $func ) : mixed
$func Closur\Closure The function to execute transactionally.
return mixed The value returned by $func
 /**
  * @param string $endpointName
  *
  * @return int
  */
 public function fetchOrGenerateEndpointId($endpointName)
 {
     $endpointId = 0;
     $this->connection->transactional(function (Connection $connection) use($endpointName, &$endpointId) {
         $lookupHash = md5($endpointName);
         $endpointRecord = $connection->executeQuery("SELECT * FROM {$this->endpointsTableName} WHERE lookup_hash = ?", [hex2bin($lookupHash)])->fetch(\PDO::FETCH_ASSOC);
         if (!$endpointRecord) {
             $connection->insert($this->endpointsTableName, ['lookup_hash' => hex2bin($lookupHash), 'name' => $endpointName]);
             $endpointId = (int) $connection->lastInsertId();
         } else {
             $endpointId = (int) $endpointRecord['id'];
         }
     });
     return $endpointId;
 }
Ejemplo n.º 2
0
 function transactional(\Closure $func)
 {
     $this->_con->transactional($func);
 }