Menu Close
    Searching for an experienced Magento 2 Development Company ?

    Database transactions for multiple Models in Magento 2

    Hello Friends!

    In this blog, we are going to learn how we can make database transactions to save records in multiple tables using their model classes.
    For example, we have to save information of a customer in customer_personal_info, customer_wallet_transaction, customer_reward_points, customer_wallet database tables. So, we will create resource model and model classes for these tables.
    To insert the data in the database sequentially, we will add model objects in the instance of \Magento\Framework\DB\Transaction Class.

    For more information about database transactions and saving a number of records in the database table, you can check the following blogs:
    Working With Database Transactions In Magento2
    Optimization Tips For Magento and Magento2 – Part 2

    Here I have used model factory classes for saving the information. Let’s see in the following Helper class (app/code/Vendor/CustomModule/Helper/SaveCustomerInfo), how we create the model object and add it to the transaction object to do the database trasaction.

     * Vendor Desc..
     * @category  Vendor
     * @package   Vendor_CustomerModule
     * @author    Vendor
     * @copyright Copyright (c) Vendor
     * @license
    namespace Vendor\CustomerModule\Helper;
    class SaveCustomerInfo extends \Magento\Framework\App\Helper\AbstractHelper
         * @param \Magento\Framework\App\Helper\Context $context
         * @param \Vendor\CustomerModule\Model\CustomerPersonalInfoFactory $customerInfo
         * @param \Vendor\CustomerModule\Model\CustomerWalletFactory $customerWallet
         * @param \Vendor\CustomerModule\Model\CustomerWalletTransactionFactory $customerWalletTrans
         * @param \Vendor\CustomerModule\Model\CustomerRewardPointsFactory $customerRewards
         * @param \Magento\Framework\DB\TransactionFactory $dbTransactionF
        public function __construct(
            \Magento\Framework\App\Helper\Context $context,
            \Vendor\CustomerModule\Model\CustomerPersonalInfoFactory $customerInfo,
            \Vendor\CustomerModule\Model\CustomerWalletFactory $customerWallet,
            \Vendor\CustomerModule\Model\CustomerWalletTransactionFactory $customerWalletTrans,
            \Vendor\CustomerModule\Model\CustomerRewardPointsFactory $customerRewards,
            \Magento\Framework\DB\TransactionFactory $dbTransactionF
        ) {
            $this->customerInfo   = $customerInfo;
            $this->customerWallet = $customerWallet;
            $this->customerWalletTrans = $customerWalletTrans;
            $this->customerRewards = $customerRewards;
            $this->dbTransactionF  = $dbTransactionF;
         * Execute method to save information
         * @return void
        public function execute()
            $dbTransaction = $this->dbTransactionF->create();
            //add information in customerPersonalInfo model
            $customerPersonalInfo = $this->customerInfo->create();
            //add customerPersonalInfo object for transaction
            //add information in customerWalletTrans model
            $walletTrans = $this->customerWalletTrans->create();
            $walletTrans->setDesc("CREDIT: By admin");
            //add customerWalletTrans object for transaction
            //add information in customerWallet model
            $wallet = $this->customerWallet->create();
            //add customerWallet object for transaction
            //add information in customerWallet model
            $reward = $this->customerRewards->create();
            $reward->setDesc("CREDIT: rewards assigned on registration");
            //add customerWallet object for transaction

    Hope this will be helpful. Thanks 🙂

    Previous Blog: Use Zend_Mail_Protocol_Imap in Magento 2

    . . .
    Discuss on Helpdesk

    Leave a Comment

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

    Be the first to comment.

    Back to Top