gpt4 book ai didi

php - Zend_Db_Adapter::beginTransaction() 是否堆栈?

转载 作者:行者123 更新时间:2023-12-04 06:35:31 28 4
gpt4 key购买 nike

考虑以下:

/** (Cas_Template_Tree::DeleteNode)
* Deletes the given node from the tree, and all of it's children.
*
* @static
* @throws Exception
* @param Cas_Template_Node $node
* @return void
*/
public static function DeleteNode(Cas_Template_Node $node)
{
$table = new Cas_Table_Templates();
$adapter = $table->getAdapter();
$leftStr = $adapter->quoteIdentifier('Left');
$rightStr = $adapter->quoteIdentifier('Right');
try
{
$adapter->beginTransaction();
$row = $table->find($node->GetId())->current();
$dependantRowSelector = array(
"$leftStr >= ?" => $row->Left,
"$rightStr <= ?" => $row->Right
);
//Get the rows removed so that we can nuke the ACLs later.
$rowsToDelete = $table->fetchAll($dependantRowSelector)->toArray();
//Delete the rows.
$table->delete($dependantRowSelector);
//Delete access control lists on those rows.
foreach ($rowsToDelete as $rowToDelete)
{
Cas_Acl::CreateExisting($rowToDelete['Acl'])->Delete();
}
$left = (int)$row->Left;
$right = (int)$row->Right;
$difference = $right - $left + 1;
$table->update(array('Left' => new Zend_Db_Expr("$leftStr - $difference")),
array("$leftStr > ?" => $right));
$table->update(array('Right' => new Zend_Db_Expr("$rightStr - $difference")),
array("$rightStr > ?" => $right));
$adapter->commit();
}
catch (Exception $ex)
{
$adapter->rollBack();
throw $ex;
}
}

/** (Cas_Acl::Delete)
* Removes this ACL (and all of its dependent access control entries) from the database.
* @return void
*/
public function Delete()
{
$aclTable = new Cas_Table_AccessControlLists();
$aceTable = new Cas_Table_AccessControlEntries();
$adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
$identifierName = $adapter->quoteIdentifier('Identifier');
$aclName = $adapter->quoteIdentifier('Acl');
try
{
$adapter->beginTransaction();
$aceTable->delete(array("$aclName = ?" => $this->GetId()));
$aclTable->delete(array("$identifierName = ?" => $this->GetId()));
}
catch (Exception $ex)
{
$adapter->rollBack();
throw $ex;
}
}

注意这两个都要求事务如何工作,否则操作将不是原子的(这会很糟糕;))但是,这里有两个事务 block 。原来的 DeleteNode 方法调用 Cas_Acl::Delete() ,它还尝试在事务 block 内执行自身。理想情况下,Zend_Db 会足够聪明地识别这种情况,并且对于这个特定的调用忽略 Cas_Acl::Delete 中的开始事务和提交/回滚调用。 .

上面的代码安全吗?它可以以任何方式显着改善吗?

最佳答案

阿法克Zend_Db无法识别嵌套事务。看代码。

public function beginTransaction()
{
$this->_connect();
$q = $this->_profiler->queryStart('begin', Zend_Db_Profiler::TRANSACTION);
$this->_beginTransaction();
$this->_profiler->queryEnd($q);
return $this;
}

这里没有代码来识别另一个事务(但也许分析器可以用来做这样的事情)和 _beginTransaction依赖于 PDO 的 beginTransaction .

您可能要做的是将第二个参数添加到 Delete()确定是否使用事务的方法,并使用 false 调用它 DeleteNode() 中的参数:
//RB called as Cas_Acl::CreateExisting($rowToDelete['Acl'])->Delete(false);
public function Delete($useTransaction = true)
{
$aclTable = new Cas_Table_AccessControlLists();
$aceTable = new Cas_Table_AccessControlEntries();
$adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
$identifierName = $adapter->quoteIdentifier('Identifier');
$aclName = $adapter->quoteIdentifier('Acl');
try
{
if ($useTransaction === true) {
$adapter->beginTransaction();
}
$aceTable->delete(array("$aclName = ?" => $this->GetId()));
$aclTable->delete(array("$identifierName = ?" => $this->GetId()));
//BTW isn't commit() should be called here?
}
catch (Exception $ex)
{
if ($useTransaction === true) {
$adapter->rollBack();
}
throw $ex;
}
}

关于php - Zend_Db_Adapter::beginTransaction() 是否堆栈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4929947/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com