1

I'm trying to update an existing collection where clause in _beforeLoad() function. The collection can be filtered on many attributes and I just want to replace the customer_id clause. I was able to clear all where clauses by using the following code but I just want to clear the customer_id condition :

$this->clear()->getSelect()->reset(\Zend_Db_Select::WHERE);

Thanks !

1
  • Have you got solution of this? Commented Mar 23, 2022 at 17:23

1 Answer 1

1

I do not think you can reset at _beforeLoad method in your collection.

You may need to reset the clause at renderFiltersBefore() method.

protected function _renderFiltersBefore()
{  
    $this->getSelect()->reset(\Magento\Framework\DB\Select::WHERE);
}

As you want to remove one of field filter then have to work to at _renderFilters

 protected function _renderFilters()
 {
        if ($this->_isFiltersRendered) {
            return $this;
        }

        $this->_renderFiltersBefore();

        foreach ($this->_filters as $filter) {
        if($filter['field'] == 'customer_id'){
            continue;
        }   
            switch ($filter['type']) {
                case 'or':
                    $condition = $this->_conn->quoteInto($filter['field'] . '=?', $filter['value']);
                    $this->_select->orWhere($condition);
                    break;
                case 'string':
                    $this->_select->where($filter['value']);
                    break;
                case 'public':
                    $field = $this->_getMappedField($filter['field']);
                    $condition = $filter['value'];
                    $this->_select->where($this->_getConditionSql($field, $condition), null, Select::TYPE_CONDITION);
                    break;
                default:
                    $condition = $this->_conn->quoteInto($filter['field'] . '=?', $filter['value']);
                    $this->_select->where($condition);
            }
        }
        $this->_isFiltersRendered = true;
        return $this;
    }
3
  • Thanks for your reply but my code $this->clear()->getSelect()->reset(\Zend_Db_Select::WHERE); is working great. The issue is this code reset all where conditions I only want to reset customer_id clause.
    – magentodev
    Commented Sep 10, 2018 at 12:58
  • I tried using your _rendersFilters method but when I display the collection select I'm still get the customer_id condition : WHERE (product_id = '11') AND (customer_id = '71') because $this->_filters is empty when the function is called
    – magentodev
    Commented Sep 10, 2018 at 13:15
  • I donot think it will create issue
    – Amit Bera
    Commented Sep 10, 2018 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.