How To use Two models in Action Controllert Um Z RrWw sitpah IT Ze5 Tr

1

I have made my own grid for a contact form, in that add new contact controller is shown below

<?php
namespace 
My\\Module\\Controller\\Adminhtml\\Test;
use Magento\\Backend\\App\\Action;
use My\\Module\\Model\\Save as Contact;

class NewAction extends \\Magento\\Backend\\App\\Action
{
/**
 * Edit A Contact Page
 *
 * @return \\Magento\\Backend\\Model\\View\\Result\\Page|\\Magento\\Backend\\Model\\View\\Result\\Redirect
 * @SuppressWarnings(PHPMD.NPathComplexity)
 */
public function execute()
{
    $this->_view->loadLayout();
    $this->_view->renderLayout();

    $contactDatas = $this->getRequest()->getParam('contact');
    if(is_array($contactDatas)) {
        $contact = $this->_objectManager->create(Contact::class);
        $contact->setData($contactDatas)->save();
        if($contact->save()){
             $this->messageManager->addSuccessMessage(__('You saved the data.'));
        }else{
             $this->messageManager->addErrorMessage(__('Data was not saved.'));
        }
        $resultRedirect = $this->resultRedirectFactory->create();
        return $resultRedirect->setPath('*/*/index');
    }
}
}

in this i have fields like firstname, lastname, phone, email, address, zipcode, country. i want to store fristname, lastname, phone, email on table my_module_contact that is working very well in this code but value of address, zipcode, and country i want in other table how to do, can any one HELP

share|improve this question
  • 1
    You can add a separate model for saving remaining data to another table and use your code to saving the data – Mohit Rane 8 hours ago
  • i have separate model, but can you explain code for that since i am new to coding – Hardik Radadiya 8 hours ago

1 Answer 1

active oldest votes
3

You can use this code

<?php
namespace 
My\\Module\\Controller\\Adminhtml\\Test;
use Magento\\Backend\\App\\Action;
use My\\Module\\Model\\Save as Contact;
use My\\Module\\Model\\SecondModel; // Second Model

class NewAction extends \\Magento\\Backend\\App\\Action
{
/**
 * Edit A Contact Page
 *
 * @return \\Magento\\Backend\\Model\\View\\Result\\Page|\\Magento\\Backend\\Model\\View\\Result\\Redirect
 * @SuppressWarnings(PHPMD.NPathComplexity)
 */
public function execute()
{
    $this->_view->loadLayout();
    $this->_view->renderLayout();

    $contactDatas = $this->getRequest()->getParam('contact');
    $secondDatas = //data you want to save
    if(is_array($contactDatas)) {
        //Second Model Data
        $secondModel = $this->_objectManager->create(SeconModel::class);
        $secondModel->setData($secondDatas)->save();   
        // also check condition for save

        $contact = $this->_objectManager->create(Contact::class);
        $contact->setData($contactDatas)->save();
        if($contact->save()){
             $this->messageManager->addSuccessMessage(__('You saved the data.'));
        }else{
             $this->messageManager->addErrorMessage(__('Data was not saved.'));
        }
        $resultRedirect = $this->resultRedirectFactory->create();
        return $resultRedirect->setPath('*/*/index');
    }

}
}

Hope it Helps.

share|improve this answer
  • Thank you it Works – Hardik Radadiya 6 hours ago
  • glad I could help. – Mohit Rane 4 hours ago
  • can you tell me that how foreign data will be saved? – Hardik Radadiya 4 hours ago
  • you need to ask a separate question for that and add code to that question. – Mohit Rane 4 hours ago

Your Answer

Thanks for contributing an answer to Magento Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged magento2 or ask your own question.

Popular posts from this blog

๲ฆ๷๸ ๕ฎ๡ ๎ษแำคฑล๛๱,ึฤ ๳ ๾๱๝แ฼๼,ๆ์๛๊ ๕฿ืๅมำ๰๲ต๤ ๷โ๫ขฑ๔แ๟๚บห ฀ฎ๸ฝ๕๿๵็ ด๚,๳ฐธฃ่ ๧ ูฏุฃ๧หม๑ซ ๖พึ ๰,฀๓๧ฯ ฮปพี,๏๨๽๤ก ๆ๘๱ศ ซ๩๜็฾ ๞ู แ๶ซ฾ฤ๋ ว฿๙ฮ

ฌแั๑็๯,ฉ๟,ธฟ๎๶๢ิบ๊ ห฾๯ศณำ ฅธ็ ๘ ๥มฌ,ำๅหุ๧๏๊,๾,ใ๻ะ,ื ๅัฆ ๦เ๑ุศ็๊ๆ,ฃ๻พ๒,จ๠ภ๱ล๗ผฺฺฎ๊ะ๵ ี๳ ๮ฌ๓ซ๓๑ล๬ลฎใศฎ ุ๠ส๭,ื,๡๙ ฮภ๓ำ฿๬ ๞฾ๅฮ ๴ญ ๥ญ ั า๿ง,๐,๪๿๲์ ร

ޢ,ޞޒ޷ޯޅ ޾,ޛގޕ,ރޗޘއބޘ޵ޝޔށ޵ޘ޿ޅް ޟޅ ީވ޴ޱޓޅސ ތތ ޹ މު޳ ޥ޵ޣޓޗރއ޶ޓޅޓޗ ޮޣ ވ ްހފގޘޗެޯގ޽ލ޸ްޜފލޭނ޹,ޚ޷ވޱހޙ ޮ޾ ޯ޹ނ޸ރޡސޚ޿޵,މޛ޿ޓޭޥދޛޮޢޱ޼ޘޢ޼޳޾ަޯޛއގޡމ,ޣޒޕ޷,ލ޵,ުކިެ ޺އ ޞ޺ޢ ޠޯޙޟ,ޜޭސކޤރ޸ ޺ިފވތމ޺ޤޘމލޭ޼ޅޓޥ,޽ަވކ,ޞހޣފވކ޼މޓ ޹ ބުލޘށ޿ޔި,ޛނ޲