0

I need to store form data to data base,But, my code not working for me, this is my code:

<?php

namespace ChennaiBox\CustomerAccount\Controller\Profile;

use Magento\Customer\Api\CustomerRepositoryInterface as  CustomerRepository;


class Save extends \Magento\Framework\App\Action\Action
 {
 protected $formKeyValidator;

protected $storeManager;

protected $customerRepository;

protected $subscriberFactory;


public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    CustomerRepository $customerRepository,
    \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
) {
    $this->storeManager = $storeManager;
    $this->formKeyValidator = $formKeyValidator;
    $this->customerRepository = $customerRepository;
    $this->subscriberFactory = $subscriberFactory;
    $this->customerSession = $customerSession;
    parent::__construct($context, $customerSession);
}


public function execute()
{


    $cutomerEmail =(string)$this->customerSession->getCustomer()->getEmail();


    if ($cutomerEmail === null) {
        $this->messageManager->addError(__('Something went wrong while saving your subscription.'));
    } else {
        try {   


                //$data = $this->getRequest()->getParams();
                $post = $this->getRequest()->getPostValue();


               $model = $this->_objectManager->create('ChennaiBox\CustomerAccount\Model\Format');

                $model->setData('customerid', $cutomerEmail);
                $model->setData('format', $post['type']);
                $model->save();

                $this->messageManager->addSuccess(__('Profile has beem saved successfully.'));

                $this->_redirect('customer/account/index/');

                return;

            } catch (\Exception $e) {
                $this->messageManager->addError(__('Something went wrong while saving your profile.'));
            }
    }
    $this->_redirect('customer/account/');
}
}

This is my model files:

  <?php

 namespace ChennaiBox\CustomerAccount\Model;

class Format extends \Magento\Framework\Model\AbstractModel
{
public function __construct(
    \Magento\Framework\Model\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Framework\Model\Resource\AbstractResource $resource = null,
    \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
    array $data = []
    ) {
     parent::__construct($context, $registry, $resource,              $resourceCollection,         $data);
 }

  public function _construct()
  {
   $this->_init('ChennaiBox\CustomerAccount\Model\ResourceModel\Format');
   }
 }

This is my ResourceModel File:

  <?php
  namespace ChennaiBox\CustomerAccount\Model\ResourceModel;

 class Format extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
/**
 * Initialize resource model
 *
 * @return void
 */
protected function _construct()
{
    $this->_init('email_format','id');
  }
 }

This is ResourceModel collection File:

 <?php
namespace ChennaiBox\CustomerAccount\Model\ResourceModel\Format;

class Collection extends \Magento\Framework\Model\ResourceModel   \Db\Collection\AbstractCollection
 {
public function _construct()
{
    $this->_init('ChennaiBox\CustomerAccount\Model\Format',    'ChennaiBox\CustomerAccount\Model\ResourceModel\Format');
}
}

This is my Template File:

  <form method="post" action="<?php echo    $this->getUrl('customeraccount/profile/save'); ?>">
 <input type="radio" name="type" value="html"> Html<br/>
 <input type="radio" name="type" value="text"> Text<br/><br/><br/>
 <input type="submit" class="button" name="submit" value="Submit">

Now, I getting Object Manager object then, now, I put value to table based on column, but its not working..

            $model->setData('customerid', $cutomerEmail);
            $model->setData('format', $post['type']);
            $model->save();

above code not working for me, magento 2 any other way to save value to table in database

  • @Dinesh Saini I follow your way but I get a problem to store form data to database, suggest me what I miss this code. – Rajkumar .E Apr 26 '16 at 3:48
  • clear cache and try again – ND17 Apr 28 '16 at 4:37

Your Answer

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

Browse other questions tagged or ask your own question.