Search code examples
templatesmagentoexpressionengine

ExpressionEngine -> Can include Magento code in one template but not in another


We use Magento in conjunction with EE for our site. In one of my templates, I have this code to load the Magento object model to be used in the template:

include_once($_SERVER['DOCUMENT_ROOT']  . '/store/app/Mage.php');
Mage::app(); 

The template loads fine and the code works.

But if I include the exact same code in another template, I get this:

Fatal error: Uncaught exception 'Exception' with message 'Warning: include(Query.php): 
 failed to open stream: 
 No such file or directory in /srv/www/htdocs/store/lib/Varien/Autoload.php on line 93' in /srv/www/htdocs/store/app/code/core/Mage/Core/functions.php:245 

Stack trace: 
#0 /srv/www/htdocs/store/lib/Varien/Autoload.php(93): mageCoreErrorHandler(2, 'include(Query.p...', '/srv/www/htdocs...', 93, Array) 
#1 /srv/www/htdocs/store/lib/Varien/Autoload.php(93): Varien_Autoload::autoload() 
#2 [internal function]: Varien_Autoload->autoload('query') 
#3 [internal function]: spl_autoload_call('query') 
#4 /srv/www/htdocs/na_cms/expressionengine/libraries/Template.php(1089): class_exists('query') 
#5 /srv/www/htdocs/na_cms/expressionengine/libraries/Template.php(968): EE_Template->process_tags() 
#6 /srv/www/htdocs/na_cms/expressionengine/libraries/Template.php(497): EE_Template->tags() 
#7 /srv/www/htdocs/na_cms/expressionengine/libraries/Template.php(248): EE_Template->parse('?? 

These templates are NOT nested.

Any ideas why this is happening and how to fix it?


Solution

  • At some point in your code, or the EE code, you're trying to use a class named "Query". This class is undefined, so PHP attempts to use its autoloader mechanism to load the class Query.

    The code above indicates Magento's autoload tries to load the class Query. This fails (as the class isn't a Magento class). Also, it appears you're in Magento's developer mode, where all Notices and warnings are turned into Exceptions. Since the autoload fails with a warning, a fatal exception is thrown.

    So, the problem could be two things. The first is someone's trying to instantiate a non-existant Query class. If this is the case, stop doing it.

    The second is that the Query class exists, but EE's autoloader never has a chance to load it because Magento's autoload tries first. If this is the case you'll need to insert some code somewhere that jiggers the autoloaders to change their order. You could also try manually including the Query class to fix this specific instance.