chellem/doctine-prefixr-bundle
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],
];
Basic Configuration:
Define prefixes in config/packages/doctrine_prefixr.yaml (Symfony 4+):
doctrine_prefixr:
prefixes:
App: app_
Admin: admin_
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).
Namespace-to-Prefix Mapping:
prefixes key in config to map bundles/namespaces to database prefixes.AcmeBlog: blog_ → All entities in AcmeBlogBundle get blog_ prefix.config/doctrine_prefixr.php (if using a bridge like spatie/laravel-doctrine-orm).Entity Generation:
php artisan doctrine:schema:update --force).table="[prefix]_users").Multi-Environment Prefixes:
config/doctrine_prefixr_dev.yaml):
doctrine_prefixr:
prefixes:
App: dev_app_
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());
}
Laravel Migration Bridge: If using Laravel migrations, ensure the bundle’s schema tool is triggered post-migration or use a custom migration service provider.
Testing: Mock the prefix resolver in tests:
$resolver = $this->createMock(PrefixResolver::class);
$resolver->method('resolve')->willReturn('test_');
$this->app->instance(PrefixResolver::class, $resolver);
Case Sensitivity:
AcmeDemo ≠ acmeDemo). Use consistent casing.Schema Tool Conflicts:
doctrine:schema:update, ensure no manual table="..." annotations in entities override the prefix.AppKernel.php.Archived Package:
Namespace Collisions:
App: app_ and AppAdmin: app_). Use unique prefixes like app_core_.Verify Prefixes: Dump the resolved prefix for an entity:
$entityManager->getMetadataFactory()->getMetadataFor('App\Entity\User')->getTableName();
Schema Dump: Generate a schema dump to inspect table names:
php artisan doctrine:schema:dump --path=schema.sql
Event Debugging:
Log the GenerateSchemaTableEvent to trace prefix application:
$event->getTableName(); // Check if prefix is applied
Custom Prefix Resolver:
Override the default resolver (e.g., DoctrinePrefixr\Resolver\PrefixResolver) to add logic like:
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);
}
}
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_',
],
],
Artisan Commands:
Register the bundle’s commands in AppServiceProvider:
public function boot() {
$this->commands([
DoctrinePrefixr\Command\GenerateSchemaCommand::class,
]);
}
Migration Conflicts: Disable Doctrine’s schema tool during migrations if conflicts arise:
// config/doctrine.php
'schema_tool' => [
'enabled' => env('DOCTRINE_SCHEMA_TOOL_ENABLED', false),
],
How can I help you explore Laravel packages today?