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

Doctine Prefixr Bundle Laravel Package

chellem/doctine-prefixr-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation: Add the package via Composer:

    composer require chellem/doctine-prefixr-bundle:dev-master
    

    Register the bundle in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3/2):

    return [
        // ...
        DoctrinePrefixr\Bundle\DoctrinePrefixrBundle\DoctrinePrefixrBundle::class => ['all' => true],
    ];
    
  2. Basic Configuration: Define prefixes in config/packages/doctrine_prefixr.yaml (Symfony 4+):

    doctrine_prefixr:
        prefixes:
            App: app_
            Admin: admin_
    
  3. First Use Case: Create an entity (e.g., src/Entity/User.php) in the App namespace. The generated table name will automatically be prefixed with app_ (e.g., app_user).


Implementation Patterns

Workflows

  1. Namespace-to-Prefix Mapping:

    • Use the prefixes key in config to map bundles/namespaces to database prefixes.
    • Example: AcmeBlog: blog_ → All entities in AcmeBlogBundle get blog_ prefix.
    • Laravel Adaptation: Mimic this in config/doctrine_prefixr.php (if using a bridge like spatie/laravel-doctrine-orm).
  2. Entity Generation:

    • Prefixes are applied during schema tool generation (e.g., php artisan doctrine:schema:update --force).
    • No manual table name changes needed in entities (unlike table="[prefix]_users").
  3. Multi-Environment Prefixes:

    • Override prefixes per environment (e.g., config/doctrine_prefixr_dev.yaml):
      doctrine_prefixr:
          prefixes:
              App: dev_app_
      

Integration Tips

  1. Doctrine Events: Listen to Doctrine\ORM\Tools\Event\GenerateSchemaTableEvent to customize prefix logic dynamically:

    // src/EventListener/PrefixListener.php
    public function onGenerateSchemaTable(GenerateSchemaTableEvent $event) {
        $entity = $event->getEntity();
        $prefix = $this->prefixResolver->resolve($entity->getNamespace());
        $event->setTableName($prefix . '_' . $event->getTableName());
    }
    
  2. Laravel Migration Bridge: If using Laravel migrations, ensure the bundle’s schema tool is triggered post-migration or use a custom migration service provider.

  3. Testing: Mock the prefix resolver in tests:

    $resolver = $this->createMock(PrefixResolver::class);
    $resolver->method('resolve')->willReturn('test_');
    $this->app->instance(PrefixResolver::class, $resolver);
    

Gotchas and Tips

Pitfalls

  1. Case Sensitivity:

    • Prefix keys in config are case-sensitive (AcmeDemoacmeDemo). Use consistent casing.
  2. Schema Tool Conflicts:

    • If using doctrine:schema:update, ensure no manual table="..." annotations in entities override the prefix.
    • Fix: Remove annotations or adjust bundle priority in AppKernel.php.
  3. Archived Package:

    • No active maintenance. Fork or patch locally if critical bugs arise (e.g., Symfony 5+ compatibility).
  4. Namespace Collisions:

    • Avoid overlapping prefixes (e.g., App: app_ and AppAdmin: app_). Use unique prefixes like app_core_.

Debugging

  1. Verify Prefixes: Dump the resolved prefix for an entity:

    $entityManager->getMetadataFactory()->getMetadataFor('App\Entity\User')->getTableName();
    
  2. Schema Dump: Generate a schema dump to inspect table names:

    php artisan doctrine:schema:dump --path=schema.sql
    
  3. Event Debugging: Log the GenerateSchemaTableEvent to trace prefix application:

    $event->getTableName(); // Check if prefix is applied
    

Extension Points

  1. Custom Prefix Resolver: Override the default resolver (e.g., DoctrinePrefixr\Resolver\PrefixResolver) to add logic like:

    • Dynamic prefixes based on environment variables.
    • Excluding specific entities from prefixing.
  2. Post-Install Hooks: Add a command to retroactively prefix existing tables:

    // src/Command/PrefixTablesCommand.php
    public function handle() {
        $tables = $this->getExistingTables();
        foreach ($tables as $table) {
            $newName = $this->prefixResolver->resolve($table['namespace']) . '_' . $table['name'];
            $this->renameTable($table['name'], $newName);
        }
    }
    
  3. Laravel Eloquent Integration: If using Eloquent, create a trait to auto-apply prefixes:

    trait PrefixedTable {
        protected static function bootPrefixedTable() {
            static::setTable(config('doctrine_prefixr.prefixes.' . class_basename(static::class)) . '_' . static::getTable());
        }
    }
    

```markdown
### Laravel-Specific Notes
1. **Doctrine ORM in Laravel**:
   Use `spatie/laravel-doctrine-orm` to integrate Doctrine with Laravel. Configure the bundle in `config/doctrine.php`:
   ```php
   'doctrine_prefixr' => [
       'prefixes' => [
           'App\Models' => 'app_',
       ],
   ],
  1. Artisan Commands: Register the bundle’s commands in AppServiceProvider:

    public function boot() {
        $this->commands([
            DoctrinePrefixr\Command\GenerateSchemaCommand::class,
        ]);
    }
    
  2. Migration Conflicts: Disable Doctrine’s schema tool during migrations if conflicts arise:

    // config/doctrine.php
    'schema_tool' => [
        'enabled' => env('DOCTRINE_SCHEMA_TOOL_ENABLED', false),
    ],
    
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.
facebook/capi-param-builder-php
babelqueue/symfony
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