6

I am loading a collection:

$model = $this->_objectManager->create('Test\Demo\Model\Book');
$collection = $model->getCollection();
$data = [regionCode => 111,regionCode=>222];
foreach ($data as $address) {
$collection->addFieldToFilter(
            'region', array(
                'eq' => $address['regionCode']
            )
        );
    if($collection->getSize()){ /* ex - 111 region available and 222 is not so both time collection getSize() - 1 */
       $model->setData('region',$address['regionCode']);
       $model->save();
       $collection->getSelect()->reset(\Magento\Framework\DB\Select::WHERE);
    }
}

How can I remove this (or all) filter from the collection again? I don't want to load a new collection every time the filter changes.

I tried also

$collection->resetData();

But that doesn't seem to have any effects on the filter.

2 Answers 2

4

Try this

protected $bookFactory;
public function __construct(..., Test\Demo\Model\BookFactory $bookFactory, ...) {
    $this->bookFactory = $bookFactory;
}

public function yourFunction() {
    $data = [regionCode => 111,regionCode=>222];
    foreach ($data as $address) {
    $colllection = $this->bookFactory->create()->getCollection();
    $result = $collection->addFieldToFilter(
                    'region', array(
                        'eq' => $address['regionCode']
                    )
                );
        if(count($result)){ /* ex - 111 region available and 222 is not so both time collection getSize() - 1 */
           $model->setData('region',$address['regionCode']);
           $model->save();
           $collection->getSelect()->reset(\Magento\Framework\DB\Select::WHERE);
        }
    }
}

Try to create singleton, might help you. lets know the result so that we can help more on this.

1
  • Not working above code already tried. Commented Jan 4, 2019 at 5:48
3

If you want to reset conditions then use

$collection->getSelect()->reset(\Magento\Framework\DB\Select::WHERE);

It will remove all where condition from Collection.

You have some logical issue

try below solution and see what Happen

$model = $this->_objectManager->create('Test\Demo\Model\Book');
$collection = $model->getCollection();
$data = [regionCode => 111, regionCode => 222];

foreach ($data as $address) {
    $cloneCollection = clone $collection;
    $cloneCollection->addFieldToFilter(
            'region', array(
        'eq' => $address['regionCode']
            )
    );
    if ($cloneCollection->getSize()) { /* ex - 111 region available and 222 is not so both time collection getSize() - 1 */
        $model->setData('region', $address['regionCode']);
        $model->save();
        // reset is not required
        //$collection->getSelect()->reset(\Magento\Framework\DB\Select::WHERE);
    }
    unset($cloneCollection);
}
8
  • I tried but this one is not working. Magento1 in seems like working fine @amit Commented Jan 3, 2019 at 9:21
  • after $collection->getSelect()->reset(\Magento\Framework\DB\Select::WHERE); add $collection->load() and check what happen?
    – Amit Bera
    Commented Jan 3, 2019 at 9:28
  • not working both code dear Commented Jan 3, 2019 at 9:30
  • every stange,please share full code what you have tried
    – Amit Bera
    Commented Jan 3, 2019 at 9:31
  • If you donot want load collection then you can use $collection2 = clone $collection and then use $collection2->getSelect()->reset(\Magento\Framework\DB\Select::WHERE); and check what happen?
    – Amit Bera
    Commented Jan 3, 2019 at 9:32

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.