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

Repository Alias Bundle Laravel Package

codemonkeys-ru/repository-alias-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:
    composer require codemonkeys-ru/repository-alias-bundle
    
  2. Register Bundle: Add to AppKernel.php:
    new CodeMonkeysRu\RepositoryAliasBundle\RepositoryAliasBundle(),
    
  3. Configure Aliases (config.yml):
    repository_alias:
        repository_key: "app.repo"  # Custom prefix for your repositories
        repository:
            user: AppBundle:User
            product: AppBundle:Product
    

First Use Case

Replace verbose repository calls:

// Before
$repo = $this->getDoctrine()->getRepository('AppBundle:User');

// After
$repo = $this->get('app.repo.user');

Implementation Patterns

Core Workflow

  1. Define Aliases: Use YAML/XML/annotation to map short names (e.g., user) to Doctrine entities (e.g., AppBundle:User).

    repository:
        admin: AppBundle:AdminUser
        category: AppBundle:Category
    
  2. Inject via DI:

    class UserService {
        public function __construct(
            private RepositoryAlias $repositoryAlias
        ) {}
    
        public function getUserRepo() {
            return $this->repositoryAlias->get('app.repo.user');
        }
    }
    
  3. Leverage Shortcuts:

    // Direct service call
    $repo = $this->get('app.repo.category');
    
    // With entity creation helper
    $category = $this->get('app.repo.category')->newEntity('Electronics');
    

Integration Tips

  • Symfony Forms: Use aliases in form type factories to simplify repository access:

    $builder->add('category', EntityType::class, [
        'class' => $this->get('app.repo.category')->getEntityName(),
    ]);
    
  • Command Bus: Pass repository aliases as arguments to commands/services:

    $command->setRepository($this->get('app.repo.user'));
    
  • Event Subscribers: Access repositories via alias in event handlers:

    $user = $this->get('app.repo.user')->find($event->getUserId());
    

Gotchas and Tips

Pitfalls

  1. Namespace Collisions: Avoid overlapping repository keys (e.g., user and user_profile). Use prefixes like admin.user or api.user.

  2. Caching Quirks: Clear Symfony cache after changing config.yml:

    php bin/console cache:clear
    
  3. Doctrine Proxy Issues: If using proxies, ensure the alias bundle’s getOriginalRepository() method is called when needed:

    $originalRepo = $this->get('app.repo.user')->getOriginalRepository();
    
  4. Deprecated Methods: Avoid getAliasFor() (v0.1.2) in new code; prefer direct service injection.

Debugging

  • Verify Alias Exists: Check if the alias is registered in config.yml and the bundle is enabled in AppKernel.php.

    php bin/console debug:container | grep app.repo
    
  • Entity Not Found: Ensure the FQCN (e.g., AppBundle:User) matches your entity namespace and Doctrine metadata.

Extension Points

  1. Dynamic Aliases: Override the RepositoryAliasExtension to load aliases dynamically (e.g., from a database):

    # config.yml
    repository_alias:
        extensions:
            - AppBundle\DynamicRepositoryAliasExtension
    
  2. Custom Repository Methods: Decorate repositories to add bundle-specific methods:

    $repo = $this->get('app.repo.user');
    $repo->customMethod(); // If extended via decorator
    
  3. Testing: Mock the RepositoryAlias service in tests:

    $this->container->set('app.repo.user', $this->createMock(Repository::class));
    
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky