I have a basic module ready with controller and view working perfectly. Now, I am trying to initiate model in order to save data using custom model in table containing attributes (question Title, Question). Basically, what steps I should proceed in order to save data through model in a custom table ?
How should I do it, any help would be greatly appreacited.
I have below code in my action file :
class Post extends \Magento\Framework\App\Action\Action
{
protected $_objectManager;
public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager)
{
$this->_objectManager = $objectManager;
}
public function execute()
{
$post = $this->getRequest()->getPostValue();
$model = $this->_objectManager->create('Chirag\Mygrid\Model\Question');
$model->setData('question_title', $post['question_title']);
$model->setData('question', $post['question']);
$model->save();
}
}
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Chirag\Mygrid\Model;
class Question extends \Magento\Framework\Model\AbstractModel
{
/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('Chirag\Mygrid\Model\Resource\Question');
}
}
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Chirag\Mygrid\Model\Resource;
class Question extends \Magento\Framework\Model\Resource\Db\AbstractDb
{
/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('questions_and_answers', 'question_id');
}
}
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Chirag\Mygrid\Model\Resource\Question;
class Collection extends \Magento\Framework\Model\Resource\Db\Collection\AbstractCollection
{
protected function _construct()
{
$this->_init('Chirag\Mygrid\Model\Question', 'Chirag\Mygrid\Model\Resource\Question');
$this->_map['fields']['page_id'] = 'main_table.page_id';
}
/**
* Prepare page's statuses.
* Available event cms_page_get_available_statuses to customize statuses.
*
* @return array
*/
public function getAvailableStatuses()
{
return [self::STATUS_ENABLED => __('Enabled'), self::STATUS_DISABLED => __('Disabled')];
}
}
1st Edit:
I had successfully completed this and have uploaded the whole setup to Git, please find below URL in case you want to check it out :
FAQ with custom grid extension in Magento 2
You can save Data like this way in Magento 2:
$question = $this->_objectManager->create('Ecom\HelloWorld\Model\Question');
$question->setTitle('Simple Question');
$question->setDescription('Question Description');
$question->save();
this is the code that you will add in your particular action
Updated Answer
Add your question model as follows:
namespace Chirag\Mygrid\Model;
class Question 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('Chirag\Mygrid\Model\Resource\Question');
}
}
Also please flush cache as well.
Please follow here as well: In magento 2 what is the correct way for getModel?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With