Magento后台Grid出现Invalid attribute error

有时,在将产品(例如后台评论列表)与其他表连接之后,在过滤或排序过程中,管理面板网格中可能会遇到错误 “Invalid attribute error”。

假设如下:
其中$collection是商品的集合

protected function _prepareCollection()
{
    $model = Mage::getModel('review/review');
    $collection = $model->getProductCollection();
	$collection->getSelect()
        ->join(array('rov' => $ratingVoteTable),
            'rt.review_id = rov.review_id',
            array('rov.percent','rov.value'));

    $this->setCollection($collection);
    return parent::_prepareCollection();
}

Grid中的column展示如下:

$this->addColumnAfter('value',
   array(
        'header'=> Mage::helper('catalog')->__('Rating Value'),
        'width' => '60px',
        'index' => 'value',
        'filter_index' => 'rov.value',
        "renderer" => "fun_review/adminhtml_review_grid_render_rating",
        'type'  => 'text',
    ),'detail');

这时,如果对Rating Value列进行搜索过滤时,会出现“Invalid attribute error”的错误,这是因为magento认为value正在调用商品的属性,但是由于value不是商品的属性,因此会显示错误。

因此,需要在代码中加点东西,如下:

$this->addColumnAfter('value',
     array(
         'header'=> Mage::helper('catalog')->__('Summary Rating'),
         'width' => '60px',
         'index' => 'value',
         'filter_index' => 'rov.value',
         "renderer" => "fun_review/adminhtml_review_grid_render_rating",
         'type'  => 'text',
         'filter_condition_callback' => array($this, 'filterValue'),
     ),'detail');

这里调用了一个函数

用于过滤

protected function filterValue($collection, $column)
{
    if ($column->getFilter()->getValue() === null) {
        return;
    }
    $collection->getSelect()->where('rov.value = ?', $column->getFilter()->getValue());
}

这样当对value进行过滤的时候,就会调用这个函数,然后就可以使用函数中的SQL语句进行过滤了,这个方法就写在Grid中直接调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值