【问题标题】:Fatal error on /app/code/core/Mage/Core/Model/Resource/Resource.php in MagentoMagento 中 /app/code/core/Mage/Core/Model/Resource/Resource.php 的致命错误
【发布时间】:2011-11-03 15:55:59
【问题描述】:

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

【问题讨论】:

  • 您能否发布您的 /app/etc/config.xml 和 /app/etc/local.xml 文件(当然已编辑)
  • 我也遇到了同样的问题。在我的情况下, app/etc/local.xml.template 文件不存在,所以 magento 正在创建空的 local.xml 文件。 Magento 试图使用写/读适配器进行连接,但连接未建立,因为 local.xml 文件为空。在第 103 行的文件 app\code\core\Mage\Install\Model\Installer\Config.php 中,magento 试图获取 local.xml.template 文件的内容并创建 local.xml 文件。这是因为我们没有将 local.xml.template 文件作为 git 代码的一部分。
  • 我遇到了同样的问题,同样的文件,同样的行。 Magento 本身一定有问题。我删除了所有第三方(本地和社区)模块,方法是从 app/etc/modules 目录中删除它们的 .xml 文件,只留下 magento 本机模块。问题依然存在。

标签: magento fatal-error


【解决方案1】:

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

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

【讨论】:

  • 嗯,我发现的问题是数据库连接的新参数是从 Magento 1.6 开始设置的。请在您的 local.xml &lt;model&gt;&lt;![CDATA[mysql4]]&gt;&lt;/model&gt; &lt;type&gt;&lt;![CDATA[pdo_mysql]]&gt;&lt;/type&gt; &lt;pdoType&gt;&lt;![CDATA[]]&gt;&lt;/pdoType&gt; 中检查这些添加的参数
  • 这个信息对我很有帮助。谢谢 Subesh!
【解决方案2】:

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

protected function _initModules() 
{
 ...

{ 之后添加此代码print_r(Mage::getConfig()-&gt;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
                (
                )

        )

)

【讨论】:

  • 您要求澄清问题,而不是提供答案。这应该是对问题的评论。
  • @Lee Saferite 我知道,但由于字符数限制,无法将这个问题插入评论框。归咎于stackoverflow界面......下次我最好不要在这种情况下问任何东西,感谢所有按下-1的人。
  • @Zyava 不要为你的贡献而松懈+1
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    另一种可能是您的 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 命名空间中的适配器,这再次产生了这个错误。)

    【讨论】:

      【解决方案5】:

      对我来说,解决方案是在 config.xml 中设置数据库连接,所以 &lt;active&gt;1&lt;/active&gt;

      恕我直言,当连接不活动时,Mage_Core_Model_Resource 应该在 Mage_Core_Model_Resource 中引发异常,而不是仅仅返回 false 并将这个 false 值传播到预期成为对象的位置。

      【讨论】:

        【解决方案6】:

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

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

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

        【讨论】:

          【解决方案7】:

          我也遇到了类似的问题。

          在我安装 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文件后问题解决。

          我希望这对其他人有所帮助。

          【讨论】:

          • 还要检查local.xml文件的权限和所有权。
          • 等等! install.php 脚本不应该负责创建local.xml 文件吗?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-13
          相关资源
          最近更新 更多