2

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.

7
  • Can you please add your installschema or upgradeschema code i want to see the code of creation of EAV attribute Commented Feb 25, 2019 at 4:49
  • Hi Rutvee, I update my question. Same vendorname_modulename_modulename_text, varchar, decimal, int tables create as like vendorname_modulename_modulename_datetime. Just value field datatype change. Commented Feb 25, 2019 at 4:55
  • Have make any new entity? or are you using any default one? Magento2's default entity declared here "eav_entity_type" table Commented Feb 25, 2019 at 5:02
  • Yes. I created some entity for my module using InstallData and also eav_entity_type of my module. Commented Feb 25, 2019 at 5:08
  • Do you have an example of module which is eav type with UI grid ? Commented Feb 25, 2019 at 5:09

1 Answer 1

1

The main difference is in loading the attributes. If your collection extends Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection only data from the main table of your eav model is loaded into the collection. Therefore your items do not have the attributes.

If you create a new item (object which extends Magento\Eav\Model\Entity\AbstractEntity) and add it to your collection, the second variant could work for the first save since the "magic" happens in Magento\Eav\Model\Entity\AbstractEntity.

But loading and changing data for existing records will definitely not work.

Anyway if you have an EAV model your collection should extend Magento\Eav\Model\Entity\Collection\AbstractCollection

9
  • Can you please give me code example ? It will be more helpful. Commented Feb 25, 2019 at 4:40
  • If I extend Magento\Eav\Model\Entity\Collection\AbstractCollection then how to display in UI grid ? Commented Feb 25, 2019 at 4:42
  • when open UI grid with Magento\Eav\Model\Entity\Collection\AbstractCollection then, It will return this error. => Fatal error: Method Magento\Ui\TemplateEngine\Xhtml\Result::__toString() must not throw an exception, caught TypeError: Argument 5 passed to Magento\Eav\Model\Entity\Collection\AbstractCollection::__construct() must be an instance of Magento\Eav\Model\Config, null given, called in /var/www/html/mg226/app/code/VendorName/ModuleName/Model/ResourceModel/Mainpage/Grid/Collection.php on line 43 in /var/www/html/mg226/vendor/magento/module-ui/Component/Wrapper/UiComponent.php on line 0 Commented Feb 25, 2019 at 4:46
  • Ok, in your question there was nothing about UI grid :-) Look at this question and the comment about DataProvider it might help you magento.stackexchange.com/q/158564/76597 Otherwise please paste some code about your grid and I will look at that tommorow. The problem is most likely not the collection Commented Feb 25, 2019 at 6:07
  • I also do that. But, not working. :( Can you please see chat room? Please help me. It's urgent for me :( Commented Feb 25, 2019 at 6:13

Your Answer

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.