Perhaps what you can do is to have a class member variable like
isTransactionStarted and 2 other methods called beginTransaction() and
commit() like this:
private function beginTransaction() {
if ($this->isTransactionStarted === false) {
$this->objDb->beginTransaction();
$this->isTransactionStarted = true;
}
}
private function commit() {
if ($this->isTransactionStarted) {
$this->objDb->commit();
$this->isTransactionStarted = false;
}
}
Then, instead of calling $this->objDb->beginTransaction(), you call
$this->beginTransaction() which now should never begin transaction twice.
Oh yeah ... you may want to do method rollback() as well on your class.
Hope it works for you ...
MSN
Juan Felipe Alavarez Saldarriaga wrote:
>
> Hey :)
>
> Is there a way to use a initiate transaction ? I mean, I got this:
>
> class MyUsersClass
> {
> protected $objDb = null;
>
> public function __construct()
> {
> $this->objDb = Zend_Registry::get( 'objDb' );
> }
>
> public function save( Array $arrData )
> {
> $this->objDb->beginTransaction();
>
> try
> {
> // Try to insert the user groups.
> foreach ( $arrUserGroups AS $intGroupIndex => $intGroupId )
> {
> $arrUserGroupsData = array( "user_id" => $lastInsertId,
> "group_id" => $intGroupId );
>
> self::saveUserGroups( $arrUserGroupsData );
> }
>
> $this->objDb->commit();
> }
> catch ( Exception $objException )
> {
> $this->objDb->rollBack();
> }
> }
>
> public function saveUserGroups( Array $arrArguments )
> {
> if ( false !== empty( $arrArguments ) )
> {
> throw new Exception( "Data to save user groups can't be
> empty." );
> }
>
> if ( false !== empty( $arrArguments["user_id"] ) )
> {
> throw new Exception( "User id can't be empty." );
> }
>
> if ( false !== empty( $arrArguments["group_id"] ) )
> {
> throw new Exception( "Group id can't be empty." );
> }
>
> $this->objDb->beginTransaction();
>
> try
> {
> // Insert data.
> $this->objDb->insert( $this->strUserGroupsTable, $arrArguments
> );
>
> $this->objDb->commit();
> }
> catch ( Exception $objException )
> {
> $this->objDb->rollBack();
>
> throw new Exception( "Can't save user groups -
> ({$objException->getMessage()})" );
> }
> }
> }
>
> So I'm saving some user data into the save method, that method have a
> transaction started, then I call the saveUserGroups method on the same
> class and it have another
> transaction started, is possible to, I don't know, get the transaction
> started in the save method ? cause I got a fatal error that another
> transaction is started.
>
> Thx.
>
>
--
View this message in context:
http://www.nabble.com/Zend_Db-Transactions---How-to-get-a-initiate-transaction---tp15960812p16717517.html
Sent from the Zend Framework mailing list archive at Nabble.com.