简体   繁体   English

Magento Admin网格字段自定义过滤器问题

[英]Magento Admin Grid Field Custom filter issue

I have created Magento Admin module with Grid and field having custom filter. 我创建了带有网格和具有自定义过滤器的字段的Magento管理模块。

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

Collection having group by as below: 具有以下分组的集合:

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

Custom Filter function as below: 自定义过滤器功能如下:

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

Using this function if i load admin grid it's throwing below error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'main_table.base_cost' in 'having clause' 如果我加载管理网格,则使用此函数会抛出以下错误: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'main_table.base_cost' in 'having clause'

But i grab select query $collection->getSelect() using this and then run to MySQL directly then, it is working fine, but it only throwing error from Magento. 但是我使用这个抓取选择查询$collection->getSelect()然后直接运行到MySQL,它工作正常,但是只会从Magento抛出错误。

I did lots of research but it's not working at all with Magento. 我做了很多研究,但与Magento根本没有用。

HAVING clauses are used to filter the results of a grouped query. HAVING子句用于过滤分组查询的结果。 If you want to filter for columns in the table, use a WHERE clause: 如果要过滤表中的列,请使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM