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.

Dev
  • 369
  • 1
  • 3
  • 18

3 Answers3

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

Bilal Usean
  • 9,977
  • 14
  • 75
  • 122
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();
Arshad Syed
  • 553
  • 5
  • 14
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.

Chirag Prajapati
  • 2,922
  • 2
  • 18
  • 42