{"id":14229,"date":"2021-10-18T00:46:17","date_gmt":"2021-10-18T06:16:17","guid":{"rendered":"https:\/\/www.emizentech.com\/blog\/?p=14229"},"modified":"2023-01-30T10:46:18","modified_gmt":"2023-01-30T10:46:18","slug":"how-to-create-data-associations-many-to-many-in-shopware","status":"publish","type":"post","link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/how-to-create-data-associations-many-to-many-in-shopware.html","title":{"rendered":"How to Create Data Associations (Many to Many) in Shopware 6"},"content":{"rendered":"\n<p>In our last blog, you learned <a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/create-one-to-many-many-to-one-data-associations-in-shopware-6.html\" target=\"_blank\" rel=\"noreferrer noopener\">how to create One to Many &amp; Many to One data associations<\/a> for your entities. In this blog, you will learn about Many to Many<br>For this, you will use two entities \u201cFirstentity\u201d &nbsp;and \u201cSecondentity\u201d (which was created in our last blog please refer to that blog \u201c<a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/create-data-associations-one-to-one-in-shopware-6.html\" target=\"_blank\" rel=\"noreferrer noopener\">How to create data associations(One to One) in Shopware 6<\/a>\u201d )<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Now we will see our data associations example first is \u201cMany to Many\u201d associations.<\/h3>\n\n\n\n<p>ManyToMany associations require another, third entity to be available. It will be called \u201cFirstentitySecondentityMappingDefinition\u201d and is responsible for connecting both definitions. It also needs its own database table so, From our last two entities, we will make a mapping definition to create many to many relationships and mapping definitions.<\/p>\n\n\n\n<p><em><strong>Emizentechplugin\\Core\\Content\\FirstentitySecondentityMappingDefinition<\/strong><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php declare(strict_types=1); \nnamespace Emizentechplugin\\Core\\Content; \n\nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Field\\CreatedAtField; \nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Field\\FkField; \nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Field\\Flag\\PrimaryKey; \nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Field\\Flag\\Required; \nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\Field\\ManyToOneAssociationField; \nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\FieldCollection; \nuse Shopware\\Core\\Framework\\DataAbstractionLayer\\MappingEntityDefinition; \nuse Emizentechplugin\\Core\\Content\\Firstentity\\FirstentityDefinition; \nuse Emizentechplugin\\Core\\Content\\Secondentity\\SecondentityDefinition; \n\nclass FirstentitySecondentityMappingDefinition extends MappingEntityDefinition \n{ \npublic const ENTITY_NAME = 'Firstentity_Secondentity; \n\npublic function getEntityName(): string {\n \treturn self::ENTITY_NAME;\n} \n\nprotected function defineFields(): FieldCollection \n{\n return new FieldCollection(&#091;\n \t(new FkField('second_id', 'secondId', SecondentityDefinition::class))-&gt;addFlags(new PrimaryKey(), new \tRequired()),\n\t(new FkField('first_id', 'firstId', FirstentityDefinition::class))-&gt;addFlags(new PrimaryKey(), new \tRequired()), \nnew ManyToOneAssociationField('Secondentities', 'second_id', SecondentityDefinition::class, 'id'),\nnew ManyToOneAssociationField('Firstentities', first_id', FirstentityDefinition::class, 'id') \n]); \n} \n}<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>First, you will notice a change in this entity in all other entity definitions the class extends from EntityDefinition in this the class extends from \u201cMappingEntityDefinition\u201d because it is a mapping definition connecting two entity definitions.<br><br><strong>Fields:<\/strong><br><br><strong>FkField:<\/strong> &nbsp;Both primary fields from the other two entity definition as \u201cFkField\u201d.<br><br><strong>ManyToOneAssociationField:<\/strong>&nbsp;In this Mapping Definition &nbsp;\u201cManyToOneAssociationField\u201d will be used for connecting it to other entities with the property names then the columns then pass the Definition class. The last parameter is most likely id, which is the column name of the connected table. You could add another boolean parameter here, which would define whether or not you want this association to always automatically be added and be loaded. This defaults to false, since enabling this could come with performance issues.<\/p>\n\n\n\n<p><strong><u><strong>Now the main Definitions which we are connecting for ManytoMany<\/strong><\/u><\/strong><\/p>\n\n\n\n<p><strong><em>Emizentechplugin\\Core\\Content\\Firstentity<\/em><\/strong><\/p>\n\n\n\n<p>Note:- please add use statement for &nbsp;\u201c<strong>FirstentitySecondentityMappingDefinition<\/strong>\u201d and &nbsp;\u201c<strong>SecondentityDefinition<\/strong>\u201d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>protected function defineFields(): FieldCollection \n{ \n\treturn new FieldCollection(&#091; \n\t(new IdField('id', 'id'))-&gt;addFlags(new Required(),new PrimaryKey()), \n\tnew ManyToManyAssociationField( 'Secondentities', SecondentityDefinition::class, \tFirstentitySecondentityMappingDefinition::class, 'second_id', 'first_id' ), \n\t]); \n}<\/code><\/pre>\n\n\n\n<p><strong><em>Emizentechplugin\\Core\\Content\\Secondentity<\/em><\/strong><\/p>\n\n\n\n<p>Note:- please add use statement for &nbsp;\u201c<strong>FirstentitySecondentityMappingDefinition<\/strong>\u201d and &nbsp;\u201c<strong>FirstentityDefinition<\/strong>\u201d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>protected function defineFields(): FieldCollection \n{ \n\treturn new FieldCollection(&#091; \n\t(new IdField('id', 'id'))-&gt;addFlags(new Required(),new PrimaryKey()), \n\tnew ManyToManyAssociationField( 'Firstentities', FirstentityDefinition::class, \tFirstentitySecondentityMappingDefinition::class, 'first_id', 'second_id' ), \n\t]); \n}<\/code><\/pre>\n\n\n\n<p><strong><u><strong>ManyToManyAssociationField Explanation:<\/strong><\/u><\/strong><\/p>\n\n\n\n<p><strong>propertyName:<\/strong> &nbsp;\u201c<strong>Secondentities<\/strong>\u201d and \u201c<strong>Firstentities<\/strong>\u201d are property name which is passed and which will contain associated entities.<\/p>\n\n\n\n<p><strong>referenceDefinition:<\/strong> The class of the associated definition to which we are asscociationg our entity \u201c<strong>SecondentityDefinition<\/strong>\u201d and \u201c<strong>FirstentityDefinition<\/strong>\u201d<\/p>\n\n\n\n<p>m<strong>appingDefinition:<\/strong> The class of the mapping definition \u201c<strong>FirstentitySecondentityMappingDefinition<\/strong>\u201d.<\/p>\n\n\n\n<p><strong>mappingLocalColumn:<\/strong> The name of the id column for the current entity, first_id&nbsp;if you&#8217;re in the FirstentityDefinition.<\/p>\n\n\n\n<p><strong>mappingReferenceColumn:<\/strong> The name of the id column for the referenced entity.<\/p>\n\n\n\n<p><br>And that\u2019s your Many to Many association established properly,<br>Now you have completed your Many to Many associations<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>We hope this cleared your doubts regarding creating data associations in Shopware 6. For more professional assistance do get in touch with Emizentech a <a href=\"https:\/\/multisitelocal.ezxdemo.com\/shopware-development.html\" target=\"_blank\" rel=\"noreferrer noopener\">Shopware development company<\/a> dedicated to providing the best ecommerce solutions.<\/p>\n\n\n","protected":false},"excerpt":{"rendered":"<p>In our last blog, you learned how to create One to Many &amp; Many to One data associations for your entities. In this blog, you will learn about Many to ManyFor this, you will use two entities \u201cFirstentity\u201d &nbsp;and \u201cSecondentity\u201d (which was created in our last blog please refer to that blog \u201cHow to create<\/p>\n","protected":false},"author":36,"featured_media":14240,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"MSN_Categories":"Uncategorized","MSN_Publish_Option":false,"MSN_Is_Local_News":false,"MSN_Is_AIAC_Included":"Empty","MSN_Location":"[]","MSN_Add_Feature_Img_On_Top_Of_Post":false,"MSN_Has_Custom_Author":false,"MSN_Custom_Author":"","MSN_Has_Custom_Canonical_Url":false,"MSN_Custom_Canonical_Url":"","_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[85],"tags":[],"class_list":{"0":"post-14229","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-shopware"},"modified_by":"Marketing EmizenTech","featured_image_src":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2021\/10\/Create-Data-Associations-Many-to-Many-Shopware-1-1.jpg","featured_image_src_square":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2021\/10\/Create-Data-Associations-Many-to-Many-Shopware-1-1.jpg","author_info":{"display_name":"Vivek Khatri","author_link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/author\/vivek"},"_links":{"self":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/14229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/users\/36"}],"replies":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/comments?post=14229"}],"version-history":[{"count":0,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/14229\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media\/14240"}],"wp:attachment":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media?parent=14229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/categories?post=14229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/tags?post=14229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}