网格列过滤器不使用magento自定义模块中的渲染器

时间:2016-02-02 12:33:41

标签: php magento filter grid adminhtml

我正在使用magento后端的自定义模块,此过滤器在使用过滤器calback时无法正常工作!谢谢!

我尝试过这样的代码,

Grid.php

protected function _prepareCollection() 
    {        
     $collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('sku')
            ->addAttributeToSelect('name');

     $this->setCollection($collection);

     return parent::_prepareCollection();        
    }

protected function _prepareColumns() {
    $this->addColumn('entity_id', array(
        'header' => Mage::helper('catalog')->__('ID'),
        'width' => '50px',
        'type' => 'number',
        'index' => 'entity_id',
    ));

    $this->addColumn('name', array(
        'header' => Mage::helper('catalog')->__('Name'),
        'index' => 'name',
    ));

    $this->addColumn('sku', array(
        'header' => Mage::helper('catalog')->__('SKU'),
        'width' => '120px',
        'index' => 'sku',
    ));

    $this->addColumn('packet_associate', array(
        'header'    => Mage::helper('catalog')->__('Packets Associated'),
        'width'     => '80px',
        //'index'     => 'packet_associate',
        'filter_index' => 'packet_associate',
        'renderer'  => 'stockmanagement/adminhtml_productassociate_renderer_associatestatus',
        'type'      => 'options',
        'options'   => array('1' => 'Yes', '0' => 'No'),
        'filter_condition_callback'
                    => array($this, '_filterPacketAssociateCondition'),
    ));

    $this->addColumn('action', array(
        'header' => Mage::helper('catalog')->__('Action'),
        'width' => '200px',
        'type' => 'action',
        'getter' => 'getId',
        'actions' => array(
            array(
                'caption' => Mage::helper('catalog')->__('Associate / Edit Packets'),
                'url' => array(
                    'base' => '*/*/new',
                    'params' => array('store' => $this->getRequest()->getParam('store'))
                ),
                'field' => 'id'
            )
        ),
        'filter' => false,
        'sortable' => false,
        'index' => 'stores',
    ));

    return parent::_prepareColumns();
}

回调功能

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

    $collection->getSelect()->joinLeft('custom_table AS b', 'e.entity_id = b.product_id', 
            array('packet_associate' => 'COUNT(b.id)')); 
    $collection->getSelect()->group(entity_id);

    $collection->getSelect()->having(
            "packet_associate = ?"
            , $value);

    return $collection;
    //var_dump($collection->getSelect()->__toString());die();

}

Associatestatus.php

class   Module_Block_Adminhtml_Productassociate_Renderer_Associatestatus     extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{    
public function render(Varien_Object $row)
{        
    $collection = Mage::getModel("stockmanagement/productassociate")->load($row->getId(), 'product_id')->getData();
    $resultCount = count($collection);
    if($resultCount > 0){
        echo 'Yes';
    }else{
        echo 'No';
    }
}

}

在这个,Renderer工作正常。,只有过滤回调函数不起作用!!!

当我在回调函数中打印查询时,如下所示,它会给出正确的结果。

SELECT `e`.*, COUNT(b.id) AS `packet_associate` FROM `catalog_product_entity` AS `e` LEFT JOIN `custom_table` AS `b` ON e.entity_id = b.product_id GROUP BY `entity_id` HAVING (packet_associate = '1')

但是在应用过滤器之后会出错。请建议我。谢谢!

2 个答案:

答案 0 :(得分:0)

评论有限,所以我把它放在答案框中。

尝试设置

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

$this->getCollection()->getSelect()->joinLeft('custom_table AS b', 'e.entity_id = b.product_id', 
        array('packet_associate' => 'COUNT(b.id)')); 
$this->getCollection()->getSelect()->group(entity_id);

$this->getCollection()->getSelect()->having(
        "packet_associate = ?"
        , $value);

    return $this;
}

代替:

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

$collection->getSelect()->joinLeft('custom_table AS b', 'e.entity_id = b.product_id', 
        array('packet_associate' => 'COUNT(b.id)')); 
$collection->getSelect()->group(entity_id);

$collection->getSelect()->having(
        "packet_associate = ?"
        , $value);

    return $collection;
}

答案 1 :(得分:0)

如果我理解正确,您希望过滤custom_table中包含/未记录对应记录的产品。 在这种情况下,没有必要对结果进行分组 - 这是一个有点繁重的操作,让我们试着避免它。你的回调函数应该如下所示:

protected function _filterPacketAssociateCondition($collection, $column)
{
    $value = $column->getFilter()->getValue();

    $collection->getSelect()->joinLeft(array('b' => 'custom_table'), 'e.entity_id = b.product_id',
        array('product_id'));

    if ($value > 0) {
        $collection->getSelect()->where("b.product_id IS NOT NULL");
    } else {
        $collection->getSelect()->where("b.product_id IS NULL");
    }

    return $this;
}

正如您所看到的,如果当前产品在product_id中至少有一个对应的行,则在左侧联接中,如果已加入custom_table列,则会有一个整数值,否则您将拥有{ {1}}在此列中。