Read More
Read More
Menu Close

    Magento2 mass update records without loading model

    Magento2 mass update records without loading model – As developer many times we needs to update table records in a bulk, and for this if you will get collection factory and save records inside foreach then it’s a bad practice. So here is the solution to update records without saving inside foreach by following some easy steps.

    1. Write update records method in your model collection page. Like you want to upgrade “status” column value in your “custom_records” table, so you will need to write these methods in your collection model path will be like – magento_root/app/code/Webkul/Custom/Model/ResourceModel/CustomRecords/Collection.php
      namespace Webkul\Custom\Model\ResourceModel\CustomRecords;
      use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
       * Webkul Custom ResourceModel CustomRecords collection
      class Collection extends AbstractCollection
           * Store manager
           * @var \Magento\Store\Model\StoreManagerInterface
          protected $storeManager;
           * @param \Magento\Framework\Data\Collection\EntityFactoryInterface     $entityFactory
           * @param \Psr\Log\LoggerInterface                                      $logger
           * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface  $fetchStrategy
           * @param \Magento\Framework\Event\ManagerInterface                     $eventManager
           * @param \Magento\Store\Model\StoreManagerInterface                    $storeManager
           * @param \Magento\Framework\DB\Adapter\AdapterInterface|null           $connection
           * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb|null     $resource
          public function __construct(
              \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
              \Psr\Log\LoggerInterface $logger,
              \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
              \Magento\Framework\Event\ManagerInterface $eventManager,
              \Magento\Store\Model\StoreManagerInterface $storeManager,
              \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
              \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
          ) {
              $this->storeManager = $storeManager;
           * @var string
          protected $_idFieldName = 'entity_id';
           * Define resource model
           * @return void
          protected function _construct()
              $this->_map['fields']['entity_id'] = 'main_table.entity_id';
           * Update Data for given condition for collection
           * @param int|string $limit
           * @param int|string $offset
           * @return array
          public function setTableRecords($condition, $columnData)
              return $this->getConnection()->update(
                  $where = $condition
    2. Now call setTableRecords() method with parameters as explained-
      $condition = "`entity_id`= 1 OR `entity_id`= 2"; //any record id with OR operator
      $status = 1;
          $condition, ['status' => $status]

      I fetched table collection using object manager to explain better but you can use collection factory.

    So in this way you can update any column records without loading collection for each record.

    Searching for an experienced
    Magento 2 Company ?
    Read More
    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


  • Tomas Jindal
    It is giving an error Call to undefined method Collection::update().
  • Back to Top