Introduction
This article give you a brief introduction on how to create a custom entity in shopware6.
Overview
If you want to create a custom entity then you need to create entity definition for your table. Shopware 6 uses data abstraction layer query for database.
Plugin Base Class
So let’s start with the plugin base class, first of all you need to create service.xml file into the directory <plugin root>/src/Resources/config/. Shopware 6 automatically find your service.xml file then create a custom entity definition class as below.
The EntityDefinition class
The main entry point for create custom entity is an EntityDefinition
class. Further create a customEntityDefinition.php file into directory <plugin root>/src/Core/Content/Bundle.
<?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('technical_name', 'technicalName'), ]); } }
There is no need to create CustomEntityClass and CustomEntityCollection file, it is your choice . it has done without creating entity class and entity collection.
Registering your custom entity
Next you need to only register your custom entity definition class into service.xml file. As well as entity is register into DI container.
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 create a new directory named src/Migration
in your plugin root and add a migration class like this in there:
<?php declare(strict_types=1); namespace Swag\CustomEntity\Migration; use Doctrine\DBAL\Connection; use Shopware\Core\Framework\Migration\MigrationStep; class Migration1552484872Custom extends MigrationStep { public function getCreationTimestamp(): int { return 1552484872; } public function update(Connection $connection): void { $sql = <<<SQL CREATE TABLE IF NOT EXISTS `custom_entity` ( `id` BINARY(16) NOT NULL, `technical_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.
$customRepository = $this->container->get('custom_entity.repository'); $customId = $customRepository->searchIds( (new Criteria())->addFilter(new EqualsFilter('technicalName', 'Foo')), Context::createDefaultContext() )->getIds()[0];
I hope it will help you. Thanks for reading