考虑以下几点:是否Zend_Db_Adapter :: beginTransaction()堆栈?
/** (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;
}
}
注意这两个是如何要求的交易工作,因为否则操作不会是原子(这将是坏的;))然而,有两个交易模块怎么回事。原始的DeleteNode方法调用Cas_Acl::Delete()
,它也尝试在事务块内执行自己。理想情况下,Zend_Db足够聪明以识别这种情况,对于这个特定的调用忽略Cas_Acl::Delete
内的开始事务和提交/回滚调用。
上面的代码是否安全?它能以任何方式显着改善吗?
你有时直接调用Delete()吗?还是只在`DeleteNode()`中调用? – singles 2011-02-08 07:22:32
@singles:是 - 只要需要删除ACL,就会调用“Cas_Acl :: Delete”。还有一个ACL附加到这个应用程序管理的大多数对象。其中一些对象使用交易,而另一些则不需要。 – 2011-02-08 08:38:14
你的代码提出了我的问题,就像tintin http://omg.wthax.org/haddock.jpg中的haddock的胡子一样,我认为“$ adapter-> beginTransaction();”应该在“try {} catch”之前设置,而不是在 – regilero 2011-02-08 12:49:46