我如何在Magento网格中显示选择选项
-
16-10-2019 - |
题
我创建了一个使用Admin Grid.php的模块。我创建了三个表来添加和检索数据,但是我无法从一个模型中获取数据以在下拉列表中显示GRID.PHP文件中的数据。
我遇到的问题是,由于某种原因,我无法从表中获取数据以在下拉列表中显示。
请参阅下面的代码:
$this->addColumn("region", array(
"header" => Mage::helper("modulename")->__("Region"),
"type" => 'options',
'options'=> Mage::getModel('modulename/region')->getRegions()
));
以上在网格中添加了一个区域列,并从我的模型中获取数据
型号/区域.php
protected function _construct(){
$this->_init("modulename/region");
}
public function getRegions() {
$regionsArray = array();
foreach($this->getCollection() as $region){
$regionsArray[$region->getId()] = $region->getTitle();
}
return $regionsArray;
}
但是,以下列是引起问题的原因
$this->addColumn('state', array(
'header' => Mage::helper('modulename')->__('State'),
'index' => 'state',
'type' => 'options',
'options'=> Mage::getModel('modulename/customerstate')->getState()
));
我似乎无法从模型中获取数据来显示选项
protected function _construct(){
$this->_init("modulename/customerstate");
}
public function getStates() {
$statesArray = array();
foreach($this->getCollection() as $state){
$statesArray[$state->getId()] = $state->getState();
}
return $statesArray;
}
定制DB表
+-------+----------+
| s_id | state |
+-------+----------+
| 1 | pending |
| 2 | active |
+-------+----------+
我不知道为什么会发生这种情况,并且可以做一些建议以帮助解决这个问题,因为我已经绕圈了几个小时了 - 我不会遇到错误,只是一个空网格上一个空的选择选项...
解决方案
感谢@kalpesh mehta和@marius的贡献,
我用以下代码解决了该错误,如前所述->getState()
代替 ->getStates()
grid.php
$this->addColumn('state', array(
'header' => Mage::helper('modulename')->__('State'),
'index' => 'state',
'type' => 'options',
'options'=> Mage::getModel('modulename/customerstate')->getStates()
));
型号/customerstate.php
public function getStates() {
$statesArray = array();
foreach($this->getCollection() as $state){
$statesArray[$state->getId()] = $state->getState();
}
return $statesArray;
}
其他提示
乍一看,您接缝处于正确的轨道上。
正如错误所暗示的那样,问题是:
Mage::getSingleton('my/modulename')
很可能会返回 null
.
从您发布的代码中,我可以得出结论 My
是您的名称空间, Module
是模块名称(du!)。
我以上代码应该是:
Mage::getSingleton('module/[entity_name_here]');
在哪里 [entity_name_here]
应该是您的别名 region
模型