Magento: Fatal error: Call to a member function getModelInstance() on a non-object in app\Mage.php on line 432
Asked Answered
M

5

7

I want to call a PHP file using ajax where in that PHP i will place order by the ajax call. But it throws error while i am using app/Mage.php from that file

require_once '../../../../../../../../../../app/Mage.php';    
$customer = Mage::getModel('customer/customer');

then it says

Fatal error: Call to a member function getModelInstance() on a non-object in app\Mage.php on line 432

Can anyone please help me???

Myrick answered 22/8, 2011 at 9:22 Comment(1)
I have solved it by using $customer = new Mage_Customer_Model_Customer(); instead of using $customer = Mage::getModel('customer/customer');Myrick
P
39

Your proposed solution is not optimal. You have not initialized Magento so module XML is not loaded yet and the factory pattern does not work.

Simply use either:

Mage::init(); // 1.5+ 

or

Mage::app(); // (pretty much anything) below 1.5

before using getModel.

Panpsychist answered 22/8, 2011 at 9:42 Comment(4)
its shows me this error "Mage registry key "controller" already exists"Justle
Thanks Daniel Sloof. I struggled a lot with this error. +11111111Fistic
Thanks Daniel, that helped.Screwy
8 years later you're still helping people with this answer... +1Insulate
E
3

You should initialize the Magento Framework first:

/* Store or website code */
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';

/* Run store or run website */
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';

Mage::init($mageRunCode, $mageRunType, array());
Enceinte answered 22/8, 2011 at 9:44 Comment(1)
It seems like Mage::init($mageRunCode, $mageRunType); would be more appropriate, but +1! (But note, I'm using Mage EE 1.12. Might be different for different versions :) Thanks, solved my error.Overtire
D
1

you need to initialize magento. the safest way to initialize it is by using initializer before your actual call to the model

Mage::init();

$customer = Mage::getModel('customer/customer');

Dingbat answered 15/5, 2013 at 15:0 Comment(0)
C
1

I got the same error message. The solution was different. I forgot to give the permission on the magento folder to the Apache.

chown -R apache:apache magento
Constitutive answered 3/6, 2014 at 11:40 Comment(0)
M
0

I personally had solved it by using

$customer = new Mage_Customer_Model_Customer();

instead of using

$customer = Mage::getModel('customer/customer');

Myrick answered 22/2, 2015 at 3:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.