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.

7

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

| improve this answer | |
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.

| improve this answer | |
  • Thanks, I am not looking for this, Please check query. I want multiple where condition for update record – Dev Nov 17 '16 at 6:14
  • you can use addFieldToFilter as many time as you want. – chirag Nov 17 '16 at 6:17
0
 <?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();
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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