开发者

Fatal error on /app/code/core/Mage/Core/Model/Resource/Resource.php in Magento

Just upgraded a Magento install from 1.5 to 1.6, and left with this error:

Fatal error: Call to a member function insert() on a non-object in /hsphere/local/home/t21004/XXXXXXXXXXXXX.com/app/code/core/Mage/Core/Model/Resource/Resource.php on line 133

The contents of line 133 are as below:

 return $this->_getWriteAdapter()->insert($this->getMainTable(), $dbModuleInfo);

config.xml :

    <?xml version="1.0"?>
<!--
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Core
 * @copyright   Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
-->
<config>
    <global>
        <install>
            <date/>
        </install>
        <resources>
            <default_setup>
                <connection>
                    <host>localhost</host>
                    <username/>
                    <password/>
                    <dbname>magento</dbname>
                    <model>mysql4</model>
                    <initStatements>SET NAMES utf8</initStatements>
                    <type>pdo_mysql</type>
                    <active>0</active>
                </connection>
            </default_setup>
            <default_write>
                <connection>
                    <use>default_setup</use>
                </connection>
            </default_write>
            <default_read>
                <connection>
                    <use>default_setup</use>
                </connection>
            </default_read>
            <core_setup>
                <setup>
                    <module>Mage_Core</module>
                </setup>
                <connection>
                    <use>default_setup</use>
                </connection>
            </core_setup>
            <core_write>
                <connection>
                    <use>default_write</use>
                </connection>
            </core_write>
            <core_read>
                <connection>
                    <use>default_read</use>
                </connection>
            </core_read>
        </resources>
        <resource>
            <connection>
                <types>
                    <pdo_mysql>
                        <adapter>Varien_Db_Adapter_Pdo_Mysql</adapter>
                        <class>Mage_Core_Model_Resource_Type_Db_Pdo_Mysql</class>
                        <compatibleMode>1</compatibleMode>
                    </pdo_mysql>
                </types>
            </connection>
        </resource>
        <models>
            <varien>
                <class>Varien</class>
            </varien>
            <core>
                <class>Mage_Core_Model</class>
                <resourceModel>core_resource</resourceModel>
            </core>
            <core_resource>
                <class>Mage_Core_Model_Resource</class>
                <deprecatedNode>core_mysql4</deprecatedNode>
                <entities>
                    <config_data>
                        <table>core_config_data</table>
                    </config_data>
                    <website>
                        <table>core_website</table>
                    </website>
                    <store>
                        <table>core_store</table>
                    </store>
                    <resource>
                        <table>core_resource</table>
                    </resource>
                    <cache>
                        <table>core_cache</table>
                    </cache>
                    <cache_tag>
                        <table>core_cache_tag</table>
                    </cache_tag>
                    <cache_option>
                        <table>core_cache_option</table>
                    </cache_option>
                </entities>
            </core_resource>
        </models>
    </global>
    <default>
        <system>
            <filesystem>
             开发者_如何学Go   <base>{{root_dir}}</base>
                <app>{{root_dir}}/app</app>
                <code>{{app_dir}}/code</code>
                <design>{{app_dir}}/design</design>
                <locale>{{app_dir}}/locale</locale>
                <etc>{{app_dir}}/etc</etc>
                <media>{{root_dir}}/media</media>
                <upload>{{root_dir}}/media/upload</upload>
                <skin>{{root_dir}}/skin</skin>
                <var>{{var_dir}}</var>
                <cache>{{var_dir}}/cache</cache>
                <session>{{var_dir}}/session</session>
                <tmp>{{var_dir}}/tmp</tmp>
                <pear>{{var_dir}}/pear</pear>
                <export>{{var_dir}}/export</export>
            </filesystem>
        </system>
        <general>
            <locale>
                <code>en_US</code>
                <timezone>America/Los_Angeles</timezone>
            </locale>
        </general>
    </default>
    <varien>
        <class>Varien</class>
    </varien>
</config>

and local.xml :

<?xml version="1.0"?>
<!--
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category   Mage
 * @package    Mage_Core
 * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
-->
<config>
    <global>
        <install>
            <date><![CDATA[Tue, 26 Apr 2011 15:59:54 +0000]]></date>
        </install>
        <crypt>
            <key><![CDATA[XXXXXXXXXXXXXXXXXXXXXXXXXXXX]]></key>
        </crypt>
        <disable_local_modules>false</disable_local_modules>
        <resources>
            <db>
                <table_prefix><![CDATA[]]></table_prefix>
            </db>
            <default_setup>
                <connection>
                    <host><![CDATA[XXXXXXXXX]]></host>
                    <username><![CDATA[XXXXXXXXXX]]></username>
                    <password><![CDATA[XXXXXXXXXX]]></password>
                    <dbname><![CDATA[XXXXXXX]]></dbname>
                    <active>1</active>
                </connection>
            </default_setup>
        </resources>
        <session_save><![CDATA[files]]></session_save>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <frontName><![CDATA[admin]]></frontName>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>


