Για να προχωρήσουμε στην διαδικασία πρέπει να έχουμε δημιουργήσει ένα module, εάν δεν γνωρίζεις πως διάβασε το άρθρο Δημιουργία module για το Magento 2.

Δημιουργία του EAV model

#app/code/Gladd/Demo/Model/Item.php

<?php
/**
 * Copyright © 2018 gladd. All rights reserved.
 */
namespace Gladd\Demo\Model;
use Magento\Framework\Model\AbstractModel;

class Item extends AbstractModel
{
    const ENTITY = "gladd_demo_item";

    protected function _construct()
    {
        $this->_init(\Gladd\Demo\Model\ResourceModel\Item::class);
    }
}

Μέσω της function _construct συνδέουμε το EAV Model που μόλις δημιουργήσαμε με το Resource Model που θα δημιουργήσουμε στο επόμενο βήμα.

Δημιουργία του Resource Model

#app/code/Gladd/Demo/Model/ResourceModel/Item.php

<?php
/**
 * Copyright © 2018 gladd. All rights reserved.
 */
namespace Gladd\Demo\Model\ResourceModel;
use Magento\Eav\Model\Entity\AbstractEntity;

class Item extends AbstractEntity
{
    protected function _construct() {}

    public function getEntityType()
    {
        if(empty($this->_type)) {
            $this->setType(\Gladd\Demo\Model\Item::ENTITY);
        }
        return parent::getEntityType();
    }
}

Δημιουργία του Resource Model Collection

#app/code/Gladd/Demo/Model/ResourceModel/Item/Collection.php

<?php
/**
 * Copyright © 2018 gladd. All rights reserved.
 */
namespace Gladd\Demo\Model\ResourceModel\Item;
use Magento\Eav\Model\Entity\Collection\AbstractCollection;

class Collection extends AbstractCollection {
    protected function _construct() {
        $this->_init(\Gladd\Demo\Model\Item::class, \Gladd\Demo\Model\ResourceModel\Item::class);
    }
}

Μέχρι εδώ καλώς, έχουμε δημιουργήσει τα βασικά αρχεία για το νέο μας EAV Entity Model, αλλά το entity δεν έχει δηλωθεί ακόμη στην βάση.

Δημιουργία των βασικών πινάκων στη βάση δεδομένων

#app/code/Gladd/Demo/Setup/InstallSchema.php

<?php
/**
 * Copyright © 2018 gladd. All rights reserved.
 */

namespace Gladd\Demo\Setup;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class InstallSchema implements InstallSchemaInterface
{
    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     * @throws \Zend_Db_Exception
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();

        /**
         * Create 'gladd_demo_item_entity' table
         */
        $table = $installer->getConnection()
            ->newTable($installer->getTable('gladd_demo_item_entity')
            )
            ->addColumn(
                'entity_id',
                Table::TYPE_INTEGER,
                null,
                ['identity' => true, 'unsigned'=> true, 'nullable' => false, 'primary' => true],
                'Entity Id'
            )
            ->addColumn(
                'attribute_set_id',
                Table::TYPE_SMALLINT,
                null,
                ['unsigned' => true, 'nullable' => false, 'default' => '0'],
                'Attribute Set ID'
            )
            ->addColumn(
                'code',
                Table::TYPE_TEXT,
                64,
                [],
                'Code'
            )
            ->addColumn(
                'name',
                Table::TYPE_TEXT,
                64,
                [],
                'Item name'
            )
            ->addColumn(
                'created_at',
                Table::TYPE_TIMESTAMP,
                null,
                ['nullable' => false, 'default' => Table::TIMESTAMP_INIT],
                'Created At'
            )
            ->addColumn(
                'updated_at',
                Table::TYPE_TIMESTAMP,
                null,
                ['nullable' => false, 'default' => Table::TIMESTAMP_INIT_UPDATE],
                'Updated At'
            )
            ->addColumn(
                'is_active',
                Table::TYPE_SMALLINT,
                null,
                ['unsigned' => true, 'nullable' => false, 'default' => '1'],
                'Is Active'
            )
            ->addIndex(
                $installer->getIdxName('gladd_demo_item_entity', ['attribute_set_id']),
                ['attribute_set_id']
            )
            ->addIndex(
                $installer->getIdxName('gladd_demo_item_entity', ['code']),
                ['code']
            )
            ->addForeignKey(
                $installer->getFkName(
                    'gladd_demo_item_entity',
                    'attribute_set_id',
                    'eav_attribute_set',
                    'attribute_set_id'
                ),
                'attribute_set_id',
                $installer->getTable('eav_attribute_set'),
                'attribute_set_id',
                Table::ACTION_CASCADE
            )
            ->setComment('Demo Item table');
        $installer->getConnection()->createTable($table);

