gpt4 book ai didi

php - 网格中的 Magento getCollection() 返回 false

转载 作者:太空宇宙 更新时间:2023-11-03 10:55:37 26 4
gpt4 key购买 nike

我目前正在学习如何使用 Magento 后端。我创建了一个模型类 example/event 和相应的资源。我能够将记录(通过 Magento 管理表单)添加到我创建的数据库表中 example_event。但是,我无法从我的表中检索记录。我的 Gird 类如下:

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 是记录(这只是一个健全性检查,以确保至少我写的东西可以与数据库对话)。目标是将集合呈现到由 _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');
}
}

关于php - 网格中的 Magento getCollection() 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21047912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com