Author: jablko
Date: 2008-12-12 15:29:21 -0800 (Fri, 12 Dec 2008)
New Revision: 1678

Modified:
   trunk/qubit/lib/model/om/BaseDigitalObject.php
   trunk/qubit/lib/model/om/BaseInformationObject.php
   trunk/qubit/lib/model/om/BaseNote.php
   trunk/qubit/lib/model/om/BasePhysicalObject.php
   trunk/qubit/lib/model/om/BaseTerm.php
   trunk/qubit/lib/propel/builder/QubitObjectBuilder.php
Log:
Fix nested set. Update nested set before insert() or update() so that left and 
right values get saved. Compensate for deleteFromNestedSet() when updating left 
and right values.


Modified: trunk/qubit/lib/model/om/BaseDigitalObject.php
===================================================================
--- trunk/qubit/lib/model/om/BaseDigitalObject.php      2008-12-12 20:50:23 UTC 
(rev 1677)
+++ trunk/qubit/lib/model/om/BaseDigitalObject.php      2008-12-12 23:29:21 UTC 
(rev 1678)
@@ -109,10 +109,10 @@
   {
     $affectedRows = 0;
 
+    $this->updateNestedSet($connection);
+
     $affectedRows += parent::insert($connection);
 
-    $this->updateNestedSet($connection);
-
     return $affectedRows;
   }
 
@@ -120,10 +120,10 @@
   {
     $affectedRows = 0;
 
+    $this->updateNestedSet($connection);
+
     $affectedRows += parent::update($connection);
 
-    $this->updateNestedSet($connection);
-
     return $affectedRows;
   }
 
@@ -318,6 +318,15 @@
       $connection = 
QubitTransactionFilter::getConnection(QubitDigitalObject::DATABASE_NAME);
     }
 
