Magento 1.7 - getModel in script outside web application fails

大憨熊 提交于 2019-12-01 14:00:34

Have a look how Magento cron works - it works as standalone script also (cron.php) and copy-paste code to your custom module. Main things:

 require 'app/Mage.php';
 ...
 Mage::getConfig()->init()

Also it seems like modules not loaded Try to add after init config:

Mage::getConfig()->loadModules();

Why does it prepend Mage in front of the model? What can be wrong in my config?

To see, where this is happening, look at Mage_Core_Model_Config::getGroupedClassName():

    if (empty($className)) {
        if (!empty($config)) {
            $className = $config->getClassName();
        }
        if (empty($className)) {
            $className = 'mage_'.$group.'_'.$groupType;
        }
        if (!empty($class)) {
            $className .= '_'.$class;
        }
        $className = uc_words($className);
    }

This can mean one of two things:

  1. The $config node was not found (global/models/starmall_shipment)
  2. The node has no class or model child or it is empty

In your case it looks like (1), so the question remains, why is your config not loaded. You assured that the config itself is correct, so the problem must be that the module is not loaded.

Can you post your module declaration file from app/etc/modules?

It is prepending Mage because it is one of the core directories.. In my server, I see /var/www/app/code/core/Mage/ as a navigable path.

Put your standalone scripts in your mage base dir, and use the following code at the "beginning"

<?php
   require_once 'app/Mage.php';
   Mage::app('admin'); 

Hopefully this will be enough to get your magento up and running, then you can figure out why the paths aren't working the way you expect.

Check whether compilation mode is enabled in your store(System->Tools->Compilation).If yes then re-run compilation process

You should define your model under <global> tag in config.xml like

<global>    
    <models>
        <starmall_shipment>
            <class>Starmall_Shipment_Model</class>
            <resourceModel>starmall_shipment_mysql4</resourceModel>
        </starmall_shipment>

        <starmall_shipment_mysql4>
            <class>Starmall_Shipment_Model_Mysql4</class>
            <entities>
                <shipment>
                    <table>starmall_shipment</table>
                </shipment>             
            </entities>
        </starmall_shipment_mysql4>
    </models>
</global>   

because you are accessing if from outside the module.

The problem is indeed in app/etc/local.xml, but it shouldn't be solved by calling Mage::app()->loadModules().

The real issue is that someone (most likely for debugging purposes) disabled local modules.

Just change

<disable_local_modules>true</disable_local_modules>

to

<disable_local_modules>false</disable_local_modules>

in app/etc/local.xml.

CarComp is write, after adding Mage::app('admin');
the Mage::getConfig()->init() working like a charm.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!