0

I have a custom module with named A and db tab is z ok. now question is my:

  1. How to add child table? which is already I have done with PK and FK key constrains.

  2. How to add data in created child table using model?

1
  • what you have tried so far?
    – Amit Bera
    Feb 10, 2022 at 17:45

1 Answer 1

0

Please follow the below code.

    protected $_orderTemplate;
    protected $_orderTemplateItem;
   
    public function __construct(
      .....
        \Bhavesh\OrderTemplate\Model\OrderTemplate $orderTemplate,
        \Bhavesh\OrderTemplate\Model\OrderTemplateItem $orderTemplateItem,
      ....
    ) {
        .....
        $this->_orderTemplate = $orderTemplate;
        $this->_orderTemplateItem = $orderTemplateItem;
        .....
    }


   public function execute()
    {
         // Parent table
          $template_name = $templateName."-".date('M Y');
          $CartTotalQty=10;
          $CartGrandTotal=1000;
          $CartSubtotal=1100;
          $orderTemplateModel->setTemplateName($template_name)
           ->setOrderSubtotal($CartSubtotal)
           ->setOrderGrandtotal($CartGrandTotal)
           ->setOrderedQty($CartTotalQty)
           ->setCustomerId($_customerid);
          $orderTemplateModel->save(); 
           // parent table id    
          $templateID = $orderTemplateModel->getTemplateId();
          // Child Table store in Product data   
          $orderTemplateItemModel = $this->_orderTemplateItem;
          $orderTemplateItemModel->setTemplateId($templateID)
                     ->setProductName($product->getName())
                     ->setProductId($product->getProductId())
                     ->setProductSku($product->getSku())
                     ->setProductQty($product->getQty())
                     ->setProductPrice($product->getPrice());
          $orderTemplateItemModel->save();;
    }

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.