Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save data using Model in Magento2

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

like image 990
Chiragit007 Avatar asked Aug 10 '15 13:08

Chiragit007


Video Answer


1 Answers

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?

like image 144
Pankaj Pareek Avatar answered Oct 01 '22 18:10

Pankaj Pareek