2

i have my custom module.i have to update multiple rows.if we follow the normal magento process i have to iterate through loop and update each at each time.so if there are large number of rows to update then it takes very long time to iterate the loop

foreach($row as $row)
{
/*  $update=$this->companyFactory->create();
                    $update->load($divaid,"divauniqueid");
                    $update->setStatus($statusid);
                    $update->save();*/
}

so i want use direct update query instead of iterate loop.which is the good .please help with this

1 Answer 1

1

Instead of writing direct sql queries, you can use the Resource model and write the code like following,

 $data = [
          'status' => 1,
         ];
    $connection->update($table, $data, ['entity_id = ?' => $model->getId()]);
5
  • 1
    can you post any full smaple code if you have..and can i write this model folder..how to get that $connection variable
    – vijay b
    Commented Jan 25, 2017 at 9:26
  • what if we want to do same operation on customer table.customer details doesn't save in one single table.so what we have to include in place of $talbe
    – vijay b
    Commented Jan 25, 2017 at 11:56
  • For sample code you can refer in Catalog and Sales core modules. Which has more real time flow
    – seeni
    Commented Jan 25, 2017 at 12:18
  • Yeah..I got the syntax..thank you..if u know can pls look at my second comment
    – vijay b
    Commented Jan 25, 2017 at 12:34
  • customer tables are EAV model. So we can't update each tables data. using customer model if you save details it will be stored in the corresponding tables. better you can take a code example for customer core module.
    – seeni
    Commented Jan 25, 2017 at 15:02

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.