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

Php 网格列过滤器不适用于magento自定义模块中的渲染器,php,magento,filter,grid,adminhtml,Php,Magento,Filter,Grid,Adminhtml,我正在使用magento后端的自定义模块,在使用filter calback时,此筛选器不起作用!谢谢 我试过这样的代码 Grid.php protected function _prepareCollection() { $collection = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('sku')

我正在使用magento后端的自定义模块,在使用filter 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();
}
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';
    }
}
回调函数

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

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();
}
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';
    }
}
}

在这种情况下,渲染器工作正常。只有过滤器回调函数不工作

当我在下面的回调函数中打印查询时,它给出了正确的结果

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')
但在应用过滤器后,会出现错误。请给我任何建议。谢谢

评论是有限的,所以我把它放在回答框中

尝试设置

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;
}
而不是:

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;
}

如果我理解正确,您希望筛选在
自定义_表
中有/没有对应记录的产品。 在这种情况下,没有必要对结果进行分组-这是一个有点繁重的操作,让我们尽量避免它。您的回调函数应该如下所示:

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;
}

如您所见,如果当前产品在
自定义\u表中至少有一个对应行,则在左侧联接中,如果联接的
产品id
列中有一个整数值,否则此列中将有
NULL

是否可以通过应用筛选器提供抛出的错误?确定。以下是错误:SQLSTATE[42S22]:未找到列:1054“having子句”中的未知列“packet\u associate”,查询是:从
catalog\u product\u entity
AS
e
having(packet\u associate='1')中选择COUNT(不同的e.entity\u id),在回调函数中抛出的错误查询和打印的查询都是不同的。错误中的查询与预期的查询不同。在您的错误中:'SELECT COUNT(DISTINCT e.entity_id)FROM catalog_product_entity AS e HAVING(packet_associate='1')是您期望的:'SELECT
e
*,COUNT(b.id)作为
packet\u-associate
来自
catalog\u-product\u-entity
AS
e
LEFT-JOIN
custom\u-table
AS
b
e.entity\u-id=b.product\u-id分组依据
entity\u-id
拥有(packet\u-associate='1')是的,我不明白发生的原因。