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中直接調用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章