Author: david
Date: Tue Feb 22 15:20:10 2011
New Revision: 8993

Log:
Build nested set for actor table

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

Modified: trunk/lib/task/propelBuildNestedSetTask.class.php
==============================================================================
--- trunk/lib/task/propelBuildNestedSetTask.class.php   Tue Feb 22 14:57:23 
2011        (r8992)
+++ trunk/lib/task/propelBuildNestedSetTask.class.php   Tue Feb 22 15:20:10 
2011        (r8993)
@@ -55,62 +55,70 @@
    */
   public function execute($arguments = array(), $options = array())
   {
-    $this->logSection('propel', 'Build nested set values...');
-
-    $sql = 'SELECT id, parent_id';
-    $sql .= ' FROM '.QubitInformationObject::TABLE_NAME;
-    $sql .= ' ORDER BY parent_id ASC, lft ASC';
-
     $databaseManager = new sfDatabaseManager($this->configuration);
     $conn = $databaseManager->getDatabase('propel')->getConnection();
 
-    $tree = $children = array();
+    $tables = array(
+      'information_object' => 'QubitInformationObject',
+      'actor' => 'QubitActor'
+    );
 
-    foreach ($conn->query($sql, PDO::FETCH_ASSOC) as $item)
+    foreach ($tables as $table => $classname)
     {
-      // Add root node to tree
-      if (QubitInformationObject::ROOT_ID == $item['id'])
-      {
-        array_push($tree, array(
-          'id' => $item['id'],
-          'lft' => 1,
-          'rgt' => null,
-          'children' => array())
-        );
-      }
-      else
+      $this->logSection('propel', 'Build nested set for '.$table.'...');
+
+      $sql = 'SELECT id, parent_id';
+      $sql .= ' FROM '.constant($classname.'::TABLE_NAME');
+      $sql .= ' ORDER BY parent_id ASC, lft ASC';
+
+      $tree = $children = array();
+
+      foreach ($conn->query($sql, PDO::FETCH_ASSOC) as $item)
       {
-        // build hash of child rows keyed on parent_id
-        if (isset($children[$item['parent_id']]))
+        // Add root node to tree
+        if (constant($classname.'::ROOT_ID') == $item['id'])
         {
-          array_push($children[$item['parent_id']], $item['id']);
+          array_push($tree, array(
+            'id' => $item['id'],
+            'lft' => 1,
+            'rgt' => null,
+            'children' => array())
+          );
         }
         else
         {
-          $children[$item['parent_id']] = array($item['id']);
+          // build hash of child rows keyed on parent_id
+          if (isset($children[$item['parent_id']]))
+          {
+            array_push($children[$item['parent_id']], $item['id']);
+          }
+          else
+          {
+            $children[$item['parent_id']] = array($item['id']);
+          }
         }
       }
-    }
 
-    // Recursively add child nodes
-    self::addChildren($tree[0], $children, 1);
+      // Recursively add child nodes
+      self::addChildren($tree[0], $children, 1);
 
-    // Crawl tree and build sql statement to update nested set columns
-    $sql = self::getNsUpdateSql($tree[0]);
+      // Crawl tree and build sql statement to update nested set columns
+      $sql = self::getNsUpdateSql($tree[0], $classname);
 
-    // Update database
-    $conn->beginTransaction();
-    try
-    {
-      $conn->exec($sql);
-    }
-    catch (PDOException $e)
-    {
-      $conn->rollback();
-      throw sfException($e);
-    }
+      // Update database
+      $conn->beginTransaction();
+      try
+      {
+        $conn->exec($sql);
+      }
+      catch (PDOException $e)
+      {
+        $conn->rollback();
+        throw sfException($e);
+      }
 
-    $conn->commit();
+      $conn->commit();
+    } // endforeach
 
     $this->logSection('propel', 'Done!');
   }
@@ -139,9 +147,9 @@
     return $width;
   }
 
-  protected function getNsUpdateSql($node)
+  protected function getNsUpdateSql($node, $classname)
   {
-    $sql = 'UPDATE '.QubitInformationObject::TABLE_NAME;
+    $sql = 'UPDATE '.constant($classname.'::TABLE_NAME');
     $sql .= ' SET lft = '.$node['lft'];
     $sql .= ', rgt = '.$node['rgt'];
     $sql .= ' WHERE id = '.$node['id'].";\n";
@@ -150,7 +158,7 @@
     {
       foreach ($node['children'] as $child)
       {
-        $sql .= self::getNsUpdateSql($child);
+        $sql .= self::getNsUpdateSql($child, $classname);
       }
     }
 

-- 
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