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

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


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');
              $this->messageManager->addSuccess(__('Profile has beem saved successfully.'));



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


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?

  • 1
    show your model and resourceModel class
    – Sohel Rana
    Apr 26 '16 at 5:42
  • also try this $Format->setCustomerid($cutomerEmail); $Format->setFormat($post['type']);
    – Amit Bera
    Apr 26 '16 at 5:45
  • @ Sohel Rana see this link magento.stackexchange.com/questions/112550/… This are my module classes Apr 26 '16 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. Apr 26 '16 at 6:23

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.

  • I update my question , now, suggest me what I miss Thanks for support Apr 26 '16 at 6:19
  • Just use above code instead of $Format = $this->_objectManager->create('ChennaiBox\CustomerAccount\Model\Format'); Apr 26 '16 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. Apr 26 '16 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. Apr 26 '16 at 10:06
  • Yes, Now, I solved problem. Thanks For Support. Apr 26 '16 at 11:01

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.