This error boils down to a config problem. The setup model is trying to install/upgrade some module in your install and failing when it cannot find a write connection for the resource. This would seem to indicate one of two places that has a broken config: The global config (/app/etc/*.xml) or a specific module config (etc/config.xml in the module).

The problem module may have a config section /config/global/resources that contains references to setup/read/write connections. Check if that is the case and if the reference for the write connection is correct. If it's not a specific module, check the global resources core_(read|write|setup) and default_(read|write|setup) for correctness.


Please go to the \app\code\core\Mage\Core\Model\App.php, line 402, you will see there

protected function _initModules() 
{
 ...

Put after { this code print_r(Mage::getConfig()->getNode('global/resources'));exit(); and show us what you got on the screen. In my case I see next thing:

Mage_Core_Model_Config_Element Object
(
    [default_setup] => Mage_Core_Model_Config_Element Object
        (
            [connection] => Mage_Core_Model_Config_Element Object
                (
                    [host] => localhost
                    [username] => root
                    [password] => Mage_Core_Model_Config_Element Object
                        (
                        )

                    [dbname] => magento
                    [initStatements] => SET NAMES utf8
                    [model] => mysql4
                    [type] => pdo_mysql
                    [pdoType] => Mage_Core_Model_Config_Element Object
                        (
                        )

                    [active] => 1
                )

        )

    [default_write] => Mage_Core_Model_Config_Element Object
        (
            [connection] => Mage_Core_Model_Config_Element Object
                (
                    [use] => default_setup
                )

        )

    [default_read] => Mage_Core_Model_Config_Element Object
        (
            [connection] => Mage_Core_Model_Config_Element Object
                (
                    [use] => default_setup
                )

        )

    [core_setup] => Mage_Core_Model_Config_Element Object
        (
            [setup] => Mage_Core_Model_Config_Element Object
                (
                    [module] => Mage_Core
                )

            [connection] => Mage_Core_Model_Config_Element Object
                (
                    [use] => default_setup
                )

        )

    [core_write] => Mage_Core_Model_Config_Element Object
        (
            [connection] => Mage_Core_Model_Config_Element Object
                (
                    [use] => default_write
                )

        )

    [core_read] => Mage_Core_Model_Config_Element Object
        (
            [connection] => Mage_Core_Model_Config_Element Object
                (
                    [use] => default_read
                )

        )

    [db] => Mage_Core_Model_Config_Element Object
        (
            [table_prefix] => Mage_Core_Model_Config_Element Object
                (
                )

        )

)


I also run into similar issue.

In my case while installing Magento when I fill all the db details and click on continute I was getting below Fatal error:

PHP Fatal error:  Call to a member function insert() on boolean in /var/www/public_html/app/code/core/Mage/Core/Model/Resource/Resource.php on line 133"

Magento was trying to connect using write/read adapter but connection not established because local.xml file was empty.

In file app\code\core\Mage\Install\Model\Installer\Config.php on line 103 magento trying to fetch content of local.xml.template file and creating local.xml file nodes.

    $template = file_get_contents(Mage::getBaseDir('etc') . DS . 'local.xml.template');
    foreach ($data as $index => $value) {
        $template = str_replace('{{' . $index . '}}', '<![CDATA[' . $value . ']]>', $template);
    }
    file_put_contents($this->_localConfigFile, $template);

So I checked there in app/etc/local.xml.template file was not present because we haven't kept it part of our git code. After placing local.xml.template file issue resolved.

I hope this will help others.


Be careful of the file permissions, I had the same problem. Just make sure you are installing magento with the appropiate file permissions (eg: the user is the owner of the files)


Another possibility is that your magento/lib directory is not updated. If you did the manual process as I did, you might have missed updating that dir. I discovered that there are quite a few reasons why the connection object might be blank:

  • Invalid configuration (may be missing stuff needed for 1.6+, may have wrong information, may be cached with old data) - seems that var/cache files might be responsible also, depending on how your caching is set up.
  • Something is really messed up in the module configuration. In our case, this wasn't the issue: core_setup was the module being upgraded.
  • Old versions of class files (seems to apply for vers less than 1.6 updating to 1.6+) in particular, when creating or getting the connection objects, it looks to see if it extends a new interface, Varien_Db_Adapter_Interface.
  • The possibility of bad file permissions messing things up exists, as always.

For the third, a quick check of app/code/core/Mage/Core/Model/Resource.php shows reasons why the _connection data members might be invalid. One of the main checks it makes is to see if the connection object created is an instance of (includes a subclass of) Varien_Db_Adapter_Interface. From the libraries for 1.4.1.1 (in our case)

class Varien_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Mysql

But in 1.6+

class Varien_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Mysql implements Varien_Db_Adapter_Interface

ergo, the check fails and even with valid configurations, you don't get connection objects. It is poor programming on their part that it doesn't REPORT this problem VERY SPECIFICALLY since it creates an error state behavior identical to, but not at all related to, another common error: misconfiguration of the database connection. It's not like it's a passive check. They explicitly check for the new interface, but then don't log or produce any kind of potential debug information regarding it.

As a final note, during the upgrade if you have lots of stuff, you may wish to explicitly log this failure in case something somewhere in your code IS using an old DB adapter somehow. (in our case, some code written by another contractor had overridden the adapter in the local namespace, which generated this error again.)


For me the solution was to set the database connection in config.xml so <active>1</active>

IMHO Magento should raise an exception in Mage_Core_Model_Resource when the connection is not active, instead of just returning false and propagating this false value to where it is expected to be an object.


I had this problem, when I have rewritten method _construct from parent Model Resource class, but I've forgotten to insert, aparat my custom logic, essentail line in every Resource model constructor:

$this->_init('catalog/category_flat', 'entity_id');

When I put that back, all worked like charm.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