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.

    <?php
    /**
     * Vendor Desc..
     *
     * @category  Vendor
     * @package   Vendor_CustomerModule
     * @author    Vendor
     * @copyright Copyright (c) Vendor
     * @license   https://example.com/license.html
     */
    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
        ) {
            parent::__construct($context);
            $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();
            $customerPersonalInfo->setFirstName("Jack");
            $customerPersonalInfo->setLastName("D'souza");
            $customerPersonalInfo->setCode("Jack420");
            $customerPersonalInfo->setGender("male");
            $customerPersonalInfo->setDob("2000-10-10");
            
            //add customerPersonalInfo object for transaction
            $dbTransaction->addObject($customerPersonalInfo);
    
            //add information in customerWalletTrans model
            $walletTrans = $this->customerWalletTrans->create();
            $walletTrans->setCode("Jack420");
            $walletTrans->setTransType("credit");
            $walletTrans->setCurrentAmount(1000);
            $walletTrans->setTransAmount(1000);
            $walletTrans->setDesc("CREDIT: By admin");
            
            //add customerWalletTrans object for transaction
            $dbTransaction->addObject($walletTrans);
    
            //add information in customerWallet model
            $wallet = $this->customerWallet->create();
            $wallet->setCode("Jack420");
            $wallet->setTotalAmount(1000);
            $wallet->setUsedAmount(0);
            
            //add customerWallet object for transaction
            $dbTransaction->addObject($wallet);
    
            //add information in customerWallet model
            $reward = $this->customerRewards->create();
            $reward->setCode("Jack420");
            $reward->setTotalRewards(2000);
            $reward->setRemainingRewards(2000);
            $reward->setUsedRewards(0);
            $reward->setDesc("CREDIT: rewards assigned on registration");
            
            //add customerWallet object for transaction
            $dbTransaction->addObject($reward);
        
            $dbTransaction->save();
            return;
        }
    }

    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