Fixing errors in CompanyCategory: order and update
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 20 May 2012 21:00:39 +0000 (23:00 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 20 May 2012 21:00:39 +0000 (23:00 +0200)
We order the results from the CompanyCategory table in the right way.
Fixing error while updating, the parent_category was missed.

apps/companyfront/modules/category/actions/actions.class.php
lib/form/doctrine/CompanyCategoryForm.class.php
lib/model/doctrine/CompanyCategoryTable.class.php

index 57727a5..81c2a2c 100644 (file)
@@ -95,7 +95,7 @@ class categoryActions extends sfActions
 
     $this->forward404Unless($companyOfficeId == $companyUserId, sprintf('Category does not exist (%s).', $request->getParameter('id')));
 
-    $this->form = new CompanyCategoryForm($company_category);
+    $this->form = new CompanyCategoryForm($company_category, array('company_user_id' => $companyUserId));
 
     $this->processForm($request, $this->form);
 
index 2a5ade5..c01dc95 100644 (file)
@@ -25,10 +25,6 @@ class CompanyCategoryForm extends BaseCompanyCategoryForm
                                                                                     'required' => true,
                                                                                     'query'    => $companyCategs));
 
-    if (!$this->getObject()->isNew())
-    {
-        $this->setDefault('parent_category', $this->getObject()->getNode()->getParent()->getId());
-    }
 
     $this->widgetSchema->setLabels(array('parent_category'  => 'Parent Company Category'));
     $this->widgetSchema->setLabels(array('general_categ_id' => 'General Category'));
@@ -143,6 +139,30 @@ class CompanyCategoryForm extends BaseCompanyCategoryForm
 
     $companyCateg = CompanyCategoryTable::getInstance()->findOneById($this->values['parent_category']);
     //Second one, right here
-    $this->getObject()->getNode()->insertAsFirstChildOf($companyCateg);
+    //First of all, we have to check if this node already has a parent
+    if ($this->getObject()->getNode()->getParent() != null)
+    {
+        //We have to move the node
+        $this->getObject()->getNode()->moveAsFirstChildOf($companyCateg);
+    }
+    else
+    {
+        //We have to insert the node
+        $this->getObject()->getNode()->insertAsFirstChildOf($companyCateg);
+    }
+  }
+
+ /**
+  * Overriding updateDefaultsFromObject method from lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/form/sfFormDoctrine.class.php
+  *
+  */
+  protected function updateDefaultsFromObject()
+  {
+    parent::updateDefaultsFromObject();
+
+    if (!$this->getObject()->isNew())
+    {
+        $this->setDefault('parent_category', $this->getObject()->getNode()->getParent()->getId());
+    }
   }
 }
index 3a2a9a4..874b7c4 100644 (file)
@@ -25,8 +25,8 @@ class CompanyCategoryTable extends Doctrine_Table
     */
     public function getCompanyCategoriesByCompanyIdQuery($companyId)
     {
-        return $this->createQuery('cg')->where('cg.company_id = ?', $companyId)
-                                       ->orWhere('cg.id = ?', '1')
-                                       ->orderBy('cg.id');
+        return $this->createQuery('cc')->where('cc.company_id = ?', $companyId)
+                                       ->orWhere('cc.id = ?', '1')
+                                       ->orderBy('cc.lft');
     }
 }