Magento 1.7 - getModel in script outside web application fails

旧巷老猫 提交于 2019-12-01 13:10:32

问题


<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>

My custom grid in the backend is working and I see the grid with data in it.
As a debug test I have in my Grid.php in _prepareCollection:

    $x = Mage::getModel('starmall_shipment/shipment');
    $x->load(3);

    var_dump($x);  // WORKS and I get information

When I run this in a standalone script:

<?php
    define("MAGE_BASE_DIR", "/home/users/xxx/xxx");
    require_once MAGE_BASE_DIR . '/app/Mage.php';
    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

    $x = Mage::getModel('starmall_shipment/shipment');
        $x->load(3);

    var_dump($x);   
?>

I see this error:

No such file or directory  in /home/users/xxx/xxx/lib/Varien/Autoload.php
Failed opening 'Mage/Starmall/Shipment/Model/Shipment.php'

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


ADDED INFORMATION:

/app/etc/Starmall_Shipment.xml

<config>
<modules>
    <Starmall_Shipment>
        <active>true</active>
        <codePool>local</codePool>
    </Starmall_Shipment>
</modules>


THE REAL PROBLEM (POSSIBLY)

My Magento hosting provider puts this tag default int /app/etc/local.xml when installing Magento.

<cache>
  <backend>memcached</backend>
  <memcached>
    <servers>
      <server>
        <host>unix:///path/to/memcached.sock</host>
        <persistent>0</persistent>
        <port>0</port>
      </server>
    </servers>
  </memcached>
</cache>

If I comment this tag out then I don't need to use loadModules() in my script.
I wonder if I need to turn this back on when going into production mode?


回答1:


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();



回答2:


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?




回答3:


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.




回答4:


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




回答5:


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.




回答6:


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.




回答7:


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



来源:https://stackoverflow.com/questions/14400150/magento-1-7-getmodel-in-script-outside-web-application-fails

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