PHP code example of brokalia / doctrine-entity-generator

1. Go to this page and download the library: Download brokalia/doctrine-entity-generator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

brokalia / doctrine-entity-generator example snippets


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

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

// 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;
    }
}

// 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;
    }
}

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);
    }
}

// config/bundles.php

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