0

In NewAction.php I am saving data in two different tables both have a different model for new contact it's working well, but while editing existing data it's updating first table row and creating a new row in the second table. here in my code "con_id" is the foreign key

 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();
        $id = $contact->getId();
        $contactt = $this->_objectManager->create(Contactt::class);
        $contactt->setData('con_id', $id);
        $contactt->setData('address', $contactDatas['address']);
        $contactt->setData('zipcode', $contactDatas['zipcode']);
        $contactt->setData('country', $contactDatas['country']);
        $contactt->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');
    }
}
}
  • Based on "con_id" on second table load the primary ID (just like you did $id = $contact->getId();) and set in the data and then try. Also, direct use of ObjectManager is not a good practise in magento2. Please use consturctor based "DI" to create objects of these models. public function __construct(Contact $contact, Contactt $contactt){ $this->contact = $contact; $this->contactt = $contatt } and then try these objects to save data. – Rahul Anand Aug 20 at 5:57
  • yes i know but i am learning now so for practice its save my time, can you tell me how ti load primary ID – Hardik Radadiya Aug 20 at 6:01
0

Replace your code with this code

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();
            $id = $contact->getId();
            $contactt = $this->_objectManager->create(Contactt::class);
            $contactt->load($id);
            $contactt->setData('address', $contactDatas['address']);
            $contactt->setData('zipcode', $contactDatas['zipcode']);
            $contactt->setData('country', $contactDatas['country']);
            $contactt->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 solves your problem

  • please let me know if it works or require modification – ASQ Aug 20 at 6:37
  • Its creating new row in second table, and also con_id value is set for new contact is 0 – Hardik Radadiya Aug 21 at 5:23
  • hello can you help me???? – Hardik Radadiya Aug 22 at 10:30
  • you have the same problem? – ASQ Aug 22 at 10:37
  • yes, can you tell me how to add filter for second table – Hardik Radadiya Aug 22 at 10:39
0
<?php
namespace My\Module\Controller\Adminhtml\Test;
use Magento\Backend\App\Action;
use My\Module\Model\Save as Contact;
use My\Module\Model\Savee as Contactt;
use My\Module\Model\ResourceModel\Save\Add\Collection as listCollectionFactory;


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();
        $id = $contact->getId();
        $contactt = $this->_objectManager->create(Contactt::class);
        if(isset($contactDatas['id'])){
            $collection = $this->_objectManager->create(listCollectionFactory::class)->load();
            $collection->addFieldToFilter('con_id', ['eq' => $contactDatas['id']]);
            $adddata = $collection->getData()[0];
            if($adddata){
                $contactt->setData('id', $adddata['id']);    
            }
        }
        $contactt->setData('con_id', $id);
        $contactt->setData('address', $contactDatas['address']);
        $contactt->setData('zipcode', $contactDatas['zipcode']);
        $contactt->setData('country', $contactDatas['country']);
        $contactt->save();
        if($contact->save() && $contactt->save()){

             $this->messageManager->addSuccessMessage(__('You saved the data.'));
        }else{
             $this->messageManager->addErrorMessage(__('Data was not saved.'));
        }
        $resultRedirect = $this->resultRedirectFactory->create();
        return $resultRedirect->setPath('*/*/index');
    }
}
}

By doing this i got solution

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.