php – 在封装PDO事务时使用闭包不起作用.为什么?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 在封装PDO事务时使用闭包不起作用.为什么?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4122字,纯文字阅读大概需要6分钟。
内容图文
![php – 在封装PDO事务时使用闭包不起作用.为什么?](/upload/InfoBanner/zyjiaocheng/780/35de96a85fe549c594b310659493bb9c.jpg)
我目前在封装PDO事务方面存在问题,易于使用;执行事务后,没有发生数据库更改!我的想法是只提供在PDO事务中执行所需的参数和可调用事务.受保护的executeTransaction方法定义PDO事务的封装,如下所示.客户端方法是getNextWidWithLock,它使用executeTransaction方法.我试图将交易从关闭中移出来并且有效!可能是为什么它似乎没有将更改提交到数据库的原因.
class ParentRepository
{ ...
/**
* Executes a transaction with logging.
* @param [type] $data data to be passed inside the callable.
* @param Callable $transaction function of the procedure
* @return [type] [description]
*/
protected function executeTransaction(Array $data, Callable $transaction)
{
$returnVariable = false;
try
{
$this->mySqlConnect->beginTransaction();
$returnVariable = $transaction($data);
$this->mySqlConnect->commit();
}
catch(\PDOException $e)
{
$this->mySqlConnect->rollBack();
// Log errors.
$dateTime = date("Y-m-d H:i", time());
self::log(sprintf("Error Transaction @ %s" . PHP_EOL, $dateTime));
self::log(sprintf("[Error Message] => %s %s" . PHP_EOL, PHP_EOL, $e->getMessage()));
self::log(sprintf("[File] => %s" . PHP_EOL, $e->getFile()));
self::log(sprintf("[Line Number] => %s" . PHP_EOL, $e->getLine()));
self::log(sprintf("[Back Trace] => %s %s" . PHP_EOL, PHP_EOL, $e->getTraceAsString()));
self::log("______________________________________" . PHP_EOL);
throw $e;
}
return $returnVariable;
}
/**
* Reserves wid in the given table with write-locking.
* @param [type] $tableName [description]
* @param [type] $branchId [description]
* @param integer $reservedSlots [description]
* @return array returns an array of wids.
* The last element must be used in an UPDATE statement while
* the rest must be used in an INSERT statement.
*/
protected function getNextWidWithLock($tableName, $branchId, $reservedSlots = 1)
{
if ($reservedSlots === 0)
return [];
$this->executeTransaction(
[
'tableName' => $tableName,
'branchId' => $branchId,
'reservedSlots' => $reservedSlots
],
function($params)
{
extract($params);
$minimumWid = $branchId . "0000000";
$maximumWid = $branchId . "9999999";
$preparedStatements = array(
$this->mySqlConnect->prepare("SET @wid_d = 0"),
$this->mySqlConnect->prepare("SELECT COALESCE(MAX(`wid`), $minimumWid) INTO @wid_d FROM $tableName
WHERE `wid` >= $minimumWid AND `wid` <= $maximumWid FOR UPDATE"),
$this->mySqlConnect->prepare("INSERT INTO $tableName(`wid`)
VALUES ( IF(@wid_d = 0, $minimumWid + 1 , @wid_d + $reservedSlots) )")
);
foreach ($preparedStatements as $statement)
$statement->execute();
$result = $this->mySqlConnect->prepare("SELECT `wid` FROM $tableName WHERE id = " . $this->mySqlConnect->lastInsertId());
$result->execute();
$end = $result->fetchColumn();
$statement = $this->mySqlConnect->prepare("SELECT IF(@wid_d = 0, $minimumWid + 1 , @wid_d + 1)");
$statement->execute();
$begin = $statement->fetchColumn();
return range($begin, $end);
}
);
}
}
解决方法:
你在关闭时使用$this,但它从PHP 5.3开始是not supporting
你可以写这样的东西:
protected function getNextWidWithLock($tableName, $branchId, $reservedSlots = 1)
{
if ($reservedSlots === 0)
return [];
$self = $this;
$this->executeTransaction(
[
'tableName' => $tableName,
'branchId' => $branchId,
'reservedSlots' => $reservedSlots
],
function($params) use ($self, $tableName, $branchId, $reservedSlots)
{
extract($params);
$minimumWid = $branchId . "0000000";
$maximumWid = $branchId . "9999999";
$preparedStatements = array(
$self->mySqlConnect->prepare("SET @wid_d = 0"),
$self->mySqlConnect->prepare("SELECT COALESCE(MAX(`wid`), $minimumWid) INTO @wid_d FROM $tableName
WHERE `wid` >= $minimumWid AND `wid` <= $maximumWid FOR UPDATE"),
$self->mySqlConnect->prepare("INSERT INTO $tableName(`wid`)
VALUES ( IF(@wid_d = 0, $minimumWid + 1 , @wid_d + $reservedSlots) )")
);
foreach ($preparedStatements as $statement)
$statement->execute();
$result = $self->mySqlConnect->prepare("SELECT `wid` FROM $tableName WHERE id = " . $self->mySqlConnect->lastInsertId());
$result->execute();
$end = $result->fetchColumn();
$statement = $self->mySqlConnect->prepare("SELECT IF(@wid_d = 0, $minimumWid + 1 , @wid_d + 1)");
$statement->execute();
$begin = $statement->fetchColumn();
return range($begin, $end);
}
);
}
}
内容总结
以上是互联网集市为您收集整理的php – 在封装PDO事务时使用闭包不起作用.为什么?全部内容,希望文章能够帮你解决php – 在封装PDO事务时使用闭包不起作用.为什么?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。