Reading list Switch to dark mode

    How to create custom entity in Shopware6

    Updated 14 September 2020

    Introduction

    This article give you a brief introduction about how to create a custom entity in shopware6. Your plugin have to save you custom data into table for this approach we need to create entity.

    Overview

    If you want to create a custom entity then you need to create entity definition for your table. for this Shopware 6 uses data abstraction layer query for database.

    Create Entity Definition

    First of all we need to create EntityDefinition class, then create a customEntityDefinition.php file into directory <plugin root>/src/Core/Content/Bundle. In Entity definition define table name and define field collection. In field collection we also use association for the tables. For example create a custom entity definition below.

    <?php declare(strict_types=1);
    
    namespace Webkul\CustomEntity\Custom;
    
    use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
    use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\PrimaryKey;
    use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Required;
    use Shopware\Core\Framework\DataAbstractionLayer\Field\IdField;
    use Shopware\Core\Framework\DataAbstractionLayer\Field\StringField;
    use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;
    
    class CustomEntityDefinition extends EntityDefinition
    {
        public const ENTITY_NAME = 'table_name';
    
        public function getEntityName(): string
        {
            return self::ENTITY_NAME;
        }
    
        public function getCollectionClass(): string
        {
            return CustomEntityCollection::class;
        }
    
        public function getEntityClass(): string
        {
            return CustomEntity::class;
        }
    
        protected function defineFields(): FieldCollection
        {
            return new FieldCollection([
                (new IdField('id', 'id'))->addFlags(new PrimaryKey(), new Required()),
                new StringField('field_name', 'fieldName'),
            ]);
        }
    }

    There is no need to create CustomEntityClass and CustomEntityCollection file, it is your choice . because it has been done without creating entity class and entity collection.

    Registering your entity definition

    Register your custom entity definition into service, creating service.xml file under <plugin root>/src/Resources/config/ .Next you need to only register your custom entity definition class into service.xml file, so entity is register into DI container.

    Start your headless eCommerce
    now.
    Find out More

    This is how your services.xml could look like:

    <?xml version="1.0" ?>
    
    <container xmlns="http://symfony.com/schema/dic/services"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    
        <services>
            <service id="Webkul\Core\Content\Bundle\CustomEntityDefinition">
                <tag name="shopware.entity.definition" entity="table_name" />
            </service>
        </services>
    </container>

    Creating the table

    In addition to creating the database table, then create a new directory named src/Migration in your plugin root and add a migration class. As well as creating Migration it will be create table into database on installing plugin or updating plugin.

    <?php declare(strict_types=1);
    
    namespace Swag\CustomEntity\Migration;
    
    use Doctrine\DBAL\Connection;
    use Shopware\Core\Framework\Migration\MigrationStep;
    
    class Migration20072020Custom extends MigrationStep
    {
        public function getCreationTimestamp(): int
        {
            return 20072020;
        }
    
        public function update(Connection $connection): void
        {
            $sql = <<<SQL
    CREATE TABLE IF NOT EXISTS `custom_entity` (
        `id` BINARY(16) NOT NULL,
        `field_name` VARCHAR(255) COLLATE utf8mb4_unicode_ci,
        `created_at` DATETIME(3) NOT NULL,
        `updated_at` DATETIME(3),
        PRIMARY KEY (`id`)
    )
        ENGINE = InnoDB
        DEFAULT CHARSET = utf8mb4
        COLLATE = utf8mb4_unicode_ci;
    SQL;
            $connection->executeUpdate($sql);
        }
    }

    Use of custom entity in your logic

    As well as With help of the DI container you can get custom repository, and You can get your entity repository through container.

    I hope it will help you. Thanks for reading 🙂

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    2 comments

  • nobody
    • Adarsh Shukla (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home