Magento – Failed to open stream

getmodelmodelmodule

I am just learning Magento from the book Magento for PHP Devs but when I tried to recreate this code I got an error: PHP warning: include(Mage/Mdg/Giftregistry/Model/Entity.php): failed to open stream: No such file or directory in /var/www/html/learn/lib/Varien/Autoload.php on line 94

Due I have no experience I require some help. I have flushed Cache several times and still don't get it right. This is some data.

The error happens when I try to create a new model:
$registry = Mage::getModel('mdg_giftregistry/entity');

ERROR: PHP warning: include(Mage/Mdg/Giftregistry/Model/Entity.php): failed to open stream: No such file or directory in /var/www/html/learn/lib/Varien/Autoload.php on line 94

Im using magerun netz98. Maybe this has something to do?

My files:

app/etc/modules/Mdg_Giftregistry.xml

<?xml version="1.0"?>
<config>
<modules>
    <Mdg_Giftregistry>
        <active>true</active>
        <codePool>local</codePool>
    </Mdg_Giftregistry>
</modules>

app/code/local/Mdg/Giftregistry/etc/config.xml:

    <?xml version="1.0"?>
<config>
    <modules>
        <Mdg_Giftregistry>
            <version>0.2.0</version>
        </Mdg_Giftregistry>
    </modules>
    <global>
        <models>
            <mdg_giftregistry>
                <class>Mdg_Giftregistry_Model</class>
            </mdg_giftregistry>
        </models>
        <blocks>
            <mdg_giftregistry>
                <class>Mdg_Giftregistry_Block</class>
            </mdg_giftregistry>
        </blocks>
        <helpers>
            <mdg_giftregistry>
                <class>Mdg_Giftregistry_Helper</class>
            </mdg_giftregistry>
        </helpers>
        <resources>
            <mdg_giftregistry_setup>
                <setup>
                    <module>Mdg_Giftregistry</module>
                </setup>
            </mdg_giftregistry_setup>
        </resources>
    </global>
</config>

app/code/local/Mdg/Giftregistry/Model/Entity.php :

    <?php
    class Mdg_Giftregistry_Model_Entity extends Mage_Core_Model_Abstract {
      public function __construct() {
      $this->_init('mdg_giftregistry/entity');
      parent::_construct();
     }
    }

Any suggestion?

Best Answer

There are a couple of different ways this can happen. I got stuck at the same place while working on the book.

If you take a look at the Alan Storm tutorials on Magento Development there is a part in the models section where he directs you to get this very same error. After creating the controller page and setting up $blogpost = Mage::getModel('weblog/blogpost'); in the controller, you refresh the page and error out. He states, "Magento is trying to __autoload include this Model, but can't find the file". In that tutorial, you haven't made the Blogpost.php file yet, so there is no file to find.

What this means for us can be a few different things. Misspeling would be the first thing to check. For me, I had misspelled Mdg_Giftregistry.xml as Mgd_Giftregistry.xml. It's really easy to do, and it's a tricky to track down because the module will load in the backend of Magento (System -> Config -> Advanced -> Advanced) but all your other files will not be seen, and therefor will never find the correct path to your model.

One de-bug clue that you have in the code is the very sneaky part of the warning: ..include(Mage/Mdg/..

You can see that Magento is trying to load Mage/Mdg/Giftregistry/Model/Entity.php and not Mdg/Giftregistry/Model/Entity.php. This means that Magento went looking in the spot it thought you had the file by taking apart the call Mage::getModel('mdg_giftregistry/entity'); into the URI Mdg/Giftregistry/Model/Entity.php and when it didn't find the file there, it looked into the Mage folder to see if it could find the model there. When that didn't happen it errors out.

Another issue (while less likely) could be file permissions. If Magento can't read the file, this is the error it would kick. Check that you have correct ownership and file permissions are correct.

The last possibility is an error with the dev:console itself. This is easy to check though, if the first line of code the book instructs you to use $customer = Mage::getModel('customer/customer')->load(1); works, then the console is fine and the issue is in the code.

Related Topic