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?
1 Answer 1
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 supportRajkumar Elumalai– Rajkumar Elumalai2016年04月26日 06:19:36 +00:00Commented Apr 26, 2016 at 6:19
-
Just use above code instead of
$Format = $this->_objectManager->create('ChennaiBox\CustomerAccount\Model\Format');Prashant Valanda– Prashant Valanda2016年04月26日 06:32:55 +00:00Commented 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.Rajkumar Elumalai– Rajkumar Elumalai2016年04月26日 06:38:18 +00:00Commented 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.Prashant Valanda– Prashant Valanda2016年04月26日 10:06:03 +00:00Commented Apr 26, 2016 at 10:06 -
Yes, Now, I solved problem. Thanks For Support.Rajkumar Elumalai– Rajkumar Elumalai2016年04月26日 11:01:46 +00:00Commented Apr 26, 2016 at 11:01
$Format->setCustomerid($cutomerEmail); $Format->setFormat($post['type']);