Author: david
Date: Sun Feb 13 14:16:38 2011
New Revision: 8967

Log:
Update nested set columns in database.

Modified:
   trunk/lib/task/nestedsetBuildTask.class.php

Modified: trunk/lib/task/nestedsetBuildTask.class.php
==============================================================================
--- trunk/lib/task/nestedsetBuildTask.class.php Sun Feb 13 00:03:12 2011        
(r8966)
+++ trunk/lib/task/nestedsetBuildTask.class.php Sun Feb 13 14:16:38 2011        
(r8967)
@@ -59,7 +59,8 @@
 
     $sql = 'SELECT id, parent_id';
     $sql .= ' FROM '.QubitInformationObject::TABLE_NAME;
-    $sql .= ' ORDER BY parent_id ASC, lft ASC';
+    //$sql .= ' ORDER BY parent_id ASC, lft ASC';
+    $sql .= ' ORDER BY id ASC';
 
     $databaseManager = new sfDatabaseManager($this->configuration);
 
@@ -98,7 +99,24 @@
     // Recursively add child nodes
     self::addChildren($tree[0], $children, 1);
 
-    print_r($tree);
+    // Crawl tree and build sql statement to update nested set columns
+    $sql = self::getNsUpdateSql($tree[0]);
+
+    // Update database
+    $conn->beginTransaction();
+    try
+    {
+      $conn->exec($sql);
+    }
+    catch (PDOException $e)
+    {
+      $conn->rollback();
+      throw sfException($e);
+    }
+
+    $conn->commit();
+
+    $this->logSection('nestedset', 'Done!');
   }
 
   protected function addChildren(&$node, $children, $lft)
@@ -124,4 +142,22 @@
 
     return $width;
   }
+
+  protected function getNsUpdateSql($node)
+  {
+    $sql = 'UPDATE '.QubitInformationObject::TABLE_NAME;
+    $sql .= ' SET lft = '.$node['lft'];
+    $sql .= ', rgt = '.$node['rgt'];
+    $sql .= ' WHERE id = '.$node['id'].";\n";
+
+    if (0 < count($node['children']))
+    {
+      foreach ($node['children'] as $child)
+      {
+        $sql .= self::getNsUpdateSql($child);
+      }
+    }
+
+    return $sql;
+  }
 }

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to