有时,在将产品(例如后台评论列表)与其他表连接之后,在过滤或排序过程中,管理面板网格中可能会遇到错误 “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中直接调用