title: Magento: How to get the root category id for a specific store in an upgrade script?
body: <p>How do I get the root category in a Magento 1.9 update script?</p>
  
  <p><a href="https://stackoverflow.com/questions/8700976/find-root-category-of-store">Find root category of store</a> proposes a solution, but I get an error message after running the script:</p>
  
  <pre><code>Mage registry key "controller" already exists
  </code></pre>
  
  <p>... as well as a trace:</p>
  
  <pre><code>#0 /var/www/instances/global/app/Mage.php(223): Mage::throwException('Mage registry k...')
  #1 /var/www/instances/global/app/code/core/Mage/Core/Model/App.php(762): Mage::register('controller', Object(Mage_Core_Controller_Varien_Front))
  #2 /var/www/instances/global/app/code/core/Mage/Core/Model/App.php(1113): Mage_Core_Model_App-&amp;gt;_initFrontController()
  #3 /var/www/instances/global/app/code/core/Mage/Core/Controller/Varien/Front.php(344): Mage_Core_Model_App-&amp;gt;getFrontController()
  #4 /var/www/instances/global/app/code/core/Mage/Core/Controller/Varien/Front.php(161): Mage_Core_Controller_Varien_Front-&amp;gt;_checkBaseUrl(Object(Mage_Core_Controller_Request_Http))
  #5 /var/www/instances/global/app/code/core/Mage/Core/Model/App.php(365): Mage_Core_Controller_Varien_Front-&amp;gt;dispatch()
  #6 /var/www/instances/global/app/Mage.php(684): Mage_Core_Model_App-&amp;gt;run(Array)
  #7 /var/www/instances/global/index.php(118): Mage::run('', 'store')
  #8 {main}
  </code></pre>
  
  <p>My script calls the Mage::init first, then tries to read the root category name of the store.</p>
  
  <pre><code>Mage::init();
  Mage::app()-&gt;getStore(3)-&gt;getRootCategoryId();
  </code></pre>
  
  <p>The error message seems to be quite common; <a href="https://stackoverflow.com/questions/9737069/mage-registry-key-controller-already-exists">Mage registry key &quot;controller&quot; already exists</a> indicates Mage::run might have been run twice.</p>
  
  <p>I therefore assume that Mage::init also should not be called twice, and is maybe invoked again after my update script is invoked - and this could cause my error message. All assumptions.</p>
  
  <p>What could i do? Omit Mage::init()? But then, I do not get the root id. Destroy the object(s) created by Mage::init()? How?</p>