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

asked Apr 25, 2016 at 15:48
2
  • @Dinesh Saini I follow your way but I get a problem to store form data to database, suggest me what I miss this code. Commented Apr 26, 2016 at 3:48
  • clear cache and try again Commented Apr 28, 2016 at 4:37

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.