Skip to content

alexk136/entity_updater_bundle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Entity Updater Bundle

This Symfony bundle provides safe DTO to Entity conversion functionality.

Features

  • Placeholder class with constants for default "none" values
  • RepositoryHelperTrait for repositories with updateField and findOrFail methods
  • EntityUpdater service for bulk entity updates from arrays

Installation

Add to your composer.json:

{
    "repositories": [
        {
            "type": "path",
            "url": "./modules/autoupdater"
        }
    ],
    "require": {
        "Elrise/entity-updater-bundle": "*"
    }
}

Then run:

composer install

Register the bundle in config/bundles.php:

return [
    // ...
    Elrise\Bundle\EntityUpdaterBundle\EntityUpdaterBundle::class => ['all' => true],
];

Usage

Using RepositoryHelperTrait

In your repository, implement EntityUpdaterRepositoryInterface and use RepositoryHelperTrait:

use Elrise\Bundle\EntityUpdaterBundle\Domain\RepositoryInterface as EntityUpdaterRepositoryInterface;
use Elrise\Bundle\EntityUpdaterBundle\Infrastructure\Doctrine\Repository\RepositoryHelperTrait;

class YourRepository extends ServiceEntityRepository implements EntityUpdaterRepositoryInterface
{
    use RepositoryHelperTrait;

    // Now you can use updateField and findOrFail methods
    public function updateEntity(Entity $entity, array $data): void
    {
        foreach ($data as $field => $value) {
            $this->updateField($entity, $field, $value);
        }
    }
}

Using EntityUpdater Service

Inject the service and use it to update entities:

use Elrise\Bundle\EntityUpdaterBundle\Domain\EntityUpdaterInterface;

class YourService
{
    public function __construct(private EntityUpdaterInterface $entityUpdater) {}

    public function updateEntity(Entity $entity, array $data): void
    {
        $this->entityUpdater->updateEntity($entity, $data);
    }
}

Example

See src/MarketEngine/Infrastructure/Doctrine/Repository/TariffRepository.php for a real-world example of using the RepositoryHelperTrait.

About

This Symfony bundle provides safe DTO to Entity conversion functionality.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages