【问题标题】:Grid column filter not working with two models in magento custom module网格列过滤器不适用于 magento 自定义模块中的两个模型
【发布时间】:2015-09-24 23:55:55
【问题描述】:

我正在使用 magento 后端的自定义模块,在使用过滤器回调时,此过滤器不起作用! 任何人都可以建议我吗? 谢谢!

我试过这样的一些代码,

Grid.php

protected function _prepareCollection()
    {
     $collection = Mage::getModel('listings/listings')->getCollection();
     $this->setCollection($collection);
       return parent::_prepareCollection();
    }
protected function _prepareColumns()
    {     
        $this->addColumn("item_id", array(
            "header" => Mage::helper("linkmanagement")->__("ID"),
            "align" => "center",
            "type" => "number",
            "index" => "item_id",

        ));

        $this->addColumn("title", array(
            "header" => Mage::helper("linkmanagement")->__("Title"),
            "index" => "title"
        ));

        $this->addColumn("cat_ids", array(
            "header" => Mage::helper("linkmanagement")->__("Cat ID"),
            "align" => "center",
            "index" => "cat_ids",
            "renderer" => 'Sathish_Linkmanagement_Block_Adminhtml_Linkmanagement_Renderer_Categories',
            'filter_condition_callback' => array($this, '_categoriesFilter')
        ));

        $this->addColumn("url_key", array(
            "header" => Mage::helper("linkmanagement")->__("URL"),
            "index" => "url_key",
            "width" => "200px",
        ));

        $this->addColumn('status',
            array(
                'header' => Mage::helper('linkmanagement')->__('Status'),
                'index' => 'status',
                'type' => 'options',
                'options' => array('1' => Mage::helper('linkmanagement')->__('Active'),
                    '0' => Mage::helper('linkmanagement')->__('Inactive')),
            )
        );

        return parent::_prepareColumns();
    }

回调函数

protected function _categoriesFilter($collection, $column)
    {
        if (!$value = $column->getFilter()->getValue()) {
            return $this;
        }

        $this->getCollection()->getSelect()->where(
            "cat_ids ?"
            , "%$value%");
        return $this;
    }

Categories.php

class Sathish_Linkmanagement_Block_Adminhtml_Linkmanagement_Renderer_Categories extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{

    public function render(Varien_Object $row)
    {
        $value =  $row->getData($this->getColumn()->getIndex());
        // $value = 38,92
        $value = explode(',',$value);
        $collection = Mage::getModel("categories/categories")->getCollection()->addFieldToFilter('category_id',$value)->getData();
        foreach($collection as $col){
            $result[] = $col['title'];
        }
        $result = implode(',', $result);// $result = schools,colleges
        return $result;
    }
}

注意: 渲染器工作正常。, 只有过滤器回调函数不起作用!!!

【问题讨论】:

  • 定义不工作。回调是否被调用过?回调代码对结果没有影响吗?
  • “不起作用”当我尝试过滤该列时它不会返回过滤器行
  • 那么使用回调时生成的 SQL 是什么?如果,当您应用回调过滤器时,您没有得到任何结果,那么您的查询可能是错误的。鉴于您发布的信息,很难给出答案。

标签: magento filter grid adminhtml


【解决方案1】:

我用我自己的渲染器检查了你的例子,并试图检查你的回调。

在你的回调方法中替换

$this->getCollection()->getSelect()

$collection

以下是我的建议:


Magento 过滤器假定不是 100% 的相似性。我的意思是,如果你想应用过滤器,你应该使用构造:

where("cat_ids LIKE ?", "%$value%");

我还是不明白下面问题的答案:

定义不工作。回调是否被调用过?是回调 代码对结果没有影响? – Lee Saferite 1 小时前

Mage::log('blabla', false, 'grid.log', true);放在回调方法的开头。然后检查这个日志文件。如果它不为空 - 您的方法调用成功。


如果你的方法调用 - 尝试

Mage::log($collection->getSelectSQL(1), false, 'grid.log', true);

过滤器应用之前和之后。并尝试在 phpmyadmin 中运行这些查询。检查结果


尝试在 Mage_Adminhtml_Block_Sales_Order_Grid 类中应用这些更改

我做了什么:

    $this->addColumn("cat_ids", array(
        "header" => Mage::helper('sales')->__('Ship to Name'),
        "align" => "center",
        "index" => "grand_total",
        "renderer" => "My_Class_.....",
        "filter_condition_callback" => array($this, '_categoriesFilter')
    ));

....

protected function _categoriesFilter($collection, $column)
{
    if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }

    $collection->getSelect()->where(
        "status LIKE ?", "%$value%"
    );

    return $this;
}

...

public function render(Varien_Object $row)
{
    $value =  $row->getData('increment_id') . $row->getData('status');
    return $value;
}

【讨论】:

  • 如果我使用$collection 而不是$this->getCollection()->getSelect() 显示Fatal error: Call to undefined method Sathish_Listings_Model_Mysql4_Listings_Collection::where()
  • 我使用两个模型“listings/listings”和“categories/categories”
  • 如果您比较我和您的代码 - 您会看到,您将 $collection 发送到您的函数并且从不使用它。您将过滤器应用于另一个不使用的集合。我不确定,但我认为您的错误出现是因为您没有进行适当的继承。而且您的收藏没有 Zend 方法。而且您的问题与回调函数无关。您应该扩展 Mage_Core_Model_Mysql4_Collection_Abstract。也许试着听从我最后的建议。当您的回调开始工作时 - 您可以将其部分转移到您的模块中。我发布的变体是 100% 可行的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-13
相关资源
最近更新 更多