I create custom EAV module.
app/code/VendorName/ModuleName/Model/ResourceModel/Mainpage/Collection.php
<?php
namespace VendorName\ModuleName\Model\ResourceModel\Mainpage;
class Collection extends \Magento\Eav\Model\Entity\Collection\AbstractCollection
{
/**
* Define resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('VendorName\ModuleName\Model\Mainpage','VendorName\ModuleName\Model\ResourceModel\Mainpage');
}
}
If I extend \Magento\Eav\Model\Entity\Collection\AbstractCollection, I can able to save records in these three table
- vendorname_modulename_mainpage
- vendorname_modulename_mainpage_varchar
- vendorname_modulename_mainpage_text
VendorName\ModuleName\Model\ResourceModel\Mainpage\Collection.php
<?php
namespace VendorName\ModuleName\Model\ResourceModel\Mainpage;
class Collection extends \Magento\Eav\Model\Entity\Collection\AbstractCollection
{
/**
* Define resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('VendorName\ModuleName\Model\Mainpage','VendorName\ModuleName\Model\ResourceModel\Mainpage');
}
}
Now, If I used this below code, then I can't able to save records as eav type. only record save in vendorname_modulename_mainpage table.
<?php
namespace VendorName\ModuleName\Model\ResourceModel\Mainpage;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use VendorName\ModuleName\Model\Mainpage as MainpageModel;
use VendorName\ModuleName\Model\ResourceModel\Mainpage as MainpageResourceModel;
class Collection extends AbstractCollection
{
protected function _construct()
{
$this->_init(MainpageModel::class,MainpageResourceModel::class);
}
}
InstallSchema.php :
namespace VendorName\ModuleName\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();
/**
* Create table 'vendorname_modulename_eav_attribute'
*/
$tableName1 = $installer->getTable('vendorname_modulename_eav_attribute');
if ($installer->getConnection()->isTableExists($tableName1) != true) {
$table1 = $installer->getConnection()
->newTable($tableName1)
->addColumn(
'attribute_id',
Table::TYPE_INTEGER,
null,
[
'identity' => true,
'unsigned' => true,
'nullable' => false,
'primary' => true
],
'Attribute ID'
)
->addColumn(
'is_global',
Table::TYPE_INTEGER,
null,
[],
'Attribute Scope'
)
->addColumn(
'position',
Table::TYPE_INTEGER,
null,
[],
'Attribute position'
)
->addColumn(
'is_wysiwyg_enabled',
Table::TYPE_INTEGER,
null,
[],
'Attribute uses WYSIWYG'
)
->addColumn(
'is_visible',
Table::TYPE_INTEGER,
null,
[],
'Attribute is visible'
)
->setComment('Mainpage EAV Attribute Table');
$installer->getConnection()->createTable($table1);
}
/**
* Create table 'vendorname_modulename_modulename'
*/
$tableName2 = $installer->getTable('vendorname_modulename_modulename');
if ($installer->getConnection()->isTableExists($tableName2) != true) {
$table2 = $installer->getConnection()
->newTable($tableName2)
->addColumn(
'entity_id',
Table::TYPE_INTEGER,
null,
[
'identity' => true,
'unsigned' => true,
'nullable' => false,
'primary' => true
],
'Entity ID'
)
->addColumn(
'entity_type_id',
Table::TYPE_SMALLINT,
null,
[
'unsigned' => true,
'nullable' => false,
'default' => '0'
],
'Entity Type ID'
)
->addColumn(
'attribute_set_id',
Table::TYPE_SMALLINT,
null,
[
'unsigned' => true,
'nullable' => false,
'default' => '0',
],
'Attribute Set ID'
)
->addColumn(
'created_at',
Table::TYPE_TIMESTAMP,
null,
[
'nullable' => false,
'default' => ''
],
'Creation Time'
)
->addColumn(
'updated_at',
Table::TYPE_TIMESTAMP,
null,
[
'nullable' => false,
'default' => ''
],
'Update Time'
)
->addIndex(
$installer->getIdxName('vendorname_modulename_modulename', ['entity_type_id']),
['entity_type_id']
)
->addIndex(
$installer->getIdxName('vendorname_modulename_modulename', ['attribute_set_id']),
['attribute_set_id']
)
->addForeignKey(
$installer->getFkName(
'vendorname_modulename_modulename',
'attribute_set_id',
'eav_attribute_set',
'attribute_set_id'
),
'attribute_set_id',
$installer->getTable('eav_attribute_set'),
'attribute_set_id',
\Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
)
->addForeignKey(
$installer->getFkName(
'vendorname_modulename_modulename',
'entity_type_id',
'eav_entity_type',
'entity_type_id'
),
'entity_type_id',
$installer->getTable('eav_entity_type'),
'entity_type_id',
\Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
)
->setComment('Mainpage Table');
$installer->getConnection()->createTable($table2);
}
/**
* Create table 'vendorname_modulename_modulename_datetime'
*/
$tableName3 = $installer->getTable('vendorname_modulename_modulename_datetime');
if ($installer->getConnection()->isTableExists($tableName3) != true) {
$table3 = $installer->getConnection()
->newTable($tableName3)
->addColumn(
'value_id',
Table::TYPE_INTEGER,
null,
[
'identity' => true,
'unsigned' => true,
'nullable' => false,
'primary' => true
],
'Value ID'
)
->addColumn(
'attribute_id',
Table::TYPE_SMALLINT,
null,
[
'unsigned' => true,
'nullable' => false,
'default' => '0'
],
'Attribute ID'
)
->addColumn(
'store_id',
Table::TYPE_SMALLINT,
null,
[
'unsigned' => true,
'nullable' => false,
'default' => '0',
],
'Store ID'
)
->addColumn(
'entity_id',
Table::TYPE_INTEGER,
null,
[
'unsigned' => true,
'nullable' => false,
'default' => '0',
],
'Entity ID'
)
->addColumn(
'value',
Table::TYPE_DATETIME,
null,
[],
'Value'
)
->addIndex(
$installer->getIdxName(
'vendorname_modulename_modulename_datetime',
['entity_id', 'attribute_id', 'store_id'],
\Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
),
['entity_id', 'attribute_id', 'store_id'],
['type' => \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE]
)
->addIndex(
$installer->getIdxName('vendorname_modulename_modulename_datetime', ['attribute_id']),
['attribute_id']
)
->addIndex(
$installer->getIdxName('vendorname_modulename_modulename_datetime', ['store_id']),
['store_id']
)
->addForeignKey(
$installer->getFkName(
'vendorname_modulename_modulename_datetime',
'attribute_id',
'eav_attribute',
'attribute_id'
),
'attribute_id',
$installer->getTable('eav_attribute'),
'attribute_id',
Table::ACTION_CASCADE
)
->addForeignKey(
$installer->getFkName(
'vendorname_modulename_modulename_datetime',
'entity_id',
'vendorname_modulename_modulename',
'entity_id'
),
'entity_id',
$installer->getTable('vendorname_modulename_modulename'),
'entity_id',
Table::ACTION_CASCADE
)
->addForeignKey(
$installer->getFkName(
'vendorname_modulename_modulename_datetime',
'store_id',
'store',
'store_id'
),
'store_id',
$installer->getTable('store'),
'store_id',
Table::ACTION_CASCADE
)
->setComment('Datetime Table');
$installer->getConnection()->createTable($table3);
}
$installer->endSetup();
}
}
Why, I can't save If I used 2nd code file?
Please help me.
Thanks.