dsnetpl/doctrine-column-comment-bundle
Installation
composer require dsnetpl/doctrine-column-comment-bundle
If not using Symfony Flex, register the bundle in config/bundles.php:
return [
Dsnetpl\DoctrineColumnCommentBundle::class => ['all' => true],
];
Enable Auto-Loading Ensure your Doctrine entities are autoloaded (default in Symfony).
First Use Case Add a docblock comment to an entity property:
/**
* This field stores the user's full name.
*
* @ORM\Column(type="string", length=255)
*/
private string $fullName;
Generate Migration Run the schema update command to apply comments:
bin/console doctrine:schema:update --dump-sql
Verify the generated SQL includes:
COMMENT ON COLUMN user.full_name IS 'This field stores the user\'s full name.'
Development Workflow
@ORM\Column annotation.--dump-sql to preview changes without applying them.doctrine:schema:update or a migration.Team Collaboration
doctrine:schema:update --complete to reset comments if schema changes break them.Integration with Existing Projects
COMMENT ON COLUMN directly in migrations if needed:
// src/Migrations/VersionYYYYMMDDHHMM.php
public function up(Schema $schema): void
{
$this->addSql('COMMENT ON COLUMN user.email IS \'User\'s verified email address.\'');
}
doctrine:schema:update --force cautiously—back up your database first.public function testSchemaUpdateGeneratesComments()
{
$this->executeCommand('doctrine:schema:update --dump-sql');
$output = $this->getDisplay();
$this->assertStringContainsString('COMMENT ON COLUMN', $output);
}
Schema Locks
doctrine:schema:update in production without --dump-sql may lock tables. Always preview SQL first.--complete to reset comments if the schema drifts.Docblock Parsing Quirks
// ❌ Avoid (uses second line)
/**
* Irrelevant line.
* This is the actual comment.
*/
Case Sensitivity
@ORM\Column(name="...").Doctrine Events
loadClassMetadata. If you override this event, ensure the bundle’s listener runs after your logic.Non-String Columns
json), the comment will still be applied but may not be useful.# PostgreSQL
\d+ user # in psql
# MySQL
SHOW CREATE TABLE user;
# config/packages/doctrine.yaml
doctrine:
dbal:
logging: true
logging_enabled: true
Custom Comment Sources
Override the bundle’s CommentExtractor service to pull comments from other sources (e.g., YAML files):
# config/services.yaml
Dsnetpl\DoctrineColumnCommentBundle\Service\CommentExtractor:
arguments:
$commentSource: '@custom.comment_source'
Conditional Comments
Skip comments for specific entities/columns by implementing CommentFilterInterface:
public function shouldAddComment(FieldMapping $field): bool
{
return !$field->getName() === 'password'; // Skip sensitive fields
}
Database-Specific Logic
Extend the SchemaManager to handle database-specific comment syntax (e.g., MySQL vs. PostgreSQL):
public function getCommentSql(string $table, string $column, string $comment): string
{
return str_starts_with($this->getDatabasePlatform()->getName(), 'postgresql')
? sprintf("COMMENT ON COLUMN %s.%s IS '%s'", $table, $column, $comment)
: sprintf("ALTER TABLE %s CHANGE %s %s COMMENT '%s'",
$table, $column, $column, $comment);
}
bin/console cache:clear
How can I help you explore Laravel packages today?