Magento getCollection()返回false

时间:2016-04-05 17:54:30

标签: php magento

我正在研究Alan Storm's Weblog tutorial。我可以从模型中检索单个记录(Model-> load(id))但是对Model-> getCollection()的调用返回false。

以下是我的config.xml中的全局部分:

<global>
   <models>
      <weblog>
          <class>Magentotutorial_Weblog_Model</class>
          <resourceModel>weblog_resource</resourceModel>
      </weblog>
      <weblog_resource>
        <class>Magentotutorial_Weblog_Model_Resource</class>
        <entities>
            <blogpost>
                <table>blog_posts</table>
            </blogpost>
        </entities>
      </weblog_resource>
  </models>
</global>

我的模型在app / code / local / Magentotutorial / Weblog / Model / Blogpost.php中:

<?php
class Magentotutorial_Weblog_Model_Blogpost extends Mage_Core_Model_Abstract
{
    protected function _construct()
    {
        $this->_init('weblog/blogpost');
    }
}

我在app / code / local / Magentotutorial / Weblog / Model / Resource / Blogpost.php中的资源模型 - 这个有效,我可以成功调用Model-&gt; load(id):

<?php
class Magentotutorial_Weblog_Model_Resource_Blogpost extends Mage_Core_Model_Resource_Db_Abstract {
    protected function _construct()
    {
        $this->_init('weblog/blogpost', 'blogpost_id');
    }
}

我的收藏类在app / code / local / Magentotutorial / Weblog / Model / Resource / Blogpost / Collection.php中:

<?php
class Magentotutorial_Weblog_Model_Resource_Blogpost_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract {
    protected function _construct()
    {
            $this->_init('weblog/blogpost');
    }
}

控制器中尝试获取集合的方法。 var_dump调用打印“bool(false)”:

public function showAllBlogPostsAction() {
    $posts = Mage::getModel('weblog/blogpost')->getCollection();
    var_dump($posts);
    foreach($posts as $blogpost){
      echo '<h3>'.$blogpost->getTitle().'';
      echo nl2br($blogpost->getPost());
}

}

这是我模块的源代码树: enter image description here

更新:我确定没有包含Collection.php文件。如果我手动包含该文件(来自indexController.php)它可以工作。现在我想弄清楚文件没有被包含的原因。

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

在编写教程的某些时候,我搞砸了文件夹名称或类的大小写。我相信我在某个地方使用过BlogPost。我解决了这个问题,但我不知道我正在处理的网站安装了class path cache,因此当您更改文件或目录的名称时,您必须清除缓存:

sitename/shell>php aoe_classpathcache.php -action clear

我第一次跑,我得到了:

Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[HY000] [2002] No such file or directory' in...

要修复我必须在我的app / etc / local.xml中将“localhost”更改为“127.0.0.1”。

相关问题