aitradeinc/symfony-entity-generator
Installation
composer require aitradeinc/symfony-entity-generator
Ensure your project uses Symfony 5.0+ (or 4.0 for legacy support).
Basic Command Generate entities from an existing database table:
php bin/console make:entity --db-connection=default --table-name=users
default with your Doctrine connection name.users with your target table.Configuration
Create a YAML config file (e.g., config/entity_generator.yaml) to customize generation:
aitradeinc_entity_generator:
groups: ['User', 'Admin'] # Optional: Group fields by entity
namespace: 'App\Entity' # Target namespace
First Use Case
Generate a User entity with all columns from the users table:
php bin/console make:entity --db-connection=default --table-name=users --entity-name=User
Outputs a fully typed User.php in src/Entity/ with:
id, email, isActive).getEmail(), setIsActive(bool)).@ORM\Column(type="boolean")).Database-First Development
php bin/console make:entity --db-connection=mysql --table-name=products --entity-name=Product
created_at column).Grouping Fields
Use YAML config to organize fields into logical groups (e.g., User vs. Admin):
aitradeinc_entity_generator:
groups:
User: [id, email, password]
Admin: [role, permissions]
--group=User to generate only User-related fields.Custom Templates
Override Twig templates (located in vendor/aitradeinc/symfony-entity-generator/Resources/views/) to modify output (e.g., add custom annotations):
{# src/Resources/views/Entity/fields.twig #}
{% for field in fields %}
/**
* @Assert\NotBlank(groups={{ group }})
*/
private {{ field.type }} ${{ field.name }};
{% endfor %}
Integration with Migrations
--force to overwrite existing files:
php bin/console make:entity --table-name=orders --force
Partial Generation Generate only specific columns (e.g., for a new feature):
php bin/console make:entity --table-name=posts --columns=title,content,author_id
doctrine/orm in Laravel via laravel-doctrine to leverage this package.app/Console/Kernel.php:
protected $commands = [
\Aitradeinc\EntityGenerator\Command\MakeEntityCommand::class,
];
Boolean Field Naming
get (e.g., getIsActive()), not is (e.g., isActive()).getter.twig).JSON Column Handling
json Doctrine types. Ensure your composer.json includes:
"doctrine/dbal": "^3.0"
private ?array $metadata = null;
public function getMetadata(): ?array { return json_decode($this->metadata, true); }
Namespace Conflicts
App\Entity\User) by default.aitradeinc_entity_generator:
namespace: 'App\\Entity'
YAML Parsing Errors
groups: [User, Admin]).--debug to see parsed config:
php bin/console debug:config aitradeinc_entity_generator
File Overwrite Behavior
--force to overwrite:
php bin/console make:entity --table-name=users --force
PHP 8 Typed Properties
private ?string $email).? from nullable fields manually.--dry-run to preview changes without writing files:
php bin/console make:entity --table-name=users --dry-run
php bin/console make:entity --table-name=users --env=dev
order, group) in column names.Custom Field Mappings
Override the FieldTypeMapper service to map database columns to custom PHP types:
# config/services.yaml
Aitradeinc\EntityGenerator\FieldTypeMapper:
arguments:
$customMappings:
enum: 'App\Enum\StatusEnum'
Post-Generation Hooks Use Symfony’s event system to modify entities after generation. Example:
// src/EventSubscriber/EntityGeneratorSubscriber.php
namespace App\EventSubscriber;
use Aitradeinc\EntityGenerator\Event\EntityGeneratedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EntityGeneratorSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
EntityGeneratedEvent::NAME => 'onEntityGenerated',
];
}
public function onEntityGenerated(EntityGeneratedEvent $event)
{
$entity = $event->getEntity();
$entity->addMethod('toArray()'); // Inject custom method
}
}
Template Inheritance
Extend Twig templates by copying them to templates/AitradeincEntityGenerator/ and modifying:
fields.twig: Add custom annotations.getter.twig: Change boolean getter naming.Database Connection Flexibility
aitradeinc_entity_generator:
db_connection: '%env(DATABASE_URL)%'
DATABASE_URL in .env:
DATABASE_URL="mysql://user:[email protected]/db_name"
How can I help you explore Laravel packages today?