【问题标题】:Magento admin grid was not displaying only header title and add button are presentMagento 管理网格不只显示标题标题和添加按钮
【发布时间】:2016-02-18 10:10:44
【问题描述】:

感谢@PHP Weblineindia 分享本教程的链接http://navaneeth.me/creating-magento-extension-with-custom-database-table/#comment-8147

我几乎遵循了本教程给出的所有细节,但是我无法显示表格网格。

更新:在调试我的模型集合时出现问题

另一个更新:我更新了所有资源模型和集合并将它们扩展到这个

Mage_Core_Model_Resource_Db_Abstract -> 资源模型

Mage_Core_Model_Resource_Db_Collection_Abstract -> 集合模型

好处是。 我现在可以向数据库添加新数据了。

但主要问题还是一样,没有显示网格

我尝试在 magento 前端进行简单的数据检索,但它给了我致命错误:在非对象上调用成员函数 load()

我还是 magento 的新手,我需要发现很多谜团。

型号

app/code/local/Rts/Pmadmin/Model/Pmadmin.php

class Rts_Pmadmin_Model_Pmadmin extends Mage_Core_Model_Abstract {
protected function _construct()
{
    $this->_init('pmadmin/pricematrix');
}
}

app/code/local/Rts/Pmadmin/Model/Mysql4/Resource/Pmadmin.php

class Rts_Pmadmin_Model_Mysql4_Resource_Pmadmin extends Mage_Core_Model_Resource_Db_Abstract {
protected function _construct()
{
    $this->_init('pmadmin/pmadmin', 'pmadmin_id');
}
}

app/code/local/Rts/Pmadmin/Model/Mysql4/Resource/Collection.php

class Rts_Pmadmin_Model_Mysql4_Resource_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract {
public function _construct()
{
    parent::_construct();
    $this->_init('pmadmin/pmadmin');
}
}

这些是代码。

更新的 config.xml

<global>
    <helpers>
        <pmadmin>
            <class>Rts_Pmadmin_Helper</class>
        </pmadmin>
    </helpers>
    <blocks>
        <pmadmin>
            <class>Rts_Pmadmin_Block</class>
        </pmadmin>
    </blocks>
    <models>
        <pmadmin>
            <class>Rts_Pmadmin_Model</class>
            <resourceModel>pmadmin_resource</resourceModel>
        </pmadmin>
        <pmadmin_resource>
            <class>Rts_Pmadmin_Model_Resource</class>
            <entities>
                <pmadmin>
                    <table>pmadmin</table>
                </pmadmin>
            </entities>
        </pmadmin_resource>
    </models>
    <resources>
        <pmadmin_setup>
            <setup>
                <module>Rts_Pmadmin</module>
               <class>Rts_Pmadmin_Model_Mysql4_Resource_Mysql4_Setup</class>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </pmadmin_setup>
        <pmadmin_write>
            <connection>
                <use>core_write</use>
            </connection>
        </pmadmin_write>
        <pmadmin_read>
            <connection>
                <use>core_read</use>
            </connection>
        </pmadmin_read>
    </resources>
    </global>

更新:内容块

class Rts_Pmadmin_Block_Adminhtml_Pmadmin_Grid extends Mage_Adminhtml_Block_Widget_Grid {

public function __construct() {
    parent::__construct();
    $this->setId('pmadmingrid');
    $this->setDefaultSort('pmadmin_id');
    $this->setDefaultDir('ASC');
    $this->setSaveParametersInSession(true);
    $this->setUseAjax(true);
}

protected function _prepareCollection() {
    $collection = Mage::getModel('pmadmin/pmadmin')->getCollection();
    // print_r($collection); exit();
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

protected function _prepareColumns() {
    $this->addColumn('pricematrix_id', array(
        'header' => Mage::helper('pmadmin')->__('ID'),
        'align' => 'right',
        'width' => '10px',
        'index' => 'pricematrix_id',
    ));

    $this->addColumn('title', array(
        'header' => Mage::helper('pmadmin')->__('Title'),
        'align' => 'left',
        'index' => 'title',
        'width' => '50px',
    ));


    $this->addColumn('short_description', array(
        'header' => Mage::helper('pmadmin')->__('Description'),
        'width' => '150px',
        'index' => 'short_description',
    ));

    $this->addColumn('file_path', array(
        'header' => Mage::helper('pmadmin')->__('File Path'),
        'width' => '150px',
        'index' => 'file_path',
    ));


    $this->addColumn('customer_group', array(
        'header' => Mage::helper('pmadmin')->__('Customer Group'),
        'width' => '150px',
        'index' => 'customer_group',
    ));


    $this->addColumn('creation_time', array(
        'header' => Mage::helper('pmadmin')->__('Posted On'),
        'width' => '150px',
        'index' => 'creation_time',
    ));

    return parent::_prepareColumns();
}


public function getRowUrl($row) {
    return $this->getUrl('*/*/edit', array('id' => $row->getId()));
}

public function getGridUrl()
{
  return $this->getUrl('*/*/grid', array('_current'=>true));
}
}

问题:

