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, “Firstentity” and “Secondentity”

Example entity definitions

1. Firstly, you have to define those two entities on this path “Emizentechplugin/src/Core/Content”.

2. There are two entities, so that you have to create two folders with the entity name first “Firstentity” and for the second “Secondentity” and then define the respective definition classes for your entity like this :

Emizentechplugin/src/Core/Content/Firstentity/FirstentityDefinition.php
?php declare(strict_types=1);

namespace Emizentechplugin\src\Core\Content\Firstentity;

use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\PrimaryKey;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Required;
use Shopware\Core\Framework\DataAbstractionLayer\Field\IdField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;

class FirstentityDefinition extends EntityDefinition
{
public const ENTITY_NAME = 'Firstentity';

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

protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),
// Other fields here
]);
}
}
?>
Emizentechplugin/src/Core/Content/Secondentity/Secondentity Definition.php
<?php declare(strict_types=1);

namespace Emizentechplugin\src\Core\Content\Secondentity;

use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\PrimaryKey;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Required;
use Shopware\Core\Framework\DataAbstractionLayer\Field\IdField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;

class SecondentityDefinition extends EntityDefinition
{
public const ENTITY_NAME = 'Secondentity';

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

protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),
// Other fields here
]);
}
}

3. Now we will see our data associations example first is “One to One” associations.

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

Now, we will look at the example:

Emizentechplugin/src/Core/Content/Firstentity/FirstentityDefinition.php
note: also add the use statement for SecondentityDefinition*
protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),
(new FkField('second_id', 'secondId', SecondentityDefinition::class))->addFlags(new Required()),
(new StringField('name', 	'name'))->addFlags(new Required()),

new OneToOneAssociationField('Secondentity', 'second_id', 'id', SecondentityDefinition::class, false)
]);
}

For OneToOneAssociationField

  • Secondentity: It is the property name that should contain the associated entity in your respective definition.
  • second_id: It is the name of the column in the database.
  • id: It is the ID column in the referenced database ( Secondentity in this case) and the referenced definition.
  • SecondentityDefinition: It is used for the property “Secondentity,” i.e., SecondentityDefinition will appear in Secondentity property that will make the association for that class and the property.
  • false -> if you want to automatically load this association every time you load a ‘Firstentity’ entity. We’ve set this to ‘false’.

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

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

Note: also add the use statement for FirstentityDefinition*

protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),
(new StringField('name', 'name'))->addFlags(new Required()),

(new OneToOneAssociationField('Firstentity', 'id', 'second_id', FirstentityDefinition::class, false))
]);
}

Note: In the ‘Secondentity’, there is no FkField necessary.

Now, we have completed the One to One Association here. For One to Many & Many to Many, check out our next blog.

We hope you will find this blog helpful. Still, if you have any queries relevant to Shopware, you can connect with our Shopware experts. They will guide you to take your Shopware projects a level ahead and attain expected results.

Avatar photo
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.

whatsapp