Nada_Surf
Nada_Surf

Reputation: 616

Is it possible to rollback transactions in Doctrine_Record 'post' hooks?

Is it possible to rollback the entire transaction from within the various Doctrine_Record 'post' hooks? I.E. from within postInsert(), postUpdate(), postSave() etc

I am using Doctrine 1.2 and from the documentation and the API it is not clear how to do this.

Upvotes: 2

Views: 232

Answers (1)

quickshiftin
quickshiftin

Reputation: 69621

Looking through the docs it's clear Doctrine_Event is your best starting point, since that's passed into the event handlers. Essentially you'll have to get a hold of the Doctrine_Connection object (through getInvoker()) and then try to call rollback on it.

Only trouble is getInvoker returns one of several types of objects and I'm unsure if they all support a rollback method, so you may need some conditional logic to determine if you can even rollback from all of them and how to do so for the different cases.

/**
 * getInvoker
 * returns the handler that invoked this event
 *
 * @return Doctrine_Connection|Doctrine_Connection_Statement|
 *         Doctrine_Connection_UnitOfWork|Doctrine_Transaction   the handler that invoked this event
 */
public function getInvoker()
{
    return $this->_invoker;
}

The documentation shows how to begin, commit and rollback through the a Doctrine_Connection so that should be the easiest starting point. So a listener would look something like:

class BlogPost extends Doctrine_Record
{
    public function postUpdate( $event )
    {
        $invoker = $event->getInvoker();
        switch(get_class($invoker)) {
            case 'Doctrine_Connection':
                $invoker->rollbakck();
                break;
            case 'Doctrine_Connection_Statement':
            case 'Doctrine_Collection_UnitOfWork':
            case 'Doctrine_Transaction':
                // todo can we rollback from these ?
                // if so figure out how :)
        }
    }
}

Upvotes: 1

Related Questions