Question

I want to add filter to grid. When I add filter to sales_billing_agreement, I got the following error.

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'store_id' in where clause is ambiguous, query was: SELECT COUNT(*) FROM `sp_sales_billing_agreement` AS `main_table`
 INNER JOIN `sp_customer_entity` AS `ce` ON ce.entity_id = main_table.customer_id
 LEFT JOIN `sp_customer_entity_varchar` AS `firstname` ON firstname.entity_id = main_table.customer_id AND firstname.entity_type_id = 1 AND firstname.attribute_id = '5'
 LEFT JOIN `sp_customer_entity_varchar` AS `middlename` ON middlename.entity_id = main_table.customer_id AND middlename.entity_type_id = 1 AND middlename.attribute_id = '6'
 LEFT JOIN `sp_customer_entity_varchar` AS `lastname` ON lastname.entity_id = main_table.customer_id AND lastname.entity_type_id = 1 AND lastname.attribute_id = '7' WHERE (`store_id` IN(2))

Here is the PHP code

$this->getCollection()->addFieldToFilter('store_id', array('in' => $storeIds))->load();

Due to the error above, I update the PHP code to this,

try {
        $this->getCollection()->addFieldToFilter('store_id', array('in' => $storeIds))->load();
        } catch (Zend_Db_Statement_Exception $e) {
            $this->getCollection()->addFieldToFilter('main_table.store_id', array('in' => $storeIds))->load();
    }

And then, the same error occurred, and I found that the previous filter is still there. Here is the new error.

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'store_id' in where clause is ambiguous, query was: SELECT COUNT(*) FROM `sp_sales_billing_agreement` AS `main_table`
 INNER JOIN `sp_customer_entity` AS `ce` ON ce.entity_id = main_table.customer_id
 LEFT JOIN `sp_customer_entity_varchar` AS `firstname` ON firstname.entity_id = main_table.customer_id AND firstname.entity_type_id = 1 AND firstname.attribute_id = '5'
 LEFT JOIN `sp_customer_entity_varchar` AS `middlename` ON middlename.entity_id = main_table.customer_id AND middlename.entity_type_id = 1 AND middlename.attribute_id = '6'
 LEFT JOIN `sp_customer_entity_varchar` AS `lastname` ON lastname.entity_id = main_table.customer_id AND lastname.entity_type_id = 1 AND lastname.attribute_id = '7' WHERE (`store_id` IN(2)) AND (`main_table`.`store_id` IN(2))

Is there a way to remove the previous filter before adding the new filter?

Thanks in advance.

Was it helpful?

Solution

Yes you can do with Zend_Db_Select

    $select = clone $collection->getSelect();
    $select->reset(Zend_Db_Select::COLUMNS);
    $select->reset(Zend_Db_Select::ORDER);
    $select->reset(Zend_Db_Select::LIMIT_COUNT);
    $select->reset(Zend_Db_Select::LIMIT_OFFSET);
    $where = $select->getPart(Zend_Db_Select::WHERE);
    foreach($where as $key=>$part) {
        if(strpos($part, "store_id") !== false) {

           // set your condition here with store id
            if($key == 0) {
                $where[$key] = "1";
            } else {
                unset($where[$key]);
            }

        }
    }
    $select->setPart(Zend_Db_Select::WHERE, $where);

you can remove $select->reset if it is not useful to you

let me know if anything i can help you more in this.

OTHER TIPS

Delete the entries of the respected grid on which filters are applied from ui_bookmark table.

Hope it will fix the issue

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top