Magento中/app/code/core/Mage/Core/Model/Resource/Resource.php致命错误

时间:2011-08-25 12:36:37

标签: magento fatal-error

刚刚将Magento安装从1.5升级到1.6,并留下了这个错误:

  

致命错误:在非对象中调用成员函数insert()   /hsphere/local/home/t21004/XXXXXXXXXXXXX.com/app/code/core/Mage/Core/Model/Resource/Resource.php   在第133行

第133行的内容如下:

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

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

7 个答案:

答案 0 :(得分:7)

此错误归结为配置问题。安装模型尝试在安装中安装/升级某些模块,并在无法找到资源的写连接时失败。这似乎表明配置中断的两个地方之一:全局配置(/app/etc/*.xml)或特定模块配置(模块中的etc / config.xml)。

问题模块可能有一个config部分/ config / global / resources,其中包含对setup / read / write连接的引用。检查是否是这种情况,以及写连接的引用是否正确。如果它不是特定模块,请检查全局资源core_(读取|写入|设置)和default_(读取|写入|设置)是否正确。

答案 1 :(得分:3)

请转到\app\code\core\Mage\Core\Model\App.php第402行,你会看到

protected function _initModules() 
{
 ...

将此代码{放在print_r(Mage::getConfig()->getNode('global/resources'));exit();之后,向我们展示您在屏幕上显示的内容。在我的情况下,我看到了下一件事:

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

        )

)

答案 2 :(得分:1)

小心文件权限,我遇到了同样的问题。 只需确保使用适当的文件权限安装magento(例如:用户是文件的所有者)

答案 3 :(得分:1)

另一种可能性是您的 magento / lib 目录未更新。如果您按照我的方式完成手动过程,则可能错过了更新该目录。我发现连接对象可能为空的原因有很多:

  • 配置无效(可能缺少1.6+所需的内容,可能有错误信息,可能会使用旧数据缓存) - 似乎var/cache文件也可能负责,具体取决于您的缓存设置方式。
  • 模块配置中有些东西搞砸了。在我们的例子中,这不是问题:core_setup是正在升级的模块。
  • 旧版本的类文件(似乎适用于小于1.6的更新到1.6+),特别是在创建或获取连接对象时,它会查看它是否扩展了新接口Varien_Db_Adapter_Interface
  • 一如既往,存在不良文件权限混乱的可能性。

对于第三个,快速检查app/code/core/Mage/Core/Model/Resource.php显示了_connection数据成员可能无效的原因。它所做的一个主要检查是查看创建的连接对象是否是(包括Varien_Db_Adapter_Interface的子类的实例。来自1.4.1.1的库(在我们的例子中)

class Varien_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Mysql

但是在1.6 +

class Varien_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Mysql implements Varien_Db_Adapter_Interface

ergo,检查失败,即使使用有效配置,也不会获得连接对象。他们的编程很糟糕,它不会报告这个问题非常具体,因为它创建的错误状态行为相同,但根本不相关,另一个常见错误:数据库连接配置错误。这不像是被动检查。它们显式检查新接口,但不记录或生成任何类型的潜在调试信息。

作为最后一点,在升级过程中,如果你有很多东西,你可能希望明确地记录这个失败,以防代码中的某个地方以某种方式使用旧的数据库适配器。 (在我们的例子中,另一个承包商编写的一些代码覆盖了local命名空间中的适配器,后者再次生成了此错误。)

答案 4 :(得分:0)

对我来说,解决方案是在config.xml中设置数据库连接,以便<active>1</active>

当连接未激活时,IMHO Magento应在Mage_Core_Model_Resource中引发异常,而不是仅返回false并将此false值传播到预期为对象的位置。

答案 5 :(得分:0)

我有这个问题,当我从父模型资源类重写方法_construct时,但我忘了插入,在每个资源模型构造函数中使用我的自定义逻辑,essentail行:

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

当我把它放回去时,一切都像魅力一样。

答案 6 :(得分:0)

我也遇到了类似的问题。

在我填写Magento的情况下,当我填写所有数据库详细信息并点击“连续”时,我遇到了致命错误:

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尝试使用写/读适配器进行连接,但由于local.xml文件为空,因此未建立连接。

在第103行的文件app \ code \ core \ Mage \ Install \ Model \ Installer \ Config.php中,magento尝试获取local.xml.template文件的内容并创建local.xml文件节点。

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

所以我在app / etc / local.xml.template文件中检查过,因为我们还没有把它作为我们的git代码的一部分。放置local.xml.template文件后解决了问题。

我希望这会对别人有所帮助。