public function doSave($con = null) { parent::doSave($con); if (!$this->getValue('parent')) { $treeObject = Doctrine_Core::getTable('Category')->getTree(); $roots = $treeObject->fetchRoots(); //If root was deleted, recreate if (count($roots) == 0) { $rootCategory = new Category(); $rootCategory->setName("Categories"); $rootCategory->setActive(true); $rootCategory->save(); $treeObject->createRoot($rootCategory); $parent = $rootCategory; } else { $parent = Doctrine::getTable('Category')->findOneById($roots[0]['id']); } if ($this->isNew()) { $this->getObject()->getNode()->insertAsFirstChildOf($parent); } else { $this->getObject()->getNode()->moveAsFirstChildOf($parent); } } elseif ($this->getValue('parent')) { $parent = Doctrine::getTable('Category')->findOneById($this->getValue('parent')); if ($this->isNew()) { $this->getObject()->getNode()->insertAsLastChildOf($parent); } else { $this->getObject()->getNode()->moveAsLastChildOf($parent); } } }
public function executeInsertCategory(sfWebRequest $request) { if ($request->hasParameter("categoryid") && $request->hasParameter("categoryname")) { $parent = Doctrine_Core::getTable('Category')->findOneById($request->getParameter("categoryid")); $category = new Category(); $category->setName($request->getParameter("categoryname")); $category->setActive(true); $category->getNode()->insertAsLastChildOf($parent); return true; } }