Installation Add the bundle via Composer:
composer require austral/entity-bundle
Enable the bundle in config/bundles.php:
Austral\EntityBundle\EntityBundle::class => ['all' => true],
First Use Case: Entity Utilities
The bundle provides core utilities for Symfony entities. Start by leveraging its EntityHelper service for common operations:
use Austral\EntityBundle\Helper\EntityHelper;
class SomeService {
public function __construct(private EntityHelper $entityHelper) {}
public function getEntityId(object $entity): ?int {
return $this->entityHelper->getId($entity);
}
}
Key Entry Points
Austral\EntityBundle\Interfaces\TranslateMasterInterface for translation support.ramsey/uuid for UUID handling (e.g., EntityHelper::generateUuid()).Entity Validation & Sanitization
Use EntityHelper::sanitize() to clean entity properties (e.g., trimming strings, converting case):
$entity = $this->entityHelper->sanitize($entity, ['name' => 'trim']);
Dot Notation Access
Leverage adbario/php-dot-notation for nested property access:
$value = $this->entityHelper->getProperty($entity, 'user.address.city');
Translation Handling
For translatable entities (implementing TranslateMasterInterface), use:
$translations = $this->entityHelper->getTranslations($entity);
Migration Utilities
Combine with doctrine/doctrine-migrations-bundle for schema-aware migrations:
use Austral\EntityBundle\Migration\MigrationHelper;
$migrationHelper = new MigrationHelper($entityManager);
$migrationHelper->addColumnIfNotExists('App\Entity\User', 'email_verified', 'boolean');
prePersist) using the bundle’s EntityEvents:
use Austral\EntityBundle\Event\EntityEvents;
$dispatcher->addListener(EntityEvents::PRE_PERSIST, function ($entity) {
// Pre-persist logic
});
Austral\EntityBundle\Interfaces\EntityInterface for consistent entity behavior across projects.gedmo/doctrine-extensions (e.g., timestamps, slugs) out of the box.UUID Generation
EntityHelper::generateUuid() may conflict with existing UUID fields if not handled carefully.$this->entityHelper->generateUuid('custom_uuid_field');
Translation Interface
TranslateMasterInterface checks can fail if the entity doesn’t properly implement the interface.instanceof checks or extend the interface:
if ($entity instanceof \Austral\EntityBundle\Interfaces\TranslateMasterInterface) {
// Safe to use translation methods
}
Doctrine Version Mismatch
2.15.* || ^3.2. Mixing versions may cause errors.composer.json:
"require": {
"doctrine/orm": "^3.2"
}
Dot Notation Edge Cases
getProperty() may throw exceptions for non-existent paths.getPropertySafe() or validate paths first:
$value = $this->entityHelper->getPropertySafe($entity, 'user.address.city');
AUSTRL_ENTITY_DEBUG=true in .env to log entity operations.var_dump(get_class_implements($entity)) to verify implemented interfaces.php bin/console doctrine:migrations:diff after changes to avoid schema issues.Custom Helpers
Extend Austral\EntityBundle\Helper\AbstractEntityHelper to add project-specific methods:
class CustomEntityHelper extends AbstractEntityHelper {
public function customMethod(object $entity) { ... }
}
Register as a service in services.yaml:
services:
App\Helper\CustomEntityHelper:
arguments: ['@doctrine.orm.entity_manager']
tags: ['austral.entity.helper']
Event Listeners
Create listeners for EntityEvents::POST_LOAD or EntityEvents::PRE_REMOVE:
use Austral\EntityBundle\Event\EntityEvents;
$dispatcher->addListener(EntityEvents::POST_LOAD, function ($entity) {
// Post-load logic (e.g., lazy-loading)
});
Doctrine Extensions
Override default behaviors by binding custom extensions to the EntityManager:
$em->getConfiguration()->addCustomStringFunction(
'CONCAT_WS',
'Austral\EntityBundle\DBAL\Function\ConcatWs'
);
How can I help you explore Laravel packages today?