3

How can I update record with multiple where clause? I know below method to update record with single where condition. I tried to used addFieldToFilter() but not succeeded.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order_ = $objectManager->create('<ext_name>\<module_name>\Model\OrderSaleslist')
                                       ->load($orderId); 
$order_->setSellerOrderConfirm("dispatched");
$order_->save(); 

I want to apply below query.

Update ordersaleslist set seller_order_confirm = 'dispatched', payment_mode = 'COD' where order_id = 1 and product_id = 20 and (status !='' OR dispatched_status!='');

Thanks in Advance.

3 Answers 3

8

you can apply multiple where with AND logic

e.g

    $collections = $this->_yourFactory->create()->getCollection()
                 ->addFieldToFilter('order_id', array('eq' => '1'))
                 ->addFieldToFilter('product_id', array('eq' => '20'));
    foreach($collections as $item)
    {
        $item->setSellerOrderConfirm('dispatched');
        $item->setPaymentMode('COD');
    }
    $collection->save();

object manager is not recommended. load and save methods are deprecated for best practice you should use service contracts. for more info refer this link

0
1
 <?php
namespace Vendor\MyModule\Model;

use Magento\Framework\Model\AbstractModel;
 class Products extends AbstractModel
    {
        protected function _construct()
        {

            $this->_init(\Vendor\MyModule\Model\ResourceModel\Products::class);
        }
        public function setProduct($newProductId){    
           $collection = $this->getCollection()->addFieldToFilter('product_id', $);
           $collection->getFirstItem()->setData('product_id',$newProductId)->save();
        }
}

I update product Id using collection in magento2 I called setProduct() from the controller.

main logic to update is

 $collection = $this->getCollection()->addFieldToFilter('product_id', $);
               $collection->getFirstItem()->setData('product_id',$newProductId)->save();
0
0

You have used two time

$order_->setSellerOrderConfirm("dispatched")->setSellerOrderConfirm("dispatched")

just use once $order_->setSellerOrderConfirm("dispatched").

if not work then you can use like this,

$order_->addFieldToFilter('order_id ', array('eq' => 1));

May be it will help you.

2
  • Thanks, I am not looking for this, Please check query. I want multiple where condition for update record
    – Dev
    Nov 17, 2016 at 6:14
  • you can use addFieldToFilter as many time as you want. Nov 17, 2016 at 6:17

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.