aristonet/entity-to-model-bundle
Installation:
composer require aristonet/entity-to-model-bundle
Ensure your project uses Symfony 6.2+ and Doctrine ORM.
First Run:
Execute the default command to generate TypeScript models in src/Models/ (default):
php bin/console convert:entitytomodel
Verify generated files in src/Models/ (or your configured output directory).
First Use Case:
Generate a single entity (e.g., Order) with a custom output path:
php bin/console convert:entitytomodel --className=Order --modelDir=src/Types/
Check src/Types/Order.ts for the generated model.
Integrate into Build Process:
Add the command to post-install-cmd in composer.json to auto-generate models on dependency updates:
"scripts": {
"post-install-cmd": [
"@php bin/console convert:entitytomodel --modelDir=src/Types/"
]
}
Partial Updates:
Use --className to regenerate only specific entities (e.g., after schema changes):
php bin/console convert:entitytomodel --className=User,Product
Custom Naming:
Override the default EntityName.ts convention by extending the bundle’s template logic (see Extension Points).
Frontend Sync:
Use the generated models in your frontend (e.g., React/Vue) by copying src/Types/ to your frontend project’s src/types/ or importing via a monorepo setup.
API Contracts:
Pair with Symfony’s ApiPlatform to ensure frontend models match API responses. Example:
// Auto-generated from Doctrine Entity
interface Order {
id: number;
items: OrderItem[];
createdAt: Date;
}
CI/CD: Add the command to your CI pipeline (e.g., GitHub Actions) to validate model consistency:
- run: php bin/console convert:entitytomodel --modelDir=src/Types/
- run: git diff --exit-code src/Types/ # Fail if models changed unexpectedly
Doctrine Metadata Cache: Clear the cache if models aren’t updating:
php bin/console cache:clear
php bin/console doctrine:cache:clear-metadata
Circular References:
The bundle may fail on complex inheritance or circular references (e.g., User ↔ Order). Exclude problematic entities with --excludeClassName.
TypeScript Version:
The generated models use basic TypeScript types. For advanced types (e.g., Pick, Omit), post-process the output with a script or template.
Verbose Output: Enable debug mode to see skipped entities or errors:
php bin/console convert:entitytomodel -vvv
Dry Run: Test the command without writing files:
php bin/console convert:entitytomodel --dry-run
Default Directories:
The bundle defaults to src/Models/ for output. Override in config/packages/aristonet_entity_to_model.yaml:
aristonet_entity_to_model:
model_dir: '%kernel.project_dir%/src/Types/'
Excluded Entities: Skip specific entities globally via config:
aristonet_entity_to_model:
excluded_classes: ['App\Entity\AuditLog', 'App\Entity\Migration']
Custom Templates:
Override the default TypeScript template by creating a custom Twig template in templates/entity_to_model/ and configure the bundle to use it:
aristonet_entity_to_model:
template: 'custom_entity_to_model.ts.twig'
Post-Processing:
Use Symfony’s EventDispatcher to hook into the model generation process. Example:
// src/EventListener/ModelGenerationListener.php
public function onModelGenerated(ModelGeneratedEvent $event) {
$content = $event->getContent();
$content = str_replace('interface', 'type', $content); // Force `type` instead of `interface`
$event->setContent($content);
}
Dynamic Paths:
Generate models in dynamic paths (e.g., per-module) by implementing a custom ModelGenerator service and binding it to the bundle’s aristonet_entity_to_model.model_generator service.
How can I help you explore Laravel packages today?