  1. 没有网格只显示标题标题和添加按钮

  2. 单击添加按钮时,会显示表单,但我无法添加新项目

  3. 模型集合在 var 转储后返回“bool(false)”

问题:

你能帮我找出问题吗?

谢谢

【问题讨论】:

  • 阅读内容太多,尤其是在我看到你几乎关注每一个细节之后......
  • @ElefantPhace 我道歉。
  • 尝试在网格文件中打印集合是否有集合。转到 Block->Adminhtml->Pmadmin->Grid.php 保护函数 _prepareCollection() { $collection = Mage::getModel('pmadmin/pmadmin')->getCollection(); print_r($collection);死; $this->setCollection($collection);返回父级::_prepareCollection(); }
  • 我已经尝试过了,但它没有显示任何内容。我还更新了我的模型,现在可以将新数据添加到数据库中。
  • 您好 Rodge,我认为您的 config.xml 文件有问题按照给定教程链接的所有步骤操作。在您的 config.xml 中,使 应该与教程相同。我会建议你遵循与给定链接相同的副本。

标签: php xml magento


【解决方案1】:

您的模型文件夹结构存在问题。我不是 magento 专家,但我认为您将资源模型和集合模型分开到单独的文件夹中。

基于@Nicolas D 的回答,我在 magento 1.9.1 上也遇到了这个问题。我也花了几天的时间才找到解决方案。

是的,@Nicolas D 是对的,您必须将资源文件夹放在 Mysql4 文件夹中。但是,在 Mysql4 文件夹下,您必须创建一个与您的模块名称相同的文件夹并放置您的 Collection.php 模型。

例如:

代替

Rts_Pmadmin_Model_Mysql4_Resource

做起来

Rts_Pmadmin_Model_Mysql4_Pmadmin

还有!不要忘记更新您的 config.xml 和其他与您的更改相关的文件。

<models>
    <pmadmin>
        <class>Rts_Pmadmin_Model</class>
        <resourceModel>pmadmin_resource</resourceModel>
    </pmadmin>
    <pmadmin_resource>
        <class>Rts_Pmadmin_Model_Mysql4</class>
        <entities>
            <pmadmin>
                <table>pmadmin</table>
            </pmadmin>
        </entities>
    </pmadmin_resource>
</models>
<resources>
    <pmadmin_setup>
        <setup>
            <module>Rts_Pmadmin</module>
           <class>Rts_Pmadmin_Model_Mysql4_Pmadmin_Mysql4_Setup</class>
        </setup>
        <connection>
            <use>core_setup</use>
        </connection>
    </pmadmin_setup>
    <pmadmin_write>
        <connection>
            <use>core_write</use>
        </connection>
    </pmadmin_write>
    <pmadmin_read>
        <connection>
            <use>core_read</use>
        </connection>
    </pmadmin_read>
</resources>

我不知道这是否是一个 magento 错误,但这就是我们解决问题的方法。

【讨论】:

    【解决方案2】:

    我曾经在我的旧 magento 版本上遇到过这个问题,这似乎也是你的情况,因此该集合是错误的:

    你必须在你的类名中添加 Mysql4。 配置:

    <pmadmin_resource>
        <class>Rts_Pmadmin_Model_Mysql4_Resource</class>
        <entities>
            <pmadmin>
                <table>pmadmin</table>
            </pmadmin>
        </entities>
    </pmadmin_resource>
    

    型号:

    Rts_Pmadmin_Model_Mysql4_Resource_Pricematrix_Collection
    Rts_Pmadmin_Model_Mysql4_Resource_Pricematrix
    

    【讨论】:

    • 是的,我的模型无法加载数据,但它允许我在数据库中添加新数据。它是一个magento错误吗?我更新了代码,但它没有显示网格表。
    • 您的模型类是否也在代码上更新得很好?此处不更新
    • 对不起,我忘记更新帖子了。我更新了我所有的模型和 config.xml 但没有运气
    • 我猜日志中没有错误?如果没有,请尝试一一调试您的小部件块功能,以查看已加载的内容和未加载的内容
    • 是的,没有错误日志。我没有 magento 的调试技能,但我会从 stackoverflow.com/questions/28320628/… ---- 和 ------ stackoverflow.com/questions/4784117/… 尝试这种技术
    猜你喜欢
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2015-02-18
    • 2017-12-19
    • 1970-01-01
    • 2014-12-03
    相关资源
    最近更新 更多