Model :: load($ params [' id'])因致命错误而失败

时间:2014-04-05 17:06:39

标签: magento

本地/ Nofrills / Booklayout /控制器/ ModelController.php

<?php
class Nofrills_Booklayout_ModelController  extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
     $params = $this->getRequest()->getParams();
     $model = Mage::getModel('nofrills_booklayout/testmodel');
     echo("Loading the blogpost with an ID of ".$params['id']);
     $model->load($params['id']);
     $data = $model->getData();
     var_dump($data);
    }

}
?>

本地/ Nofrills / Booklayout /型号/ Testmodel.php

<?php
class Nofrills_Booklayout_Model_Testmodel extends Mage_Core_Model_Abstract
{
  protected function _construct()
  {
    parent::_construct();
    $this->_init('nofrills_Booklayout/testmodel');
  }
}
?>

local / Nofrills / Booklayout / etc / Config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
    <Nofrills_Booklayout>
        <version>0.1.0</version>
    </Nofrills_Booklayout>
</modules>
<frontend>
    <routers>
        <nofrills_booklayout>
            <use>standard</use>
            <args>
                <module>Nofrills_Booklayout</module>
                <frontName>nofrills_booklayout</frontName>
            </args>
        </nofrills_booklayout>
    </routers>
</frontend>
<global>
    <blocks>
        <nofrills_booklayout>
            <class>Nofrills_Booklayout_Block</class>
        </nofrills_booklayout>
    </blocks>
    <models>
        <nofrills_booklayout>
            <class>Nofrills_Booklayout_Model</class>
             <resourceModel>nofrills_booklayout_resource</resourceModel> 
        </nofrills_booklayout>
         <nofrills_booklayout_resource>
            <class>Nofrills_Booklayout_Model_Resource</class>
         </nofrills_booklayout_resource>
    </models>
    <helpers>
        <nofrills_booklayout>
            <class>Nofrills_Booklayout_Helper</class>
        </nofrills_booklayout>
    </helpers>
</global>

执行以下控制器操作&#34; http://myserver.com/magento/nofrills_booklayout/model/index/id/1&#34; ModelController.php中的代码行Mage :: getModel()返回有效对象。 Testmodel.php位于local / Nofrills / Booklayout / Model / Testmodel.php中。 执行代码行$ model-&gt; load($ params [&#39; id&#39;]);预期的错误是:

Warning: include(Nofrills/Booklayout/Model/Resource/Testmodel.php) [function.include]: failed to open stream: No such file ...

显示的实际错误是

Loading the blogpost with an ID of 1 Fatal error: Call to a member function load() on a non-object in /var/www/magento/app/code/core/Mage/Core/Model/Abstract.php on line 225 Call Stack: 0.0119 660352 1. {main}() /var/www/magento/index.php:0 0.0144 1219280 2. Mage::run(???, ???, ???) /var/www/magento/index.php:87 0.0205 3044928 3. Mage_Core_Model_App->run(???) /var/www/magento/app/Mage.php:684 1.1513 16921392 4. Mage_Core_Controller_Varien_Front->dispatch() /var/www/magento/app/code/core/Mage/Core/Model/App.php:354 1.1652 18830664 5. Mage_Core_Controller_Varien_Router_Standard->match(???) /var/www/magento/app/code/core/Mage/Core/Controller/Varien/Front.php:172 1.1694 19696808 6. Mage_Core_Controller_Varien_Action->dispatch(???) /var/www/magento/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php:250 1.2951 23809000 7. Nofrills_Booklayout_ModelController->indexAction() /var/www/magento/app/code/core/Mage/Core/Controller/Varien/Action.php:418 29.6125 24956920 8. Mage_Core_Model_Abstract->load(???, ???) /var/www/magento/app/code/local/Nofrills/Booklayout/controllers/ModelController.php:9 

在调试过程中,我注意到函数 _getResourceModelFactoryClassName($ modelClass)返回false。

可以帮助识别问题吗?

2 个答案:

答案 0 :(得分:0)

如果您想调用模型然后调用

,我认为您正在以错误的方式调用模型
 $model = Mage::getModel('booklayout/testmodel');
     echo("Loading the blogpost with an ID of ".$params['id']);
     $model->load($params['id']);

如果你想调用模型,那么它的语法将是

$model = Mage::getModel('MODULE_NAME/MODEL_NAME');

如果您想要任何查询,请告诉我

答案 1 :(得分:0)

默认情况下,Magento使用您在模型构造函数中传递的字符串作为其_init方法的参数来加载Model的支持资源。在您的情况下,您通过了'nofrills_Booklayout/testmodel'

Mage_Core_Model_Config使用此字符串来检索模型的config.xml的xml节点,该节点“代表”模块的资源类所在的子目录。 Mage_Core_Model_Config::_getResourceModelFactoryClassName将该字符串分为两部分(分别是斜杠之前的部分和部分之后的部分)。

第一部分用于上述xml节点。由于_getResourceModelFactoryClassName找不到有效的节点,因此返回false(正如您已经想到的那样,导致错误的原因«对非对象的成员函数load()的调用»

如果您更换

$this->_init('nofrills_Booklayout/testmodel');

使用:

$this->_init('nofrills_booklayout/testmodel');

你得到了你预期的错误。