{"id":256417,"date":"2020-06-28T13:01:05","date_gmt":"2020-06-28T13:01:05","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=256417"},"modified":"2020-09-14T12:26:30","modified_gmt":"2020-09-14T12:26:30","slug":"how-to-create-custom-entity-in-shopware6","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/","title":{"rendered":"How to create custom entity in Shopware6"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>This article give you a brief introduction about how to create a custom entity in <a href=\"https:\/\/webkul.com\/blog\/custom-storefront-controller-in-shopware6\/\">shopware6<\/a>. Your plugin have to save you custom data into table for this approach we need to create entity.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Overview<\/h2>\n\n\n\n<p>If you want to create a custom entity then you need to create entity definition for your table. for this Shopware 6  uses <a href=\"https:\/\/docs.shopware.com\/en\/shopware-platform-dev-en\/references-internals\/core\/dal\">data abstraction layer<\/a> query for database.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create Entity Definition<\/h2>\n\n\n\n<p> First of all we need to create EntityDefinition class, then create a customEntityDefinition.php file into directory  &lt;plugin root&gt;\/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.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php declare(strict_types=1);\n\nnamespace Webkul\\CustomEntity\\Custom;\n\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\EntityDefinition;\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Field\\Flag\\PrimaryKey;\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Field\\Flag\\Required;\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Field\\IdField;\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Field\\StringField;\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\FieldCollection;\n\nclass CustomEntityDefinition extends EntityDefinition\n{\n    public const ENTITY_NAME = &#039;table_name&#039;;\n\n    public function getEntityName(): string\n    {\n        return self::ENTITY_NAME;\n    }\n\n    public function getCollectionClass(): string\n    {\n        return CustomEntityCollection::class;\n    }\n\n    public function getEntityClass(): string\n    {\n        return CustomEntity::class;\n    }\n\n    protected function defineFields(): FieldCollection\n    {\n        return new FieldCollection(&#091;\n            (new IdField(&#039;id&#039;, &#039;id&#039;))-&gt;addFlags(new PrimaryKey(), new Required()),\n            new StringField(&#039;field_name&#039;, &#039;fieldName&#039;),\n        ]);\n    }\n}<\/pre>\n\n\n\n<p>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. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Registering your entity definition<\/h2>\n\n\n\n<p>Register your custom entity definition into service, creating service.xml file under &lt;plugin root&gt;\/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.<\/p>\n\n\n\n<p>This is how your&nbsp;<code>services.xml<\/code>&nbsp;could look like:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?xml version=&quot;1.0&quot; ?&gt;\n\n&lt;container xmlns=&quot;http:\/\/symfony.com\/schema\/dic\/services&quot;\n           xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\n           xsi:schemaLocation=&quot;http:\/\/symfony.com\/schema\/dic\/services http:\/\/symfony.com\/schema\/dic\/services\/services-1.0.xsd&quot;&gt;\n\n    &lt;services&gt;\n        &lt;service id=&quot;Webkul\\Core\\Content\\Bundle\\CustomEntityDefinition&quot;&gt;\n            &lt;tag name=&quot;shopware.entity.definition&quot; entity=&quot;table_name&quot; \/&gt;\n        &lt;\/service&gt;\n    &lt;\/services&gt;\n&lt;\/container&gt;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Creating the table<\/h2>\n\n\n\n<p>In addition to creating the database table, then create a new directory named\u00a0<code>src\/Migration<\/code>\u00a0in 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.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php declare(strict_types=1);\n\nnamespace Swag\\CustomEntity\\Migration;\n\nuse Doctrine\\DBAL\\Connection;\nuse Shopware\\Core\\Framework\\Migration\\MigrationStep;\n\nclass Migration20072020Custom extends MigrationStep\n{\n    public function getCreationTimestamp(): int\n    {\n        return 20072020;\n    }\n\n    public function update(Connection $connection): void\n    {\n        $sql = &lt;&lt;&lt;SQL\nCREATE TABLE IF NOT EXISTS `custom_entity` (\n    `id` BINARY(16) NOT NULL,\n    `field_name` VARCHAR(255) COLLATE utf8mb4_unicode_ci,\n    `created_at` DATETIME(3) NOT NULL,\n    `updated_at` DATETIME(3),\n    PRIMARY KEY (`id`)\n)\n    ENGINE = InnoDB\n    DEFAULT CHARSET = utf8mb4\n    COLLATE = utf8mb4_unicode_ci;\nSQL;\n        $connection-&gt;executeUpdate($sql);\n    }\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Use of custom entity in your logic<\/h2>\n\n\n\n<p>As well as With help of the DI container you can get custom repository, and You can get your entity repository through container.<\/p>\n\n\n\n<p>I hope it will help you. Thanks for reading \ud83d\ude42<\/p>\n\n\n\n<p> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 <a href=\"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":325,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-256417","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to create custom entity in Shopware6 - Webkul Blog<\/title>\n<meta name=\"description\" content=\"how-to-create-custom-entity-in-shopware6| Creating a custom entity in shopware6| Creating custom entity class for shopware6\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to create custom entity in Shopware6 - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"how-to-create-custom-entity-in-shopware6| Creating a custom entity in shopware6| Creating custom entity class for shopware6\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-28T13:01:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-09-14T12:26:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Prince Gupta\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prince Gupta\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/\"},\"author\":{\"name\":\"Prince Gupta\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/669b7c5067f73a7ae2204ff4aca829fd\"},\"headline\":\"How to create custom entity in Shopware6\",\"datePublished\":\"2020-06-28T13:01:05+00:00\",\"dateModified\":\"2020-09-14T12:26:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/\"},\"wordCount\":289,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/\",\"url\":\"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/\",\"name\":\"How to create custom entity in Shopware6 - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2020-06-28T13:01:05+00:00\",\"dateModified\":\"2020-09-14T12:26:30+00:00\",\"description\":\"how-to-create-custom-entity-in-shopware6| Creating a custom entity in shopware6| Creating custom entity class for shopware6\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to create custom entity in Shopware6\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/669b7c5067f73a7ae2204ff4aca829fd\",\"name\":\"Prince Gupta\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/dd55a986709c72a714a8135a38f3b2cba1009ea371caec823a8547b2e01df18d?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/dd55a986709c72a714a8135a38f3b2cba1009ea371caec823a8547b2e01df18d?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Prince Gupta\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/princegupta-wp031\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to create custom entity in Shopware6 - Webkul Blog","description":"how-to-create-custom-entity-in-shopware6| Creating a custom entity in shopware6| Creating custom entity class for shopware6","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/","og_locale":"en_US","og_type":"article","og_title":"How to create custom entity in Shopware6 - Webkul Blog","og_description":"how-to-create-custom-entity-in-shopware6| Creating a custom entity in shopware6| Creating custom entity class for shopware6","og_url":"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2020-06-28T13:01:05+00:00","article_modified_time":"2020-09-14T12:26:30+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Prince Gupta","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Prince Gupta","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/"},"author":{"name":"Prince Gupta","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/669b7c5067f73a7ae2204ff4aca829fd"},"headline":"How to create custom entity in Shopware6","datePublished":"2020-06-28T13:01:05+00:00","dateModified":"2020-09-14T12:26:30+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/"},"wordCount":289,"commentCount":2,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/","url":"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/","name":"How to create custom entity in Shopware6 - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2020-06-28T13:01:05+00:00","dateModified":"2020-09-14T12:26:30+00:00","description":"how-to-create-custom-entity-in-shopware6| Creating a custom entity in shopware6| Creating custom entity class for shopware6","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/how-to-create-custom-entity-in-shopware6\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to create custom entity in Shopware6"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/669b7c5067f73a7ae2204ff4aca829fd","name":"Prince Gupta","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/dd55a986709c72a714a8135a38f3b2cba1009ea371caec823a8547b2e01df18d?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dd55a986709c72a714a8135a38f3b2cba1009ea371caec823a8547b2e01df18d?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Prince Gupta"},"url":"https:\/\/webkul.com\/blog\/author\/princegupta-wp031\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/256417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/325"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=256417"}],"version-history":[{"count":6,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/256417\/revisions"}],"predecessor-version":[{"id":267541,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/256417\/revisions\/267541"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=256417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=256417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=256417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}