Thank you! See you In the next blog post.
How to create Entity, EntityDefinition, EntityHydrator and EntityCollection in Shopware 6?
- Darpan Chaudhari
- eCommerce Talk
- Jan 15, 2026
- Reading time: 8 minutes

Hello, we are today bringing a simple tutorial on How to create Entity, EntityDefinition, EntityHydrator, and EntityCollection in Shopware 6? And if you are looking on more advanced information do check it here
These are the classes linked to entities, along with a brief explanation of why they are required:
| Class Type | Explanation | Example |
| Entity | It is a description of the entity. |
Customer Product MSBlogs |
| Entity Hydrator | This class fills up the database with data |
CustomerHydrator ProductHydrator MSBlogsHydrator |
| EntityDefinition | It Defines the Entity name and the Entity properties. This will be used in the collection. |
ProductDefinition CustomerDefinition MSBlogsDefinition |
| EntityCollection | This class offers an attractive approach to interacting with a list of entities by using customized filters and scorers instead of basic collections. |
CustomerCollection ProductCollection MSBlogsCollection |
Naming Customs
It is critical to understand the domain to which your entity is tied. There are a few domains in which your entity might exist; however, if you are building a generic entity, it should be placed in the Content domain. Other potential domains include Checkout, Framework, and System.
So, please create the directory src/Content at the root of your plugin so that we may place the PHP files there.
EntityDefinition Class
As the name, this class defines the entity name, and entity properties. Remember the entity name should be matched with the DB table name for which you are creating the entity. In our case, it should be magespark_blogs. As such, we have created a table with this name in the previous blog.
<?php
namespace MageSpark\HelloWorld\Core\Content\MSBlog;
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\Field\StringField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;
class MSBlogsDefinition extends EntityDefinition{
public const ENTITY_NAME = 'magespark_blogs';
public function getEntityName(): string
{
return self::ENTITY_NAME;
}
public function getEntityClass(): string
{
return MSBlogsEntity::class;
}
protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),
(new StringField('name', 'name'))->addFlags(new Required())
]);
}
}
As you can see, we've implemented an IdField for the id column, a Stringfield for the name.
All that's left to do now, is to introduce your MSBlogsDefinition to Shopware by registering your class in your services.xml
services.xml
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="MageSpark\HelloWorld\Core\Content\MSBlog\MSBlogDefinition">
<tag name="shopware.entity.definition" entity="magespark_blogs" />
</service>
</services>
</container>
Entity Class
The entity class itself is a simple key-value object, like a struct, which contains as many properties as fields in the definition, ignoring the ID field, which is handled by the EntityIdTrait.
We will have two attributes: id, name. The attributes created and updated are being handled by Shopware itself. Please create the MSBlogsEntity.php file on the Content directory:
We have used this class in the getEntityClass method of MSBlogsEntityDefinition class.
<?php
namespace MageSpark\HelloWorld\Core\Content\MSBlog;
use Shopware\Core\Framework\DataAbstractionLayer\Entity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityIdTrait;
class MSBlogsEntity extends Entity
{
use EntityIdTrait;
/**
* @var string
*/
protected string $name;
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @param string $name
* @return void
*/
public function setName(string $name): void
{
$this->name = $name;
}
}
EntityHydrator Class
Allows hydrating database values into struct objects. Make the MSBlogsHydrator.php file, which is as follows:
<?php
namespace MageSpark\HelloWorld\Core\Content\MSBlog;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\EntityHydrator;
use Shopware\Core\Framework\DataAbstractionLayer\Entity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
use Shopware\Core\Framework\Uuid\Uuid;
class MSBlogsHydrator extends EntityHydrator
{
protected function assign(EntityDefinition $definition, Entity $entity, string $root, array $row, Context $context): Entity
{
if (isset($row[$root . '.id'])) {
$entity->id = Uuid::fromBytesToHex($row[$root . '.id']);
}
if (isset($row[$root . '.name'])) {
$entity->name = $row[$root . '.name'];
}
return $entity;
}
}
EntityCollection
Make the MSBlogsCollection.php in the same directory as your MSBlogsDefinition and MSBlogsEntity.
This class extends the Shopware\Core\Framework\DataAbstractionLayer\EntityCollection to override the getExpectedClass method.
This method should return the EntityClass. So, in our case it should return the MSBlogsEntity class.
<?php
namespace MageSpark\HelloWorld\Core\Content\MSBlog;
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
/**
* @method void add(MSBlogsEntity $entity)
* @method void set(string $key, MSBlogsEntity $entity)
* @method MSBlogsEntity[] getIterator()
* @method MSBlogsEntity[] getElements()
* @method MSBlogsEntity|null get(string $key)
* @method MSBlogsEntity|null first()
* @method MSBlogsEntity|null last()
*/
class MSBlogsCollection extends EntityCollection
{
public function getExpectedClass(): string
{
return MSBlogsEntity::class;
}
}
We must introduce this class in the MSBlogsEntityDefinition. To do that, we have to override the getCollectionClass method of EntityDefinition class using MSBlogsEntityDefinition class.
Final words
Great! Our entity, as well as all entity-related classes, has been constructed! This is how the final structure should appear:
MageSparkHelloWorld
└── src
└── Content
└── MSBlog
├── MSBlogsCollection.php
├── MSBlogsDefinition.php
├── MSBlogsEntity.php
└── MSBlogsHydrator.php