        /**
         * Create 'gladd_demo_item_entity_datetime' table
         */
        $table = $installer->getConnection()
            ->newTable($installer->getTable('gladd_demo_item_entity_datetime')
            )
            ->addColumn(
                'value_id',
                Table::TYPE_INTEGER,
                null,
                ['identity' => 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(
                    'gladd_demo_item_entity_datetime',
                    ['entity_id', 'attribute_id', 'store_id'],
                    AdapterInterface::INDEX_TYPE_UNIQUE
                ),
                ['entity_id', 'attribute_id', 'store_id'],
                ['type' => AdapterInterface::INDEX_TYPE_UNIQUE]
            )
            ->addIndex(
                $installer->getIdxName('gladd_demo_item_entity_datetime', ['attribute_id']),
                ['attribute_id']
            )
            ->addIndex(
                $installer->getIdxName('gladd_demo_item_entity_datetime', ['store_id']),
                ['store_id']
            )
            ->addForeignKey(
                $installer->getFkName(
                    'gladd_demo_item_entity_datetime',
                    'attribute_id',
                    'eav_attribute',
                    'attribute_id'
                ),
                'attribute_id',
                $installer->getTable('eav_attribute'),
                'attribute_id',
                Table::ACTION_CASCADE
            )
            ->addForeignKey(
                $installer->getFkName(
                    'gladd_demo_item_entity_datetime',
                    'entity_id',
                    'gladd_demo_item_entity',
                    'entity_id'
                ),
                'entity_id',
                $installer->getTable('gladd_demo_item_entity'),
                'entity_id',
                Table::ACTION_CASCADE
            )
            ->addForeignKey(
                $installer->getFkName('gladd_demo_item_entity_datetime', 'store_id', 'store', 'store_id'),
                'store_id',
                $installer->getTable('store'),
                'store_id',
                Table::ACTION_CASCADE
            )
            ->setComment('Demo Item Datetime Attribute Backend Table');
        $installer->getConnection()->createTable($table);

        /**
         * Create 'gladd_demo_item_entity_decimal' table
         */
        $table = $installer->getConnection()
            ->newTable($installer->getTable('gladd_demo_item_entity_decimal')
            )
            ->addColumn(
                'value_id',
                Table::TYPE_INTEGER,
                null,
                ['identity' => 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_DECIMAL,
                '12,4',
                [],
                'Value'
            )
            ->addIndex(
                $installer->getIdxName(
                    'gladd_demo_item_entity_decimal',
                    ['entity_id', 'attribute_id', 'store_id'],
                    AdapterInterface::INDEX_TYPE_UNIQUE
                ),
                ['entity_id', 'attribute_id', 'store_id'],
                ['type' => AdapterInterface::INDEX_TYPE_UNIQUE]
            )
            ->addIndex(
                $installer->getIdxName('gladd_demo_item_entity_decimal', ['store_id']),
                ['store_id']
            )
            ->addIndex(
                $installer->getIdxName('gladd_demo_item_entity_decimal', ['attribute_id']),
                ['attribute_id']
            )
            ->addForeignKey(
                $installer->getFkName(
                    'gladd_demo_item_entity_decimal',
                    'attribute_id',
                    'eav_attribute',
                    'attribute_id'
                ),
                'attribute_id',
                $installer->getTable('eav_attribute'),
                'attribute_id',
                Table::ACTION_CASCADE
            )
            ->addForeignKey(
                $installer->getFkName(
                    'gladd_demo_item_entity_decimal',
                    'entity_id',
                    'gladd_demo_item_entity',
                    'entity_id'
                ),
                'entity_id',
                $installer->getTable('gladd_demo_item_entity'),
                'entity_id',
                Table::ACTION_CASCADE
            )
            ->addForeignKey(
                $installer->getFkName('gladd_demo_item_entity_decimal', 'store_id', 'store', 'store_id'),
                'store_id',
                $installer->getTable('store'),
                'store_id',
                Table::ACTION_CASCADE
            )
            ->setComment('Demo Item Decimal Attribute Backend Table');
        $installer->getConnection()->createTable($table);

        /**
         * Create 'gladd_demo_item_entity_int' table
         */
        $table = $installer->getConnection()
            ->newTable($installer->getTable('gladd_demo_item_entity_int')
            )
            ->addColumn(
                'value_id',
                Table::TYPE_INTEGER,
                null,
                ['identity' => 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_INTEGER,
                null,
                [],
                'Value'
            )
            ->addIndex(
                $installer->getIdxName(
                    'gladd_demo_item_entity_int',
                    ['entity_id', 'attribute_id', 'store_id'],
                    AdapterInterface::INDEX_TYPE_UNIQUE
                ),
                ['entity_id', 'attribute_id', 'store_id'],
                ['type' => AdapterInterface::INDEX_TYPE_UNIQUE]
            )
            ->addIndex(
                $installer->getIdxName('gladd_demo_item_entity_int', ['attribute_id']),
                ['attribute_id']
            )
            ->addIndex(
                $installer->getIdxName('gladd_demo_item_entity_int', ['store_id']),
                ['store_id']
            )
            ->addForeignKey(
                $installer->getFkName('gladd_demo_item_entity_int', 'attribute_id', 'eav_attribute', 'attribute_id'),
                'attribute_id',
                $installer->getTable('eav_attribute'),
                'attribute_id',
                Table::ACTION_CASCADE
            )
            ->addForeignKey(
                $installer->getFkName('gladd_demo_item_entity_int', 'entity_id', 'gladd_demo_item_entity', 'entity_id'),
                'entity_id',
                $installer->getTable('gladd_demo_item_entity'),
                'entity_id',
                Table::ACTION_CASCADE
            )
            ->addForeignKey(
                $installer->getFkName('gladd_demo_item_entity_int', 'store_id', 'store', 'store_id'),
                'store_id',
                $installer->getTable('store'),
                'store_id',
                Table::ACTION_CASCADE
            )
            ->setComment('Demo Item Attribute Backend Table');
        $installer->getConnection()->createTable($table);

        /**
         * Create 'gladd_demo_item_entity_text' table
         */
        $table = $installer->getConnection()
            ->newTable($installer->getTable('gladd_demo_item_entity_text')
            )
            ->addColumn(
                'value_id',
                Table::TYPE_INTEGER,
                null,
                ['identity' => 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_TEXT,
                '64k',
                [],
                'Value'
            )
            ->addIndex(
                $installer->getIdxName(
                    'gladd_demo_item_entity_text',
                    ['entity_id', 'attribute_id', 'store_id'],
                    AdapterInterface::INDEX_TYPE_UNIQUE
                ),
                ['entity_id', 'attribute_id', 'store_id'],
                ['type' => AdapterInterface::INDEX_TYPE_UNIQUE]
            )
            ->addIndex(
                $installer->getIdxName('gladd_demo_item_entity_text', ['attribute_id']),
                ['attribute_id']
            )
            ->addIndex(
                $installer->getIdxName('gladd_demo_item_entity_text', ['store_id']),
                ['store_id']
            )
            ->addForeignKey(
                $installer->getFkName(
                    'gladd_demo_item_entity_text',
                    'attribute_id',
                    'eav_attribute',
                    'attribute_id'
                ),
                'attribute_id',
                $installer->getTable('eav_attribute'),
                'attribute_id',
                Table::ACTION_CASCADE
            )
            ->addForeignKey(
                $installer->getFkName(
                    'gladd_demo_item_entity_text',
                    'entity_id',
                    'gladd_demo_item_entity',
                    'entity_id'
                ),
                'entity_id',
                $installer->getTable('gladd_demo_item_entity'),
                'entity_id',
                Table::ACTION_CASCADE
            )
            ->addForeignKey(
                $installer->getFkName('gladd_demo_item_entity_text', 'store_id', 'store', 'store_id'),
                'store_id',
                $installer->getTable('store'),
                'store_id',
                Table::ACTION_CASCADE
            )
            ->setComment('Demo Item Text Attribute Backend Table');
        $installer->getConnection()->createTable($table);

        /**
         * Create 'gladd_demo_item_entity_varchar' table
         */
        $table = $installer->getConnection()
            ->newTable($installer->getTable('gladd_demo_item_entity_varchar')
            )
            ->addColumn(
                'value_id',
                Table::TYPE_INTEGER,
                null,
                ['identity' => 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_TEXT,
                255,
                [],
                'Value'
            )
            ->addIndex(
                $installer->getIdxName(
                    'gladd_demo_item_entity_varchar',
                    ['entity_id', 'attribute_id', 'store_id'],
                    AdapterInterface::INDEX_TYPE_UNIQUE
                ),
                ['entity_id', 'attribute_id', 'store_id'],
                ['type' => AdapterInterface::INDEX_TYPE_UNIQUE]
            )
            ->addIndex(
                $installer->getIdxName('gladd_demo_item_entity_varchar', ['attribute_id']),
                ['attribute_id']
            )
            ->addIndex(
                $installer->getIdxName('gladd_demo_item_entity_varchar', ['store_id']),
                ['store_id']
            )
            ->addForeignKey(
                $installer->getFkName(
                    'gladd_demo_item_entity_varchar',
                    'attribute_id',
                    'eav_attribute',
                    'attribute_id'
                ),
                'attribute_id',
                $installer->getTable('eav_attribute'),
                'attribute_id',
                Table::ACTION_CASCADE
            )
            ->addForeignKey(
                $installer->getFkName(
                    'gladd_demo_item_entity_varchar',
                    'entity_id',
                    'gladd_demo_item_entity',
                    'entity_id'
                ),
                'entity_id',
                $installer->getTable('gladd_demo_item_entity'),
                'entity_id',
                Table::ACTION_CASCADE
            )
            ->addForeignKey(
                $installer->getFkName('gladd_demo_item_entity_varchar', 'store_id', 'store', 'store_id'),
                'store_id',
                $installer->getTable('store'),
                'store_id',
                Table::ACTION_CASCADE
            )
            ->setComment('Demo Item Varchar Attribute Backend Table');
        $installer->getConnection()->createTable($table);

        /**
         * Create 'gladd_demo_eav_attribute' table
         */
        $table = $installer->getConnection()
            ->newTable($installer->getTable('gladd_demo_eav_attribute')
            )
            ->addColumn(
                'attribute_id',
                Table::TYPE_SMALLINT,
                null,
                ['unsigned' => true, 'nullable' => false, 'primary' => true],
                'Attribute ID'
            )
            ->addColumn(
                'is_visible',
                Table::TYPE_SMALLINT,
                null,
                ['unsigned' => true, 'nullable' => false, 'default' => '1'],
                'Is Visible'
            )
            ->addForeignKey(
                $installer->getFkName('gladd_demo_eav_attribute', 'attribute_id', 'eav_attribute', 'attribute_id'),
                'attribute_id',
                $installer->getTable('eav_attribute'),
                'attribute_id',
                Table::ACTION_CASCADE
            )
            ->setComment('Demo EAV Attribute Table');
        $installer->getConnection()
            ->createTable($table);

        $installer->endSetup();
    }
}

Προετοιμασία του νέου entity

#app/code/Gladd/Demo/Setup/ItemSetup.php

<?php
/**
 * Copyright © 2018 gladd. All rights reserved.
 */

namespace Gladd\Demo\Setup;

use Magento\Eav\Model\Entity\Setup\Context;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class ItemSetup extends EavSetup
{
    /**
     * Init
     *
     * @param ModuleDataSetupInterface $setup
     * @param Context $context
     * @param CacheInterface $cache
     * @param CollectionFactory $attrGroupCollectionFactory
     */
    public function __construct(
        ModuleDataSetupInterface $setup,
        Context $context,
        CacheInterface $cache,
        CollectionFactory $attrGroupCollectionFactory
    ) {
        parent::__construct($setup, $context, $cache, $attrGroupCollectionFactory);
    }

    /**
     * Default entities and attributes
     *
     * @return array
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function getDefaultEntities()
    {
        return [
            'gladd_demo_item' => [
                'entity_model' => \Gladd\Demo\Model\ResourceModel\Item::class,
                'table' => 'gladd_demo_item_entity',
                'additional_attribute_table' => 'gladd_demo_eav_attribute',
                'attributes' => [
                    'name' => [
                        'type' => 'varchar',
                        'label' => 'Name',
                        'input' => 'text',
                        'sort_order' => 1,
                    ],
                    'code' => [
                        'type' => 'static',
                        'label' => 'Code',
                        'input' => 'text',
                        'unique' => true,
                        'sort_order' => 2,
                    ],
                    'description' => [
                        'type' => 'text',
                        'label' => 'Description',
                        'input' => 'textarea',
                        'sort_order' => 3,
                    ],
                    'created_at' => [
                        'type' => 'static',
                        'input' => 'date',
                        'sort_order' => 4,
                        'visible' => false,
                    ],
                    'updated_at' => [
                        'type' => 'static',
                        'input' => 'date',
                        'sort_order' => 5,
                        'visible' => false,
                    ],
                ],
            ]
        ];
    }
}

Δημιουργία του νέου entity

#app/code/Gladd/Demo/Setup/InstallData.php

<?php
/**
 * Copyright © 2018 gladd. All rights reserved.
 */

namespace Gladd\Demo\Setup;

use Gladd\Demo\Setup\ItemSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    /**
     * Item setup factory
     *
     * @var ItemSetupFactory
     */
    private $itemSetupFactory;

    public function __construct(
        ItemSetupFactory $itemSetupFactory
    ) {
        $this->itemSetupFactory = $itemSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $itemSetup = $this->itemSetupFactory->create(['setup' => $setup]);
        $itemSetup->installEntities();
    }
}

Σε αυτό το σημείο έχουμε όλα τα απαραίτητα αρχεία και είμαστε έτοιμοι να εκτελέσουμε την διαδικασία του συστήματος για να δημιουργηθεί το νέο μας Eav Entity.

$ php bin/magento module:enable Gladd_Demo
$ php bin/magento setup:upgrade

Αφήστε ένα Σχόλιο

Η ηλ. διεύθυνση σας δεν δημοσιεύεται. Τα υποχρεωτικά πεδία σημειώνονται με *

Scroll to Top

Εγγραφή στο
Newsletter

Προσφορές, νέα και ανακοινώσεις κατευθείαν στο inbox σας!

Δεν στέλνουμε spam