bengor-user/doctrine-orm-bridge-bundle
Install the Bundle:
composer require bengor-user/doctrine-orm-bridge-bundle
Ensure Symfony >= 2.8 and PHP >= 5.5 are met.
Enable the Bundle:
Add to config/bundles.php:
return [
// ...
BenGorUser\DoctrineORMBridgeBundle\BenGorUserDoctrineORMBridgeBundle::class => ['all' => true],
];
Configure Dependencies:
Ensure UserBundle and DoctrineORMBridge are installed and configured. Refer to UserBundle docs for setup.
First Use Case:
Use the bridge to seamlessly integrate Doctrine ORM with UserBundle's user management. Example:
use BenGorUser\DoctrineORMBridgeBundle\Bridge\DoctrineUserManager;
use Doctrine\ORM\EntityManagerInterface;
$entityManager = $this->get('doctrine')->getManager();
$userManager = new DoctrineUserManager($entityManager);
$user = $userManager->findUserBy(['email' => 'user@example.com']);
User Entity Integration:
Extend BenGorUser\UserBundle\Entity\User with Doctrine ORM annotations or YAML/XML mappings. Example:
use Doctrine\ORM\Mapping as ORM;
use BenGorUser\UserBundle\Entity\User as BaseUser;
/**
* @ORM\Entity(repositoryClass="BenGorUser\DoctrineORMBridgeBundle\Repository\UserRepository")
*/
class User extends BaseUser { ... }
Custom User Manager:
Override DoctrineUserManager to inject custom logic:
use BenGorUser\DoctrineORMBridgeBundle\Bridge\DoctrineUserManager as BaseManager;
class CustomUserManager extends BaseManager {
public function loadUserByUsername($username) {
$user = parent::loadUserByUsername($username);
// Custom logic (e.g., soft-deletes)
return $user;
}
}
Register as a service in services.yaml:
services:
BenGorUser\DoctrineORMBridgeBundle\Bridge\DoctrineUserManager:
alias: App\Security\CustomUserManager
Event Listeners:
Use Doctrine lifecycle events (e.g., prePersist, preUpdate) to sync UserBundle logic with ORM:
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
class UserLifecycleSubscriber implements EventSubscriber {
public function getSubscribedEvents() {
return ['prePersist', 'preUpdate'];
}
public function prePersist(LifecycleEventArgs $args) {
$user = $args->getEntity();
if ($user instanceof User) {
$user->setLastLogin(new \DateTime());
}
}
}
Repository Customization:
Extend UserRepository to add custom queries:
use BenGorUser\DoctrineORMBridgeBundle\Repository\UserRepository as BaseRepository;
class UserRepository extends BaseRepository {
public function findActiveUsers() {
return $this->createQueryBuilder('u')
->where('u.isActive = :active')
->setParameter('active', true)
->getQuery()
->getResult();
}
}
Deprecated Dependencies:
symfony/security-bundle v5+ may require adjustments).DoctrineORMBridge v2+ if using newer Doctrine ORM versions.Configuration Overrides:
UserBundle is configured first. Misconfiguration (e.g., duplicate security.user_provider) will cause ServiceNotFoundException.services.yaml:
services:
BenGorUser\DoctrineORMBridgeBundle\Bridge\DoctrineUserManager:
tags: ['security.user_provider']
priority: 100 # Higher than default UserBundle provider
Entity Inheritance:
User with mapped superclasses if using Doctrine inheritance. Prefer composition or single-table inheritance (STI) for compatibility.Event Dispatching:
postLoad) may conflict with UserBundle events (e.g., user.post_load). Use @Priority annotations to control execution order:
use Symfony\Component\EventDispatcher\Priority\HighestPriority;
class CustomListener {
public function onUserLoad(UserEvent $event) {
// Runs before other listeners
}
}
Service Container Issues:
DoctrineUserManager fails to inject, verify:
doctrine.orm.entity_manager is registered.UserRepository referencing DoctrineUserManager directly).Query Builder Conflicts:
UserRepository methods may clash with UserBundle's DQL. Use QueryBuilder explicitly:
$qb = $this->createQueryBuilder('u');
$qb->select('u.id, u.username'); // Avoid SELECT *
Symfony Cache:
php bin/console cache:clear
Custom User Properties:
Add fields to User entity and update DoctrineUserManager:
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $customField;
// Getter/Setter
Role Management:
Extend role logic by overriding DoctrineUserManager::addRole():
public function addRole(UserInterface $user, $role) {
if (!in_array($role, ['ROLE_ADMIN', 'ROLE_USER'])) {
throw new \InvalidArgumentException('Invalid role');
}
parent::addRole($user, $role);
}
Doctrine Extensions:
Integrate with StoDoctrineExtensionsBundle (e.g., for soft-deletes):
use Gedmo\SoftDeleteable\Entity\SoftDeleteableTrait;
class User extends BaseUser {
use SoftDeleteableTrait;
}
Update DoctrineUserManager to handle soft-deleted users:
public function findUserBy(array $criteria) {
$user = parent::findUserBy($criteria);
if ($user && !$user->isDeleted()) {
return $user;
}
return null;
}
API Platform Integration:
For Symfony API Platform projects, expose User as an API resource:
# config/api_platform/resources.yaml
resources:
App\Entity\User:
collectionOperations:
- GET
itemOperations:
- GET
- PUT
How can I help you explore Laravel packages today?