views:

250

answers:

1

Hi,

I'm importing some content into a Nested Tree model, and I'm implementing a Transaction to be sure each object has been saved; and if not, remove it from the tree. I'm using Doctrine 1.1.6.

// Start the transaction
$conn = Doctrine_Manager::connection();
try {
  $conn->beginTransaction();

  // add it as a child of the suburb
  $object->getNode()->insertAsLastChildOf($parent);
  ...
  // save related objects
  ...
  $conn->commit();

} catch(Doctrine_Exception $e) {
  $conn->rollback();
}

What's happening right now is that if there is any error in the transaction block, all the objects will be deleted. However the tree doesn't return to the original position. This means, that I'll have blank spaces in the 'lft' and 'rgt' positions. The tree would be corrupted then; and fixing a tree with thousands of results can be very expensive.

How I can do to rollback the insertAsLastChildOf(). I think this is something Doctrine should do by itself, but I hope someone can give me a hint.

thanks!

A: 

I had to do a manual rollback of the tree when the insert fails, this is the code I've used:

try {
  $conn->beginTransaction();

  // add it as a child of the suburb
  $obj->getNode()->insertAsLastChildOf($parent);
  $rgt = $obj->rgt;
  ...
  ...
}catch(Doctrine_Exception $e) {
  $conn->rollback();

  $result = Doctrine_Query::create()->update('Model p')
    ->set('p.lft = p.lft - 2')
    ->set('p.rgt = p.rgt - 2')
    ->where('p.rgt > ?', $rgt)
    ->addWhere('p.root_id = ?', $parent->root_id) 
    ->execute();
}
fesja