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. [Database &amp; ORM](/categories/database)
4. /
5. brokalia/doctrine-entity-generator

ActiveSymfony-bundle[Database &amp; ORM](/categories/database)

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 yesterday

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 35% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity45

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

1324d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/75415385?v=4)[Brokalia](/maintainers/brokalia)[@Brokalia](https://github.com/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

[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.

1189.8k](/packages/rcsofttech-audit-trail-bundle)[easycorp/easyadmin-bundle

Admin generator for Symfony applications

4.3k17.9M387](/packages/easycorp-easyadmin-bundle)[kimai/kimai

Kimai - Time Tracking

4.8k9.0k1](/packages/kimai-kimai)[oro/platform

Business Application Platform (BAP)

645143.5k115](/packages/oro-platform)[2lenet/crudit-bundle

The easy like Crud'it Bundle.

1616.4k14](/packages/2lenet-crudit-bundle)[ahmed-bhs/doctrine-doctor

Runtime analysis tool for Doctrine ORM integrated into Symfony Web Profiler. Unlike static linters, it analyzes actual query execution at runtime to detect performance bottlenecks, security vulnerabilities, and best practice violations during development with real execution context and data.

9410.8k](/packages/ahmed-bhs-doctrine-doctor)

PHPackages © 2026

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