Magento – Magento 2: setdata() is not saving the values into the database

magento-2.0magento-2.1magento-2.1.3magento2magento2.2

Controller file

Post.php

<?php

namespace QaisarSatti\HelloWorld\Controller\Index;

class Post extends \Magento\Framework\App\Action\Action

{

    protected $_objectManager;
    protected $hello;

    public function __construct(\Magento\Framework\App\Action\Context $context,
                                \Magento\Framework\ObjectManagerInterface $objectManager,
                                \QaisarSatti\HelloWorld\Model\HelloworldFactory $helloworld)
    {
        $this->hello = $helloworld;
        $this->_objectManager = $objectManager;
        parent::__construct($context);
    }
    public function execute()
    {
        $post = $this->getRequest()->getPostValue();
        $model = $this->_objectManager->create('QaisarSatti\HelloWorld\Model\Helloworld');
        $model->setData('post_id', $post['post_id']);
        $model->setData('title', $post['title']);
        $model->setData('content', $post['content']);
        $model->save();
        $this->_redirect('*/*/');
        $this->messageManager->addSuccess(__('Your values has beeen submitted successfully.'));
    }

}

Model file

QaisarSatti\HelloWorld\Model\Helloworld.php

<?php


namespace QaisarSatti\HelloWorld\Model;



    class Helloworld extends \Magento\Framework\Model\AbstractModel  
    {   
        protected function _construct()
        {
            $this->_init('QaisarSatti\HelloWorld\Model\ResourceModel\Helloworld');
        }
    }

Resource Module:
QaisarSatti\HelloWorld\Model\ResourceModel\Helloworld.php

<?php

namespace QaisarSatti\HelloWorld\Model\ResourceModel;


use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class Helloworld extends AbstractDb

{

    protected function _construct()

    {

        $this->_init('sample_posts', 'post_id');

    }

}

Collection.php

QaisarSatti\HelloWorld\Model\ResourceModel\Helloworld\Collection.php

<?php


namespace QaisarSatti\HelloWorld\Model\ResourceModel\Helloworld;


use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;


class Collection extends AbstractCollection

{

    protected function _construct()
    {

        $this->_init(
        'QaisarSatti\HelloWorld\Model\Helloworld',
        'QaisarSatti\HelloWorld\Model\ResourceModel\Helloworld');

    }

}

Setup\InstallSchema.php

<?php

namespace QaisarSatti\HelloWorld\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;

class InstallSchema implements InstallSchemaInterface {

    public function install( SchemaSetupInterface $setup, ModuleContextInterface $context ) {
        $installer = $setup;

        $installer->startSetup();

        $table = $installer->getConnection()->newTable(
            $installer->getTable( 'sample_posts' )
        )->addColumn(
            'post_id',
            Table::TYPE_SMALLINT,
            null,
            [ 'identity' => true, 'nullable' => false, 'primary' => true ],
            'Post ID'
        )->addColumn(
            'title',
            Table::TYPE_TEXT,
            255,
            [ 'nullable' => false ],
            'Post Title'
        )->addColumn(
            'content',
            Table::TYPE_TEXT,
            '2M',
            [ ],
            'Post Content'
        )->setComment(
            'Sample Post Table'
        );

        $installer->getConnection()->createTable( $table );

        $installer->endSetup();
    }
}

I can able to get the values from frontend phtml file using the method getPostValue(). But I can't able to save it. I like to save the values which I get from getpostvalue() method to the database. Please help me.

Best Answer

Hi guys thanks for the support. Finally i found the solution after searching many sites.

In default Magento will try to update the database not insertion when we trying to save the data with the following code.

$_data = array('post_id'=>$data1,'title'=>$data2,'content'=>$data3);

$model->setData($_data);

$model->save();

Magento will check the Model to connect with the database. If the table has the primarykey column. And if we are trying to insert the value to the primary key field with the storefront value, Magento will look for update it won't create new row(insertion) in the table. At this time we need to use the Magento features in the Resource Model.

protected $_isPkAutoIncrement = false;

Then Magento will allow to store the value and create the new row in the table.

Related Topic