PHPackages                             brokalia/doctrine-entity-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. brokalia/doctrine-entity-generator

ActiveSymfony-bundle

brokalia/doctrine-entity-generator
==================================

Generates a doctrine entity and mapper from domain entity

0.1.0(3y ago)22.7kMITPHPPHP &gt;=8.1

Since Nov 18Pushed 3y ago2 watchersCompare

[ Source](https://github.com/Brokalia/doctrine-entity-generator)[ Packagist](https://packagist.org/packages/brokalia/doctrine-entity-generator)[ RSS](/packages/brokalia-doctrine-entity-generator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

Doctrine Entity Generator
=========================

[](#doctrine-entity-generator)

This Symfony bundle provides a console command to generate doctrine entities with mapping attributes and a mapper class from domain entity class.

Then you can use domain entities in your domain and application layers and map it to doctrine entities for persistence.

Usage
-----

[](#usage)

```
bin/console doctrine-generator:entity "App\Domain\MyDomainEntity"
```

Conventions
-----------

[](#conventions)

The primary key of the doctrine entity must be the "id" property of domain entity. If there are not an "id" property in the domain entity, the doctrine entity will not have primary key.

```
class MyDomainEntity {
    private string $id; // Will be the doctrine primary key
}

class DoctrineMyDomainEntity {
    #[ORM\Column(type: 'string')]
    #[ORM\Id]
    public string $id;
}
```

Complete Example
----------------

[](#complete-example)

Given MyDomainEntity with some value objects:

```
// src/Domain/MyDomainEntity.php
class MyDomainEntity {
    public function __construct(
        private MyDomainEntityId $id,
        private SampleValueObject $valueObject,
    ) {
    }

    public function getId(): MyDomainEntityId
    {
        return $this->id;
    }

    public function getValueObject(): SampleValueObject
    {
        return $this->valueObject;
    }
}
```

```
// src/Domain/MyDomainEntityId.php
class MyDomainEntityId
{
    public function __construct(private string $value)
    {
    }

    public function getValue(): string
    {
        return $this->value;
    }
}
```

```
// src/Domain/EntityId.php
class SampleValueObject
{
    public function __construct(
        private string $firstAttribute,
        private int $secondAttribute
    ) {
    }

    public function getFirstAttribute(): string
    {
        return $this->firstAttribute;
    }

    public function getSecondAttribute(): int
    {
        return $this->secondAttribute;
    }
}
```

Generates a doctrine entity and a mapper to map between domain and doctrine entities:

```
// src/Infrastructure/Persistence/DoctrineMyDomainEntity.php
class DoctrineMyDomainEntity
{
    #[ORM\Column(type: 'string')]
    #[ORM\Id]
    public string $id;

    #[ORM\Column(type: 'string')]
    public string $valueObject_firstAttribute;

    #[ORM\Column(type: 'integer')]
    public int $valueObject_secondAttribute;
}
```

```
// src/Infrastructure/Persistence/DoctrineMyDomainEntityMapper.php
class DoctrineMyDomainEntityMapper
{
    public function fromDomain(
        MyDomainEntity $domainEntity,
        ?DoctrineMyDomainEntity $doctrineEntity,
    ): DoctrineMyDomainEntity {
        $doctrineEntity = $doctrineEntity ?? new DoctrineMyDomainEntity();

        $doctrineEntity->id = $domainEntity->getId()->getValue();
        $doctrineEntity->valueObject_firstAttribute = $domainEntity->getValueObject()->getFirstAttribute();
        $doctrineEntity->valueObject_secondAttribute = $domainEntity->getValueObject()->getSecondAttribute();

        return $doctrineEntity;
    }

    public function toDomain(DoctrineMyDomainEntity $doctrineEntity): MyDomainEntity
    {
        $reflector = new ReflectionClass(MyDomainEntity::class);
        $constructor = $reflector->getConstructor();
        if (!$constructor) {
            throw new RuntimeException('No constructor');
        }
        $object = $reflector->newInstanceWithoutConstructor();
        $constructor->invoke(
            $object,
            new MyDomainEntityId($doctrineEntity->id),
            new SampleValueObject(
                $doctrineEntity->valueObject_firstAttribute,
                $doctrineEntity->valueObject_secondAttribute
            ),
        );
        return $object;
    }
}
```

Now you can create a repository for the domain entity using doctrine to persist the entity with the mapper.

```
class MyDomainEntityRepository {
    public function __construct(
        private EntityManagerInterface $entityManager,
        private DoctrineMyDomainEntityMapper $mapper,
    ) {
    }

    public function save(MyDomainEntity $entity): void
    {
        // Get previous existent doctrine entity if exists for update cases
        $existentDoctrineEntity = $this->findDoctrineEntity($entity->getId()->getValue());

        // Map domain entity to doctrine entity
        $doctrineEntity = $this->mapper->fromDomain(
            $entity,
            $existentDoctrineEntity ?? new DoctrineMyDomainEntity()
        );

        // Persist
        $this->entityManager->persist($doctrineEntity);
        $this->entityManager->flush();
    }

    public function findById(MyDomainEntityId $id): ?MyDomainEntity
    {
        // Get doctrine entity
        $doctrineEntity = $this->findDoctrineEntity($id->getValue());

        if (!$doctrineEntity) {
            return null;
        }

        // Return domain entity mapped
        return $this->mapper->toDomain($doctrineEntity);
    }

    private function findDoctrineEntity(string $id): ?DoctrineMyDomainEntity
    {
        return $this->entityManager->getRepository(DoctrineMyDomainEntity::class)->find($id);
    }
}
```

Installation
============

[](#installation)

Make sure Composer is installed globally, as explained in the [installation chapter](https://getcomposer.org/doc/00-intro.md)of the Composer documentation.

Applications that use Symfony Flex
----------------------------------

[](#applications-that-use-symfony-flex)

Open a command console, enter your project directory and execute:

```
$ composer require --dev brokalia/doctrine-entity-generator
```

Applications that don't use Symfony Flex
----------------------------------------

[](#applications-that-dont-use-symfony-flex)

### Step 1: Download the Bundle

[](#step-1-download-the-bundle)

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

```
$ composer require --dev brokalia/doctrine-entity-generator
```

### Step 2: Enable the Bundle

[](#step-2-enable-the-bundle)

Then, enable the bundle by adding it to the list of registered bundles in the `config/bundles.php` file of your project:

```
// config/bundles.php

return [
    // ...
    Brokalia\DoctrineEntityGenerator\DoctrineEntityGeneratorBundle::class => ['dev' => true],
];
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1270d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6af9ec4afbc88e277fea99d16c0cea84c2ca205bd8a8db32949a70d679477ccd?d=identicon)[brokalia](/maintainers/brokalia)

---

Top Contributors

[![chuano](https://avatars.githubusercontent.com/u/779929?v=4)](https://github.com/chuano "chuano (16 commits)")

### Embed Badge

![Health badge](/badges/brokalia-doctrine-entity-generator/health.svg)

```
[![Health](https://phpackages.com/badges/brokalia-doctrine-entity-generator/health.svg)](https://phpackages.com/packages/brokalia-doctrine-entity-generator)
```

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M651](/packages/sylius-sylius)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[prestashop/prestashop

PrestaShop is an Open Source e-commerce platform, committed to providing the best shopping cart experience for both merchants and customers.

9.0k15.4k](/packages/prestashop-prestashop)[open-dxp/opendxp

Content &amp; Product Management Framework (CMS/PIM)

7310.3k29](/packages/open-dxp-opendxp)[rcsofttech/audit-trail-bundle

Enterprise-grade, high-performance Symfony audit trail bundle. Automatically track Doctrine entity changes with split-phase architecture, multiple transports (HTTP, Queue, Doctrine), and sensitive data masking.

1022.4k](/packages/rcsofttech-audit-trail-bundle)[leapt/core-bundle

Symfony LeaptCoreBundle

2529.1k4](/packages/leapt-core-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
