Cannot retrieve entity config: modulename/tablename

Error

You may encounter a Cannot retrieve entity config: modulename/tablename error in Magento when adding a setup script.

Solution

There are two main causes of this error. The first is to ensure that when you define your lt;resourceModel> within your module’s config.xml file, for example:

<models>
    <modulename>
        <class>Vendor_Modulename_Model</class>
        <resourceModel>vendor_modulename_resource</resourceModel>
    </modulename>
</models>

Ensure that the <vendor_modulename_resource> is defined outside the pair of <modulename> nodes, but within the pair of <models> nodes. The code below shows how this should look.

<models>
    <modulename>
        <class>Vendor_Modulename_Model</class>
        <resourceModel>vendor_modulename_resource</resourceModel>
        <vendor_modulename_resource>
            ....
        </vendor_modulename_resource>
    </modulename>
</models>

The other cause of this error can be due to a misconfiguration within the pair of <vendor_modulename_resource> nodes.

Within the setup scripts, when using the line $installer->getTable('modulename/tablename'), ensure that the part before the / within getTable() matches the node name of that within the <models> node in config.xml.

<models>
    <modulename> <!-- This node -->
        ....
    </modulename>
</models>

The part after the / should match the name of the node given within the <entities> node used to define your table name.

<models>
    <modulename>
        <class>Vendor_Modulename_Model</class>
        <resourceModel>vendor_modulename_resource</resourceModel>
        <vendor_modulename_resource>
            <class>Vendor_Modulename_Model_Resource</class>
            <entities>
                <tablename> <!-- This node -->
                    <table>your_table_name</table>
                </tablename>
            </entities>
        </vendor_modulename_resource>
    <modulename>
</models>

Ensuring that these two configurations are correct will eliminate the risk of the Cannot retrieve entity config error.

Note: This article is based on Magento Open Source version 1.9.