/**
  * Persists an ioMenuItem tree to the database.
  *
  * Typically, you'll persist your entire menu tree. This will save the root
  * menu item as a root in Doctrine's nested set with the whole tree under it:
  *
  * $menu = new ioMenuItem('root');
  * $menu->addChild('Home', '@homepage');
  * Doctrine_Core::getTable('ioDoctrineMenuItem')->persist($menu);
  *
  * You can also persist part of a tree or persist a full menu under an
  * existing Doctrine nested set node:
  *
  * $menu->addChild('Links');
  * $menu['Links']->addChild('Sympal', 'http://www.sympalphp.org');
  * $tbl = Doctrine_Core::getTable('ioDoctrineMenuItem');
  * $node = $tbl->findOneByName('some name'); // find an existing node
  * // save the Links submenu under the above node
  * $tbl->persist($menu['Links'], $node);
  *
  * @param  ioMenuItem $menu
  * @param  ioDoctrineMenuItem $parentDoctrineMenu Optional parent node, else
  *                                                it will save as root
  * @return ioDoctrineMenuItem
  * @throws sfException
  */
 public function persist(ioMenuItem $menu, ioDoctrineMenuItem $parent = null)
 {
     // run a few sanity checks and create the root node
     if (!$parent) {
         // protect against people calling persist on non-root objects, which
         // would otherwise cause those items to persist as new roots
         if (!$menu->isRoot()) {
             throw new sfException('Non-root menu items as root items. Either persist the entire
       tree or pass an ioDoctrineMenuItem parent as the second argument.');
         }
         // Make sure the root has a name
         if (!$menu->getName()) {
             throw new sfException('A root object cannot be persisted without a name. Call setName()
       on the root menu item to set its name');
         }
         $root = $this->fetchRootByName($menu->getName());
         if (!$root) {
             // create a new root
             $root = new ioDoctrineMenuItem();
             $root->name = $menu->getName();
             $root->save();
             $this->getTree()->createRoot($root);
         }
         $parent = $root;
     }
     // merge in the menu data into the parent menu
     $parent->persistFromMenuArray($menu->toArray());
 }
function create_root($name, $clearData = true, $data = array())
{
    if ($clearData) {
        Doctrine_Core::getTable('ioDoctrineMenuItem')->createQuery()->delete()->execute();
    }
    $rt = new ioDoctrineMenuItem();
    $rt->name = $name;
    $rt->fromArray($data);
    $rt->save();
    Doctrine_Core::getTable('ioDoctrineMenuItem')->getTree()->createRoot($rt);
    return $rt;
}