Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Doctrine Orm Bridge Bundle Laravel Package

bengor-user/doctrine-orm-bridge-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install the Bundle:

    composer require bengor-user/doctrine-orm-bridge-bundle
    

    Ensure Symfony >= 2.8 and PHP >= 5.5 are met.

  2. Enable the Bundle: Add to config/bundles.php:

    return [
        // ...
        BenGorUser\DoctrineORMBridgeBundle\BenGorUserDoctrineORMBridgeBundle::class => ['all' => true],
    ];
    
  3. Configure Dependencies: Ensure UserBundle and DoctrineORMBridge are installed and configured. Refer to UserBundle docs for setup.

  4. 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']);
    

Implementation Patterns

Workflows

  1. 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 { ... }
    
  2. 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
    
  3. 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());
            }
        }
    }
    
  4. 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();
        }
    }
    

Gotchas and Tips

Pitfalls

  1. Deprecated Dependencies:

    • The bundle was last updated in 2017. Ensure compatibility with modern Symfony/Doctrine versions (e.g., symfony/security-bundle v5+ may require adjustments).
    • Test with DoctrineORMBridge v2+ if using newer Doctrine ORM versions.
  2. Configuration Overrides:

    • The bundle assumes UserBundle is configured first. Misconfiguration (e.g., duplicate security.user_provider) will cause ServiceNotFoundException.
    • Fix: Explicitly define service priorities in services.yaml:
      services:
          BenGorUser\DoctrineORMBridgeBundle\Bridge\DoctrineUserManager:
              tags: ['security.user_provider']
              priority: 100  # Higher than default UserBundle provider
      
  3. Entity Inheritance:

    • Avoid extending User with mapped superclasses if using Doctrine inheritance. Prefer composition or single-table inheritance (STI) for compatibility.
  4. Event Dispatching:

    • Doctrine events (e.g., 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
          }
      }
      

Debugging

  1. Service Container Issues:

    • If DoctrineUserManager fails to inject, verify:
      • doctrine.orm.entity_manager is registered.
      • No circular dependencies exist (e.g., UserRepository referencing DoctrineUserManager directly).
  2. Query Builder Conflicts:

    • Custom UserRepository methods may clash with UserBundle's DQL. Use QueryBuilder explicitly:
      $qb = $this->createQueryBuilder('u');
      $qb->select('u.id, u.username'); // Avoid SELECT *
      
  3. Symfony Cache:

    • Clear cache after modifying bundle configurations:
      php bin/console cache:clear
      

Extension Points

  1. Custom User Properties: Add fields to User entity and update DoctrineUserManager:

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $customField;
    
    // Getter/Setter
    
  2. 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);
    }
    
  3. 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;
    }
    
  4. 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
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope