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

  • 1
    You can add a separate model for saving remaining data to another table and use your code to saving the data – Mohit Rane Aug 14 at 8:08
  • i have separate model, but can you explain code for that since i am new to coding – Hardik Radadiya Aug 14 at 8:09
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.

  • Thank you it Works – Hardik Radadiya Aug 14 at 10:15
  • glad I could help. – Mohit Rane Aug 14 at 11:40
  • can you tell me that how foreign data will be saved? – Hardik Radadiya Aug 14 at 11:49
  • you need to ask a separate question for that and add code to that question. – Mohit Rane Aug 14 at 12:00

Your Answer

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 or ask your own question.