Last Updated on January 21, 2022

In our last blog, you learned how to create One to One data associations for your entities. In this blog, you will learn about One to Many & Many to One

Let’s start with the process of creating data associations in Shopware 6.

For this, you will use two entities “Firstentity”  and “Secondentity” (which was created in our last blog please refer to that blog “How to create data associations(One to One) in Shopware 6

Now we will see our data associatons example first is “One to Many/Many to One” associations. One to Many/Many to One associations require you need to define a foreign key for “Many to One”. E.g. the Secondentity table has multiple records for the Firstentity table, Therefore you have to add the first_id column to the Secondentity table: In this example, it will be second_id in the FirstentityDefinition

Now We Will Look At The Example:

Emizentechplugin/src/Core/Content/Firstentity/FirstentityDefinition.php

Note: also add the use statement for SecondentityDefinition and OneToManyAssociationField*

protected function defineFields(): FieldCollection
{

return new FieldCollection([

(new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),

new OneToManyAssociationField('secondentity', SecondentityDefinition::class, 'first_id')

]);

}
Hire Shopware developers

Parameters Of One-To-Many Association Field

1. Secondentity

It s the property that will contain all secondentity’s the class name of SecondentityDefinition.

2. first_id

The name of the column in the referenced table points to the definition itself.

Now you will add code for the Secondentity for completion of the ManytoOne association example.

Emizentechplugin/src/Core/Content/Secondentity/SecondentityDefinition.php

Note: also add the use statement for FirstentityDefinition and ManyToOneAssociation*

protected function defineFields(): FieldCollection
{

return new FieldCollection([

(new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),

(new FkField('first_id', 'firstId', FirstentityDefinition::class))->addFlags(new PrimaryKey(), new Required()),

new ManyToOneAssociationField('Firstentity', 'first_id', FirstentityDefinition::class, 'id', false),

]);

}

For Many-To-One Association

first_id: new FkField, which is the field for the new first_id column.

Many-To-One AssociationField Parameter

Firstentity: You have to apply the name of the property, which will contain the single FirstentityDefinition(Entity name or table name)
first_id: The name of the column, which references the inverse side entity.
FirstentityDefinition: The class of the referenced definition and the name of the ID column in the definition’s database table itself.

Now you have completed your One to Many & Many to One association. For Many to Many, it will be covered in our next blog.

Wrapping Up

We hope you find this blog to be helpful in creating data associations in Shopware 6. Please do not hesitate to contact us if you have any questions about this. we are the best Shopware development company that would be delighted to assist you.

Author

Founder and tech lead at Emizentech, Mr. Vivek has over ten years of experience in developing IT infrastructures and solutions. With his profound knowledge in eCommerce technologies like Shopware, Magento, and Shopify, Mr. Vivek has been assisting SMEs to enterprises across the globe by developing and maintaining their eCommerce applications. Technology innovation and trends insight come easy to Vivek with his thorough knowledge in the eCommerce domain. See him talking about ideas, trends, and technology in this blog. To know more about how Team Vivek can assist you in your eCommerce strategy? Connect team Vivek here.