Zend DB atomic transactions
I have to perform a group of SQL operations that either survive together or die together. So that if any part of my statement/s fail, the entire process is rolled back to the beginning as if the transaction never took place.
without zend I would do:
$conn->query('BEGIN');
$conn->query($myQuery);
//rollback if anything went wrong
$conn->query('COMMIT');
In Zend I created the Db connection in my bootstrap:
protected function _initDb()
{
$config=Zend_registry::get('config');
$db = Zend_Db::factory($config->resources->db->adapter, $config->resources->db->params);
//set default adapter
Zend_Db_Table_Abstract::setDefaultAdapter($db);
Zend_Registry::s开发者_Python百科et("db", $db);
}
And interactions with Db are made by models classes (extending Zend_Db_Table_abstract)
class Platform extends Zend_Db_Table_Abstract
{
public function insertPlatform($x)
{
$this->insert($x);
}
}
class Game extends Zend_Db_Table_Abstract
{
public function insertGame($x)
{
$this->insert($x);
}
}
How could I achieve the same (survive together or die together)in Zend?
thanks
Luca
Zend_Db wraps the transaction functionality too. So there is nothing different in using it with or without ZF.
You find ZF transactions explained in the Adapter Section of the Zend_Db Manual. Look for the paragraph 'Controlling Database Transactions'.
精彩评论