dcs/user-persistence-orm-bundle
Install Dependencies
composer require damianociarla/dcs-user-persistence-orm-bundle
Enable the Bundle
Add to config/bundles.php:
return [
// ...
Damianociarla\DCSUserPersistenceORMBundle\DCSUserPersistenceORMBundle::class => ['all' => true],
];
Configure Core Bundle
Update config/packages/dcs_user_core.yaml:
dcs_user_core:
repository_service: dcs_user.persistence.orm.repository
Run Migrations
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Fetch a User
use DCS\User\CoreBundle\Model\User;
use DCS\User\CoreBundle\Repository\UserRepositoryInterface;
class SomeService {
public function __construct(
private UserRepositoryInterface $userRepository
) {}
public function findUser(int $id): ?User {
return $this->userRepository->find($id);
}
}
Event-Driven Persistence
The bundle listens to DCSUserPersistenceORMEvents (e.g., UserSaveEvent, UserDeleteEvent) to auto-persist User entities via Doctrine.
Repository Integration
UserRepositoryInterface (auto-wired) for CRUD operations.DCSUserPersistenceORMEvents to add custom logic (e.g., pre-save validation).Custom User Mapping
Override the default User entity mapping by extending the base class:
use DCS\User\CoreBundle\Model\User as BaseUser;
class CustomUser extends BaseUser {
// Add custom fields
}
Service Integration
// Auto-persist a user via event
$user = new User();
$this->eventDispatcher->dispatch(new UserSaveEvent($user));
Bulk Operations
$users = $this->userRepository->findBy(['role' => 'admin']);
foreach ($users as $user) {
$user->setActive(false);
$this->eventDispatcher->dispatch(new UserSaveEvent($user));
}
Soft Deletes
Configure DCSUserPersistenceORMEvents::USER_DELETE to use soft-delete logic:
# config/packages/dcs_user_persistence_orm.yaml
dcs_user_persistence_orm:
soft_delete: true
Event Dispatching Order
Ensure DCSUserPersistenceORMBundle is loaded after DCSUserCoreBundle in bundles.php to avoid missing events.
Doctrine Mapping Conflicts
If extending User, ensure custom fields are annotated with @ORM\Column or mapped in orm.xml:
use Doctrine\ORM\Mapping as ORM;
class CustomUser extends BaseUser {
/**
* @ORM\Column(type="string", nullable=true)
*/
private ?string $customField;
}
Migration Issues
Run doctrine:schema:update --force if migrations fail due to schema changes.
Event Listeners Log events to verify they’re dispatched:
$this->eventDispatcher->addListener(
DCSUserPersistenceORMEvents::USER_SAVE,
function (UserSaveEvent $event) {
error_log('User saved: ' . $event->getUser()->getId());
}
);
Repository Debugging
Enable Doctrine debug mode in .env:
DOCTRINE_ORM_OPTIMIZER_ENABLED=false
Custom Events
Extend DCSUserPersistenceORMEvents for domain-specific logic:
class CustomUserEvents {
public const USER_PRE_SAVE = 'user.pre_save';
}
Repository Decorators Decorate the default repository to add methods:
# config/services.yaml
services:
App\Service\CustomUserRepository:
decorates: 'dcs_user.persistence.orm.repository'
arguments: ['@dcs_user.persistence.orm.repository']
Doctrine Lifecycle Callbacks
Use @ORM\PrePersist/@ORM\PreRemove in custom User extensions for pre-persistence logic.
How can I help you explore Laravel packages today?