{"id":13164,"date":"2021-09-26T00:00:03","date_gmt":"2021-09-26T05:30:03","guid":{"rendered":"https:\/\/www.emizentech.com\/blog\/?p=13164"},"modified":"2023-01-30T10:56:29","modified_gmt":"2023-01-30T10:56:29","slug":"create-data-associations-one-to-one-in-shopware-6","status":"publish","type":"post","link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/create-data-associations-one-to-one-in-shopware-6.html","title":{"rendered":"How To Create Data Associations (One to One) In Shopware 6"},"content":{"rendered":"<p>In this blog, we will learn about how to add an association to your entities. Three kinds of associations are there:<\/p>\n<ul>\n<li>One to One<\/li>\n<li>Many to One<\/li>\n<li>One to Many<\/li>\n<li>Many to Many<\/li>\n<\/ul>\n<p>For this, you will use two entities, \u201cFirstentity\u201d and \u201cSecondentity\u201d<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Example_entity_definitions\"><\/span>Example entity definitions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"1_Firstly_you_have_to_define_those_two_entities_on_this_path_%E2%80%9CEmizentechpluginsrcCoreContent%E2%80%9D\"><\/span>1. Firstly, you have to define those two entities on this path \u201cEmizentechplugin\/src\/Core\/Content\u201d.<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h3><span class=\"ez-toc-section\" id=\"2_There_are_two_entities_so_that_you_have_to_create_two_folders_with_the_entity_name_first_%E2%80%9CFirstentity%E2%80%9D_and_for_the_second_%E2%80%9CSecondentity%E2%80%9D_and_then_define_the_respective_definition_classes_for_your_entity_like_this\"><\/span>2. There are two entities, so that you have to create two folders with the entity name first \u201cFirstentity\u201d and for the second \u201cSecondentity\u201d and then define the respective definition classes for your entity like this :<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><strong>Emizentechplugin\/src\/Core\/Content\/Firstentity\/FirstentityDefinition.php<\/strong><\/pre>\n<pre>?php declare(strict_types=1);\n\nnamespace Emizentechplugin\\src\\Core\\Content\\Firstentity;\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\\FieldCollection;\n\nclass FirstentityDefinition extends EntityDefinition\n{\npublic const ENTITY_NAME = 'Firstentity';\n\npublic function getEntityName(): string\n{\nreturn self::ENTITY_NAME;\n}\n\nprotected function defineFields(): FieldCollection\n{\nreturn new FieldCollection([\n(new IdField('id', 'id'))-&gt;addFlags(new Required(), new PrimaryKey()),\n\/\/ Other fields here\n]);\n}\n}\n?&gt;\n<\/pre>\n<pre><strong>Emizentechplugin\/src\/Core\/Content\/Secondentity\/Secondentity Definition.php<\/strong><\/pre>\n<pre>&lt;?php declare(strict_types=1);\n\nnamespace Emizentechplugin\\src\\Core\\Content\\Secondentity;\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\\FieldCollection;\n\nclass SecondentityDefinition extends EntityDefinition\n{\npublic const ENTITY_NAME = 'Secondentity';\n\npublic function getEntityName(): string\n{\nreturn self::ENTITY_NAME;\n}\n\nprotected function defineFields(): FieldCollection\n{\nreturn new FieldCollection([\n(new IdField('id', 'id'))-&gt;addFlags(new Required(), new PrimaryKey()),\n\/\/ Other fields here\n]);\n}\n}\n\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"3_Now_we_will_see_our_data_associations_example_first_is_%E2%80%9COne_to_One%E2%80%9D_associations\"><\/span>3. Now we will see our data associations example first is \u201cOne to One\u201d associations.<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>One to One associations require you to define a foreign key for one of the two connected associations. E.g., the Firstentity table has to contain a second_id column, or the other way around: A first_id column in the Secondentity table. In this example, it will be second_id in the FirstentityDefinition<\/p>\n<p>Now, we will look at the example:<\/p>\n<pre>Emizentechplugin\/src\/Core\/Content\/Firstentity\/FirstentityDefinition.php\nnote: also add the use statement for SecondentityDefinition*\nprotected function defineFields(): FieldCollection\n{\nreturn new FieldCollection([\n(new IdField('id', 'id'))-&gt;addFlags(new Required(), new PrimaryKey()),\n(new FkField('second_id', 'secondId', SecondentityDefinition::class))-&gt;addFlags(new Required()),\n(new StringField('name', \t'name'))-&gt;addFlags(new Required()),\n\nnew OneToOneAssociationField('Secondentity', 'second_id', 'id', SecondentityDefinition::class, false)\n]);\n}\n<\/pre>\n<p>For OneToOneAssociationField<\/p>\n<ul>\n<li>Secondentity: It is the property name that should contain the associated entity in your respective definition.<\/li>\n<li>second_id: It is the name of the column in the database.<\/li>\n<li>id: It is the ID column in the referenced database ( Secondentity in this case) and the referenced definition.<\/li>\n<li>SecondentityDefinition: It is used for the property \u201cSecondentity,\u201d i.e., SecondentityDefinition will appear in Secondentity property that will make the association for that class and the property.<\/li>\n<li>false -&gt; if you want to automatically load this association every time you load a \u2018Firstentity\u2019 entity. We&#8217;ve set this to \u2018false\u2019.<\/li>\n<\/ul>\n<p>Now you will add code for the Secondentity for completion of the OnetoOne association example.<\/p>\n<pre><strong>Emizentechplugin\/src\/Core\/Content\/Secondentity\/Secondentity Definition.php<\/strong><\/pre>\n<p><b>Note:<\/b> also add the use statement for FirstentityDefinition*<\/p>\n<pre>protected function defineFields(): FieldCollection\n{\nreturn new FieldCollection([\n(new IdField('id', 'id'))-&gt;addFlags(new Required(), new PrimaryKey()),\n(new StringField('name', 'name'))-&gt;addFlags(new Required()),\n\n(new OneToOneAssociationField('Firstentity', 'id', 'second_id', FirstentityDefinition::class, false))\n]);\n}\n<\/pre>\n<p><b>Note:<\/b> In the \u2018Secondentity\u2019, there is no FkField necessary.<\/p>\n<p>Now, we have completed the One to One Association here. For One to Many &amp; Many to Many, check out our next blog.<\/p>\n<p>We hope you will find this blog helpful. Still, if you have any queries relevant to Shopware, you can connect with our <a href=\"https:\/\/multisitelocal.ezxdemo.com\/hire-shopware-developer.html\" target=\"_blank\" rel=\"noopener\">Shopware experts<\/a>. They will guide you to take your Shopware projects a level ahead and attain expected results.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will learn about how to add an association to your entities. Three kinds of associations are there: One to One Many to One One to Many Many to Many For this, you will use two entities, \u201cFirstentity\u201d and \u201cSecondentity\u201d Example entity definitions 1. Firstly, you have to define those two entities<\/p>\n","protected":false},"author":36,"featured_media":13168,"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":[84],"class_list":{"0":"post-13164","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-shopware","8":"tag-shopware"},"modified_by":"Marketing EmizenTech","featured_image_src":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2021\/09\/How-to-create-data-associationsOne-to-One-in-Shopware-6-1.jpg","featured_image_src_square":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2021\/09\/How-to-create-data-associationsOne-to-One-in-Shopware-6-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\/13164","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=13164"}],"version-history":[{"count":0,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/13164\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media\/13168"}],"wp:attachment":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media?parent=13164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/categories?post=13164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/tags?post=13164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}