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
magento2
-
1You 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
add a comment |
1 Answer
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.
-
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
add a comment |