2

I Need to store form data to database, I follow, model/ResourceModel way,

This below my controller code: SetData() method not working.

<?php
namespace ChennaiBox\CustomerAccount\Controller\Profile;
 class Save extends \Magento\Framework\App\Action\Action
 {
protected $storeManager;
protected $subscriberFactory;
protected $_objectManager;
public function __construct(
 \Magento\Framework\App\Action\Context $context,
 \Magento\Customer\Model\Session $customerSession, 
 \Magento\Store\Model\StoreManagerInterface $storeManager,
 \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
 \Magento\Framework\ObjectManagerInterface $objectManager
) {
 $this->storeManager = $storeManager;
 $this->subscriberFactory = $subscriberFactory;
 $this->customerSession = $customerSession;
 $this->_objectManager = $objectManager;
 parent::__construct($context, $customerSession);
}
/**
 * Save newsletter subscription preference action
 *
 * @return void|null
 */
public function execute()
{
 $cutomerEmail =(string)$this->customerSession->getCustomer()->getEmail();
 if ($cutomerEmail === null) {
 $this->messageManager->addError(__('Something went wrong while saving your subscription.'));
 } else {
 try { 
 $post = $this->getRequest()->getPostValue();
 $Format = $this->_objectManager->create('ChennaiBox\CustomerAccount\Model\Format');
 $Format->setCustomerid($cutomerEmail); 
 $Format->setFormat($post['type']);
 $Format->save();
 $this->messageManager->addSuccess(__('Profile has beem saved successfully.'));
 $this->_redirect('customer/account/');
 return;
 } catch (\Exception $e) {
 $this->messageManager->addError(__('Something went wrong while saving your profile.'));
 }
 }
 $this->_redirect('customer/account/');
}

}

This is my Controller File, I defined Construct for Object manager.

My table name is "email_format" and its have two columns, 1.customerid 2. format, Magento2 any other way to save data to database?, how to solve this problem?

asked Apr 26, 2016 at 5:22
4
  • 1
    show your model and resourceModel class Commented Apr 26, 2016 at 5:42
  • also try this $Format->setCustomerid($cutomerEmail); $Format->setFormat($post['type']); Commented Apr 26, 2016 at 5:45
  • @ Sohel Rana see this link magento.stackexchange.com/questions/112550/… This are my module classes Commented Apr 26, 2016 at 6:02
  • @ Amit Bera , I update my queation, I tried your way , that also not working for me. suggest me any other way to do this, Thanks for Support. Commented Apr 26, 2016 at 6:23

1 Answer 1

4

Change your code as below:

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

changed to:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$Format = $objectManager->create('ChennaiBox\CustomerAccount\Model\Format');

You have used $this->_objectManager but you have not defined it in construct.

answered Apr 26, 2016 at 6:09
6
  • I update my question , now, suggest me what I miss Thanks for support Commented Apr 26, 2016 at 6:19
  • Just use above code instead of $Format = $this->_objectManager->create('ChennaiBox\CustomerAccount\Model\Format'); Commented Apr 26, 2016 at 6:32
  • @ Prashant Valanda I use Your code also "$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $Format = $objectManager->create('ChennaiBox\CustomerAccount\Model\Format');" its not working for me, I think my model class probelm, This is model class link magento.stackexchange.com/questions/112550/… suggest me what i miss in my model class. Commented Apr 26, 2016 at 6:38
  • In your code you are trying to set email address in your primary key so remove this line from your code $Format->setCustomerid($cutomerEmail); and recheck it. Commented Apr 26, 2016 at 10:06
  • Yes, Now, I solved problem. Thanks For Support. Commented Apr 26, 2016 at 11:01

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.