I'm working on magento2 custom module. i have to install some data in my custom_table. so i am referencing below script.
i know the idea about $postFactory as describe here
Magento2: How to add pre defined data (installData.php) for Custom Module .
I'm bit confused.
As mentioned below __construct function is calling postFactory:
Is it $postFactory necessary for inseting data??
If yes then my modulename & tabelname both are different so how can i do that ??
<?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();
}
}
Magento\Framework\App\ResourceConnectionclass to insert data in table. – Prince Patel Mar 07 '18 at 07:00