I have 2 custom table and from admin side I want to save few field values to these 2 custom table while clicking Save button. For example in adminside I have Status option and Message option. The Status option value need to be in custom table 1 and message in custom table 2. How can I do this in Magento 2. I have Save.php file in Controller/Adminhtml/Manage folder.
Asked
Active
Viewed 1,184 times
2
-
Have you created two models for two tables ? – Murtuza Zabuawala Aug 17 '17 at 12:04
-
Yes I have created 2 models. – Bridgit Thomas Aug 17 '17 at 12:05
1 Answers
1
Your controller constructor would be like
public function __construct(Action\Context $context,Package\Module\Model\Model1 $model1 , Package\Module\Model\Model2 $model2)
{
$this->model1 = $model1;
$this->model2 = $model2;
parent::__construct($context);
}
public function execute()
{
$data = $this->getRequest()->getPostValue();
$this->model1->setData('fieldname',$data['status']);
$this->model1->save();
$this->model2->setData('fieldname',$data['messageOption']);
$this->model2->save();
}
Murtuza Zabuawala
- 14,814
- 9
- 44
- 75