This Symfony bundle provides safe DTO to Entity conversion functionality.
Placeholderclass with constants for default "none" valuesRepositoryHelperTraitfor repositories withupdateFieldandfindOrFailmethodsEntityUpdaterservice for bulk entity updates from arrays
Add to your composer.json:
{
"repositories": [
{
"type": "path",
"url": "./modules/autoupdater"
}
],
"require": {
"Elrise/entity-updater-bundle": "*"
}
}Then run:
composer installRegister the bundle in config/bundles.php:
return [
// ...
Elrise\Bundle\EntityUpdaterBundle\EntityUpdaterBundle::class => ['all' => true],
];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);
}
}
}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);
}
}See src/MarketEngine/Infrastructure/Doctrine/Repository/TariffRepository.php for a real-world example of using the RepositoryHelperTrait.