2017-04-07 78 views
0

我已经创建了Magento Admin模块,网格和字段具有自定义过滤器。有Magento管理网格字段自定义过滤器问题

$this->addColumn('diff', array(
     'header' =>'Diff.', 
     'align'  =>'left', 
     'type' => 'number', 
     'index'  =>'diff', 
     'filter_condition_callback' => array($this, '_diffFilter'), 
    )); 

收集小组通过如下:

$collection->getSelect()->group(array('main_table.order_id'));

自定义过滤功能如下:

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

    if($_filter_data["from"]!=''){ 
     $collection->getSelect()->having('ROUND((main_table.base_cost-main_table.base_price)*100/main_table.base_cost) >= ?', $_filter_data["from"]);   
    } 

    if($_filter_data["to"]!=''){ 
     $collection->getSelect()->having('ROUND((main_table.base_cost-main_table.base_price)*100/main_table.base_cost) <= ?', $_filter_data["to"]); 
    } 

    return $this; 
} 

使用此功能,如果我加载管理网格它抛出以下错误: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'main_table.base_cost' in 'having clause'

但我抓住选择查询$collection->getSelect()使用这个,然后直接运行到MySQL然后,它工作正常,但它只从Magento抛出错误。

我做了很多研究,但它根本不适用于Magento。

回答

0

HAVING子句用于过滤分组查询的结果。如果你想在表中筛选列,使用WHERE条款:

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

    if($_filter_data["from"]!=''){ 
     $collection->getSelect()->where('ROUND((main_table.base_cost-main_table.base_price)*100/main_table.base_cost) >= ?', $_filter_data["from"]);   
    } 

    if($_filter_data["to"]!=''){ 
     $collection->getSelect()->where('ROUND((main_table.base_cost-main_table.base_price)*100/main_table.base_cost) <= ?', $_filter_data["to"]); 
    } 

    return $this; 
} 
+0

对不起,它有SQL组查询所以这就是为什么我使用having子句,请参阅上述更新的问题。 –

+0

您尝试执行的过滤在“HAVING”子句中仍然不合适。请列出您要制作的完整查询。另外,如果您查看堆栈跟踪,加载数据或计算结果时产生的错误是什么? –

相关问题