0

I have created a custom table name as "my_topic" using InstallSchema now i want to insert data in this table. How can i do this please suggest your answers.

1 Answer 1

2

Consider your table with title,content_heading and content field are defined,

Now you have to pass inside __consturct field to your module factory object like below, Table with below content are automatically added.

<?php
namespace Vendor\Modulename\Setup;

use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    private $_postFactory;

    public function __construct(\Vendor\Modulename\Model\PostFactory $postFactory)
    {
        $this->_postFactory = $postFactory;
    }


    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $data = [
            'title' => 'Hello world!',
            'content_heading' => 'Hello world!',
            'content' => 'logn description put here'
        ];

        $this->_postFactory->create()->setData($data)->save();
    }

}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.