+    if (!isset($this->lft) || !isset($this->rgt))
+    {
+      $delta = 2;
+    }
+    else
+    {
+      $delta = $this->rgt - $this->lft + 1;
+    }
+
     if (null === $parent = $this->offsetGet('Parent', array('connection' => 
$connection)))
     {
       $statement = $connection->prepare('
@@ -341,20 +350,11 @@
     {
       $parent->refresh(array('connection' => $connection));
 
-      if (!isset($this->lft) || !isset($this->rgt))
+      if (isset($this->lft) && isset($this->rgt) && $this->lft <= $parent->lft 
&& $this->rgt >= $parent->rgt)
       {
-        $delta = 2;
+        throw new PropelException('An object cannot be a descendant of 
itself.');
       }
-      else
-      {
-        if ($this->lft <= $parent->lft && $this->rgt >= $parent->rgt)
-        {
-          throw new PropelException('An object cannot be a descendant of 
itself.');
-        }
 
-        $delta = $this->rgt - $this->lft + 1;
-      }
-
       $statement = $connection->prepare('
         UPDATE '.QubitDigitalObject::TABLE_NAME.'
         SET '.QubitDigitalObject::LFT.' = '.QubitDigitalObject::LFT.' + ?
@@ -393,6 +393,12 @@
 
     $this->deleteFromNestedSet($connection);
 
+    if ($shift > 0)
+    {
+      $this->lft -= $delta;
+      $this->rgt -= $delta;
+    }
+
     $this->lft += $shift;
     $this->rgt += $shift;
 

Modified: trunk/qubit/lib/model/om/BaseInformationObject.php
===================================================================
--- trunk/qubit/lib/model/om/BaseInformationObject.php  2008-12-12 20:50:23 UTC 
(rev 1677)
+++ trunk/qubit/lib/model/om/BaseInformationObject.php  2008-12-12 23:29:21 UTC 
(rev 1678)
@@ -184,10 +184,10 @@
   {
     $affectedRows = 0;
 
+    $this->updateNestedSet($connection);
+
     $affectedRows += parent::insert($connection);
 
-    $this->updateNestedSet($connection);
-
     return $affectedRows;
   }
 
@@ -195,10 +195,10 @@
   {
     $affectedRows = 0;
 
+    $this->updateNestedSet($connection);
+
     $affectedRows += parent::update($connection);
 
-    $this->updateNestedSet($connection);
-
     return $affectedRows;
   }
 
@@ -503,6 +503,15 @@
       $connection = 
QubitTransactionFilter::getConnection(QubitInformationObject::DATABASE_NAME);
     }
 
+    if (!isset($this->lft) || !isset($this->rgt))
+    {
+      $delta = 2;
+    }
+    else
+    {
+      $delta = $this->rgt - $this->lft + 1;
+    }
+
     if (null === $parent = $this->offsetGet('Parent', array('connection' => 
$connection)))
     {
       $statement = $connection->prepare('
@@ -526,20 +535,11 @@
     {
       $parent->refresh(array('connection' => $connection));
 
-      if (!isset($this->lft) || !isset($this->rgt))
+      if (isset($this->lft) && isset($this->rgt) && $this->lft <= $parent->lft 
&& $this->rgt >= $parent->rgt)
       {
-        $delta = 2;
+        throw new PropelException('An object cannot be a descendant of 
itself.');
       }
-      else
-      {
-        if ($this->lft <= $parent->lft && $this->rgt >= $parent->rgt)
-        {
-          throw new PropelException('An object cannot be a descendant of 
itself.');
-        }
 
-        $delta = $this->rgt - $this->lft + 1;
-      }
-
       $statement = $connection->prepare('
         UPDATE '.QubitInformationObject::TABLE_NAME.'
         SET '.QubitInformationObject::LFT.' = '.QubitInformationObject::LFT.' 
+ ?
@@ -578,6 +578,12 @@
 
     $this->deleteFromNestedSet($connection);
 
+    if ($shift > 0)
+    {
+      $this->lft -= $delta;
+      $this->rgt -= $delta;
+    }
+
     $this->lft += $shift;
     $this->rgt += $shift;
 

Modified: trunk/qubit/lib/model/om/BaseNote.php
===================================================================
--- trunk/qubit/lib/model/om/BaseNote.php       2008-12-12 20:50:23 UTC (rev 
1677)
+++ trunk/qubit/lib/model/om/BaseNote.php       2008-12-12 23:29:21 UTC (rev 
1678)
@@ -686,6 +686,15 @@
       $connection = 
QubitTransactionFilter::getConnection(QubitNote::DATABASE_NAME);
     }
 
+    if (!isset($this->lft) || !isset($this->rgt))
+    {
+      $delta = 2;
+    }
+    else
+    {
+      $delta = $this->rgt - $this->lft + 1;
+    }
+
     if (null === $parent = $this->offsetGet('Parent', array('connection' => 
$connection)))
     {
       $statement = $connection->prepare('
@@ -709,20 +718,11 @@
     {
       $parent->refresh(array('connection' => $connection));
 
-      if (!isset($this->lft) || !isset($this->rgt))
+      if (isset($this->lft) && isset($this->rgt) && $this->lft <= $parent->lft 
&& $this->rgt >= $parent->rgt)
       {
-        $delta = 2;
+        throw new PropelException('An object cannot be a descendant of 
itself.');
       }
-      else
-      {
-        if ($this->lft <= $parent->lft && $this->rgt >= $parent->rgt)
-        {
-          throw new PropelException('An object cannot be a descendant of 
itself.');
-        }
 
-        $delta = $this->rgt - $this->lft + 1;
-      }
-
       $statement = $connection->prepare('
         UPDATE '.QubitNote::TABLE_NAME.'
         SET '.QubitNote::LFT.' = '.QubitNote::LFT.' + ?
@@ -761,6 +761,12 @@
 
     $this->deleteFromNestedSet($connection);
 
+    if ($shift > 0)
+    {
+      $this->lft -= $delta;
+      $this->rgt -= $delta;
+    }
+
     $this->lft += $shift;
     $this->rgt += $shift;
 

Modified: trunk/qubit/lib/model/om/BasePhysicalObject.php
===================================================================
--- trunk/qubit/lib/model/om/BasePhysicalObject.php     2008-12-12 20:50:23 UTC 
(rev 1677)
+++ trunk/qubit/lib/model/om/BasePhysicalObject.php     2008-12-12 23:29:21 UTC 
(rev 1678)
@@ -170,10 +170,10 @@
   {
     $affectedRows = 0;
 
+    $this->updateNestedSet($connection);
+
     $affectedRows += parent::insert($connection);
 
-    $this->updateNestedSet($connection);
-
     return $affectedRows;
   }
 
@@ -181,10 +181,10 @@
   {
     $affectedRows = 0;
 
+    $this->updateNestedSet($connection);
+
     $affectedRows += parent::update($connection);
 
-    $this->updateNestedSet($connection);
-
     return $affectedRows;
   }
 
@@ -383,6 +383,15 @@
       $connection = 
QubitTransactionFilter::getConnection(QubitPhysicalObject::DATABASE_NAME);
     }
 
+    if (!isset($this->lft) || !isset($this->rgt))
+    {
+      $delta = 2;
+    }
+    else
+    {
+      $delta = $this->rgt - $this->lft + 1;
+    }
+
     if (null === $parent = $this->offsetGet('Parent', array('connection' => 
$connection)))
     {
       $statement = $connection->prepare('
@@ -406,20 +415,11 @@
     {
       $parent->refresh(array('connection' => $connection));
 
-      if (!isset($this->lft) || !isset($this->rgt))
+      if (isset($this->lft) && isset($this->rgt) && $this->lft <= $parent->lft 
&& $this->rgt >= $parent->rgt)
       {
-        $delta = 2;
+        throw new PropelException('An object cannot be a descendant of 
itself.');
       }
-      else
-      {
-        if ($this->lft <= $parent->lft && $this->rgt >= $parent->rgt)
-        {
-          throw new PropelException('An object cannot be a descendant of 
itself.');
-        }
 
-        $delta = $this->rgt - $this->lft + 1;
-      }
-
       $statement = $connection->prepare('
         UPDATE '.QubitPhysicalObject::TABLE_NAME.'
         SET '.QubitPhysicalObject::LFT.' = '.QubitPhysicalObject::LFT.' + ?
@@ -458,6 +458,12 @@
 
     $this->deleteFromNestedSet($connection);
 
+    if ($shift > 0)
+    {
+      $this->lft -= $delta;
+      $this->rgt -= $delta;
+    }
+
     $this->lft += $shift;
     $this->rgt += $shift;
 

Modified: trunk/qubit/lib/model/om/BaseTerm.php
===================================================================
--- trunk/qubit/lib/model/om/BaseTerm.php       2008-12-12 20:50:23 UTC (rev 
1677)
+++ trunk/qubit/lib/model/om/BaseTerm.php       2008-12-12 23:29:21 UTC (rev 
1678)
@@ -172,10 +172,10 @@
   {
     $affectedRows = 0;
 
+    $this->updateNestedSet($connection);
+
     $affectedRows += parent::insert($connection);
 
-    $this->updateNestedSet($connection);
-
     return $affectedRows;
   }
 
@@ -183,10 +183,10 @@
   {
     $affectedRows = 0;
 
+    $this->updateNestedSet($connection);
+
     $affectedRows += parent::update($connection);
 
-    $this->updateNestedSet($connection);
-
     return $affectedRows;
   }
 
@@ -1594,6 +1594,15 @@
       $connection = 
QubitTransactionFilter::getConnection(QubitTerm::DATABASE_NAME);
     }
 
+    if (!isset($this->lft) || !isset($this->rgt))
+    {
+      $delta = 2;
+    }
+    else
+    {
+      $delta = $this->rgt - $this->lft + 1;
+    }
+
     if (null === $parent = $this->offsetGet('Parent', array('connection' => 
$connection)))
     {
       $statement = $connection->prepare('
@@ -1617,20 +1626,11 @@
     {
       $parent->refresh(array('connection' => $connection));
 
-      if (!isset($this->lft) || !isset($this->rgt))
+      if (isset($this->lft) && isset($this->rgt) && $this->lft <= $parent->lft 
&& $this->rgt >= $parent->rgt)
       {
-        $delta = 2;
+        throw new PropelException('An object cannot be a descendant of 
itself.');
       }
-      else
-      {
-        if ($this->lft <= $parent->lft && $this->rgt >= $parent->rgt)
-        {
-          throw new PropelException('An object cannot be a descendant of 
itself.');
-        }
 
-        $delta = $this->rgt - $this->lft + 1;
-      }
-
       $statement = $connection->prepare('
         UPDATE '.QubitTerm::TABLE_NAME.'
         SET '.QubitTerm::LFT.' = '.QubitTerm::LFT.' + ?
@@ -1669,6 +1669,12 @@
 
     $this->deleteFromNestedSet($connection);
 
+    if ($shift > 0)
+    {
+      $this->lft -= $delta;
+      $this->rgt -= $delta;
+    }
+
     $this->lft += $shift;
     $this->rgt += $shift;
 

Modified: trunk/qubit/lib/propel/builder/QubitObjectBuilder.php
===================================================================
--- trunk/qubit/lib/propel/builder/QubitObjectBuilder.php       2008-12-12 
20:50:23 UTC (rev 1677)
+++ trunk/qubit/lib/propel/builder/QubitObjectBuilder.php       2008-12-12 
23:29:21 UTC (rev 1678)
@@ -1180,20 +1180,20 @@
 
 EOF;
 
-    if (isset($this->inheritanceFk))
+    if (isset($this->nestedSetLeftColumn) && 
isset($this->nestedSetRightColumn))
     {
       $script .= <<<EOF
 
-    \$affectedRows += parent::insert(\$connection);
+    \$this->updateNestedSet(\$connection);
 
 EOF;
     }
 
-    if (isset($this->nestedSetLeftColumn) && 
isset($this->nestedSetRightColumn))
+    if (isset($this->inheritanceFk))
     {
       $script .= <<<EOF
 
-    \$this->updateNestedSet(\$connection);
+    \$affectedRows += parent::insert(\$connection);
 
 EOF;
     }
@@ -1273,21 +1273,21 @@
 
 EOF;
 
-    if (isset($this->inheritanceFk))
+    // TODO: Only update nested set if the self foreign key has changed
+    if (isset($this->nestedSetLeftColumn) && 
isset($this->nestedSetRightColumn))
     {
       $script .= <<<EOF
 
-    \$affectedRows += parent::update(\$connection);
+    \$this->updateNestedSet(\$connection);
 
 EOF;
     }
 
-    // TODO: Only update nested set if the self foreign key has changed
-    if (isset($this->nestedSetLeftColumn) && 
isset($this->nestedSetRightColumn))
+    if (isset($this->inheritanceFk))
     {
       $script .= <<<EOF
 
-    \$this->updateNestedSet(\$connection);
+    \$affectedRows += parent::update(\$connection);
 
 EOF;
     }
@@ -1758,6 +1758,15 @@
       \$connection = 
QubitTransactionFilter::getConnection({$this->getPeerClassName()}::DATABASE_NAME);
     }
 
+    if (!isset(\$this->{$this->getColumnVarName($this->nestedSetLeftColumn)}) 
|| !isset(\$this->{$this->getColumnVarName($this->nestedSetRightColumn)}))
+    {
+      \$delta = 2;
+    }
+    else
+    {
+      \$delta = \$this->{$this->getColumnVarName($this->nestedSetRightColumn)} 
- \$this->{$this->getColumnVarName($this->nestedSetLeftColumn)} + 1;
+    }
+
     if (null === \${$this->getFkVarName($this->selfFk)} = 
\$this->offsetGet('{$this->getFkPhpName($this->selfFk)}', array('connection' => 
\$connection)))
     {
       \$statement = \$connection->prepare('
@@ -1781,20 +1790,11 @@
     {
       \$parent->refresh(array('connection' => \$connection));
 
-      if 
(!isset(\$this->{$this->getColumnVarName($this->nestedSetLeftColumn)}) || 
!isset(\$this->{$this->getColumnVarName($this->nestedSetRightColumn)}))
+      if (isset(\$this->{$this->getColumnVarName($this->nestedSetLeftColumn)}) 
&& isset(\$this->{$this->getColumnVarName($this->nestedSetRightColumn)}) && 
\$this->{$this->getColumnVarName($this->nestedSetLeftColumn)} <= 
\${$this->getFkVarName($this->selfFk)}->{$this->getColumnVarName($this->nestedSetLeftColumn)}
 && \$this->{$this->getColumnVarName($this->nestedSetRightColumn)} >= 
\${$this->getFkVarName($this->selfFk)}->{$this->getColumnVarName($this->nestedSetRightColumn)})
       {
-        \$delta = 2;
+        throw new PropelException('An object cannot be a descendant of 
itself.');
       }
-      else
-      {
-        if (\$this->{$this->getColumnVarName($this->nestedSetLeftColumn)} <= 
\${$this->getFkVarName($this->selfFk)}->{$this->getColumnVarName($this->nestedSetLeftColumn)}
 && \$this->{$this->getColumnVarName($this->nestedSetRightColumn)} >= 
\${$this->getFkVarName($this->selfFk)}->{$this->getColumnVarName($this->nestedSetRightColumn)})
-        {
-          throw new PropelException('An object cannot be a descendant of 
itself.');
-        }
 
-        \$delta = 
\$this->{$this->getColumnVarName($this->nestedSetRightColumn)} - 
\$this->{$this->getColumnVarName($this->nestedSetLeftColumn)} + 1;
-      }
-
       \$statement = \$connection->prepare('
         UPDATE '.{$this->getPeerClassName()}::TABLE_NAME.'
         SET '.{$this->getColumnConstant($this->nestedSetLeftColumn)}.' = 
'.{$this->getColumnConstant($this->nestedSetLeftColumn)}.' + ?
@@ -1833,6 +1833,12 @@
 
     \$this->deleteFromNestedSet(\$connection);
 
+    if (\$shift > 0)
+    {
+      \$this->{$this->getColumnVarName($this->nestedSetLeftColumn)} -= \$delta;
+      \$this->{$this->getColumnVarName($this->nestedSetRightColumn)} -= 
\$delta;
+    }
+
     \$this->{$this->getColumnVarName($this->nestedSetLeftColumn)} += \$shift;
     \$this->{$this->getColumnVarName($this->nestedSetRightColumn)} += \$shift;
 


--~--~---------~--~----~------------~-------~--~----~
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.ca/group/qubit-commits?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to