Last Updated on January 21, 2022

In our last blog, you learned how to create One to Many & Many to One data associations for your entities. In this blog, you will learn about Many to Many
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” )

1. Now we will see our data associations example first is “Many to Many” associations.

ManyToMany associations require another, third entity to be available. It will be called “FirstentitySecondentityMappingDefinition” 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.

Emizentechplugin\Core\Content\FirstentitySecondentityMappingDefinition

<?php declare(strict_types=1); 
namespace Emizentechplugin\Core\Content; 

use Shopware\Core\Framework\DataAbstractionLayer\Field\CreatedAtField; 
use Shopware\Core\Framework\DataAbstractionLayer\Field\FkField; 
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\PrimaryKey; 
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Required; 
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToOneAssociationField; 
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection; 
use Shopware\Core\Framework\DataAbstractionLayer\MappingEntityDefinition; 
use Emizentechplugin\Core\Content\Firstentity\FirstentityDefinition; 
use Emizentechplugin\Core\Content\Secondentity\SecondentityDefinition; 

class FirstentitySecondentityMappingDefinition extends MappingEntityDefinition 
{ 
public const ENTITY_NAME = 'Firstentity_Secondentity; 

public function getEntityName(): string {
 	return self::ENTITY_NAME;
} 

protected function defineFields(): FieldCollection 
{
 return new FieldCollection([
 	(new FkField('second_id', 'secondId', SecondentityDefinition::class))->addFlags(new PrimaryKey(), new 	Required()),
	(new FkField('first_id', 'firstId', FirstentityDefinition::class))->addFlags(new PrimaryKey(), new 	Required()), 
new ManyToOneAssociationField('Secondentities', 'second_id', SecondentityDefinition::class, 'id'),
new ManyToOneAssociationField('Firstentities', first_id', FirstentityDefinition::class, 'id') 
]); 
} 
}

Explanation:

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 “MappingEntityDefinition” because it is a mapping definition connecting two entity definitions.

Fields:

FkField:  Both primary fields from the other two entity definition as “FkField”.

ManyToOneAssociationField: In this Mapping Definition  “ManyToOneAssociationField” 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.

Now the main Definitions which we are connecting for ManytoMany

Emizentechplugin\Core\Content\Firstentity

Note:- please add use statement for  “FirstentitySecondentityMappingDefinition” and  “SecondentityDefinition

protected function defineFields(): FieldCollection 
{ 
	return new FieldCollection([ 
	(new IdField('id', 'id'))->addFlags(new Required(),new PrimaryKey()), 
	new ManyToManyAssociationField( 'Secondentities', SecondentityDefinition::class, 	FirstentitySecondentityMappingDefinition::class, 'second_id', 'first_id' ), 
	]); 
}

Emizentechplugin\Core\Content\Secondentity

Note:- please add use statement for  “FirstentitySecondentityMappingDefinition” and  “FirstentityDefinition

protected function defineFields(): FieldCollection 
{ 
	return new FieldCollection([ 
	(new IdField('id', 'id'))->addFlags(new Required(),new PrimaryKey()), 
	new ManyToManyAssociationField( 'Firstentities', FirstentityDefinition::class, 	FirstentitySecondentityMappingDefinition::class, 'first_id', 'second_id' ), 
	]); 
}

ManyToManyAssociationField Explanation:

propertyName:  “Secondentities” and “Firstentities” are property name which is passed and which will contain associated entities.

referenceDefinition: The class of the associated definition to which we are asscociationg our entity “SecondentityDefinition” and “FirstentityDefinition

mappingDefinition: The class of the mapping definition “FirstentitySecondentityMappingDefinition”.

mappingLocalColumn: The name of the id column for the current entity, first_id if you’re in the FirstentityDefinition.

mappingReferenceColumn: The name of the id column for the referenced entity.


And that’s your Many to Many association established properly,
Now you have completed your Many to Many associations

Wrapping Up

We hope this cleared your doubts regarding creating data associations in Shopware 6. For more professional assistance do get in touch with Emizentech a Shopware development company dedicated to providing the best ecommerce solutions.

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.