Php 网格中的Magento getCollection()返回false
我目前正在学习如何使用Magento后端。我已经创建了一个模型类Php 网格中的Magento getCollection()返回false,php,mysql,magento,Php,Mysql,Magento,我目前正在学习如何使用Magento后端。我已经创建了一个模型类example/event和一个相应的资源。我可以将记录(通过Magento管理表单)添加到我创建的DB表中example\u event。但是,我在从表中检索记录时遇到问题。我的网格级别如下: class MasteringMagento_Example_Block_Adminhtml_Event_Grid extends Mage_Adminhtml_Block_Widget_Grid { protected func
example/event
和一个相应的资源。我可以将记录(通过Magento管理表单)添加到我创建的DB表中example\u event
。但是,我在从表中检索记录时遇到问题。我的网格级别如下:
class MasteringMagento_Example_Block_Adminhtml_Event_Grid extends Mage_Adminhtml_Block_Widget_Grid {
protected function _prepareCollection() {
$collection = Mage::getModel('example/event')->getCollection();
var_dump($collection); // bool(false)
//$record = Mage::getModel('example/event')->load(1); //id from example_event table
//var_dump($record); // returns object record as expected
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns() {
$this->addColumn('name', array(
'type'=>'text',
'index'=>'name',
'header'=>$this->__('Name')
));
$this->addColumn('start', array(
'type'=>'date',
'index'=>'start',
'header'=>$this->__('Start Date')
));
$this->addColumn('end', array(
'type'=> 'date',
'index'=>'end',
'header'=>$this->__('End Date')
));
return $this;
}
}
如代码中所述,$collection=Mage::getModel('example/event')->getCollection()
返回false。但是,我可以使用$record=Mage::getModel('example/event')->load(1)从数据库表中检索单个记录代码>其中1是记录的ID(这只是一个健全的检查,以确保至少我写的东西可以与DB对话)。目标是将集合呈现到由\u prepareColumns()
函数构建的网格中
同样,我对Magento后端的编程是全新的,但我已经一遍又一遍地查看代码,似乎无法理解我的集合对象为何为空。如果可以:
$record = Mage::getModel('example/event')->load(1)
但你不能
Mage::getModel('example/event')->getCollection();
主要原因是您没有在模块中创建集合模型。
需要这样做:
class MasteringMagento_Example_Model_Resource_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract {
protected function _construct() {
$this->_init('example/event');
}
}
如果可以:
$record = Mage::getModel('example/event')->load(1)
但你不能
Mage::getModel('example/event')->getCollection();
主要原因是您没有在模块中创建集合模型。
需要这样做:
class MasteringMagento_Example_Model_Resource_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract {
protected function _construct() {
$this->_init('example/event');
}
}
因此,您可以加载(1),但无法加载所有集合?首先,检查var/log
文件夹中的错误。然后,确保该文件存在:MasteringMagento/Example/Model/Resource/Event/Collection.php
。如果有,请将该文件的内容以及config.xml和MasteringMagento/Example/Model/Event.php的内容发布到您的问题中。@sergio这是正确的。@Marius我有MasteringMagento/Example/Model/Resource/Event.php
。我缺少MasteringMagento/Example/Model/Resource/Event/Collection.php
。我遵循的指南没有提到这样的课程,所以这可能就是问题所在。我将进一步研究这一点。正如我所说,我对它是全新的,所以感谢您为我指明了正确的方向。因此,您可以加载(1),但无法加载所有集合?首先,检查var/log
文件夹中的错误。然后,确保该文件存在:MasteringMagento/Example/Model/Resource/Event/Collection.php
。如果有,请将该文件的内容以及config.xml和MasteringMagento/Example/Model/Event.php的内容发布到您的问题中。@sergio这是正确的。@Marius我有MasteringMagento/Example/Model/Resource/Event.php
。我缺少MasteringMagento/Example/Model/Resource/Event/Collection.php
。我遵循的指南没有提到这样的课程,所以这可能就是问题所在。我将进一步研究这一点。正如我所说的,我对它是全新的,所以谢谢你为我指明了正确的方向!就这样。我需要class MasteringMagento\u Example\u Model\u Resource\u Event\u Collection在MasteringMagento/Example/Model/Resource/Event/Collection.php中扩展Mage\u Core\u Model\u Resource\u Db\u Collection\u Abstract
。我现在更了解如何在Magento中使用模型。令人惊讶的是,我正在使用的教程没有提到集合摘要。可能需要一个新的教程。很好,它可以帮助你和@marius谢谢你,谢谢你!就这样。我需要class MasteringMagento\u Example\u Model\u Resource\u Event\u Collection在MasteringMagento/Example/Model/Resource/Event/Collection.php中扩展Mage\u Core\u Model\u Resource\u Db\u Collection\u Abstract
。我现在更了解如何在Magento中使用模型。令人惊讶的是,我正在使用的教程没有提到集合摘要。可能需要一个新的教程。很好,它可以帮助你