From: Gustavo Martin Morcuende Date: Sun, 20 May 2012 21:00:39 +0000 (+0200) Subject: Fixing errors in CompanyCategory: order and update X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=e47575c636e75d75594ff2c7e6aa272387ea5d39;p=mobi%2F.git Fixing errors in CompanyCategory: order and update We order the results from the CompanyCategory table in the right way. Fixing error while updating, the parent_category was missed. --- diff --git a/apps/companyfront/modules/category/actions/actions.class.php b/apps/companyfront/modules/category/actions/actions.class.php index 57727a5..81c2a2c 100644 --- a/apps/companyfront/modules/category/actions/actions.class.php +++ b/apps/companyfront/modules/category/actions/actions.class.php @@ -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); diff --git a/lib/form/doctrine/CompanyCategoryForm.class.php b/lib/form/doctrine/CompanyCategoryForm.class.php index 2a5ade5..c01dc95 100644 --- a/lib/form/doctrine/CompanyCategoryForm.class.php +++ b/lib/form/doctrine/CompanyCategoryForm.class.php @@ -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()); + } } } diff --git a/lib/model/doctrine/CompanyCategoryTable.class.php b/lib/model/doctrine/CompanyCategoryTable.class.php index 3a2a9a4..874b7c4 100644 --- a/lib/model/doctrine/CompanyCategoryTable.class.php +++ b/lib/model/doctrine/CompanyCategoryTable.class.php @@ -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'); } }