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.
asked Sep 28, 2016 at 8:42
Ramkishan Suthar
4,0526 gold badges36 silver badges56 bronze badges
1 Answer 1
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();
}
}
answered Sep 28, 2016 at 8:50
Rakesh Jesadiya
42.5k19 gold badges135 silver badges186 bronze badges
default