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.

asked Sep 28, 2016 at 8:42

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();
 }
}
answered Sep 28, 2016 at 8:50
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.