<?php
use Doctrine\ORM\Tools\Event\GenerateSchemaTableEventArgs;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\Tools\ToolEvents;
use PHPSnippets\Db\Entity\Test;
use Doctrine\SqlFormatter\SqlFormatter;
$listener = new class() {
public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $event) : void
{
$table = $event->getClassTable();
if ($table->getName() === 'test') {
$table->addColumn('column_of_event', 'boolean');
}
}
};
$entityManager->getEventManager()
->addEventListener(ToolEvents::postGenerateSchemaTable, $listener);
$tool = new SchemaTool($entityManager);
$testEntityMetadata = $entityManager->getClassMetadata(Test::CLASS);
dump(
'Column names of entity class Test:',
$testEntityMetadata->columnNames
);
$queries = $tool->getCreateSchemaSql([$testEntityMetadata]);
echo (new SqlFormatter())->format($queries[0]);
^ "Column names of entity class Test:"
^ array:3 [ "id" => "id" "name" => "name" "creationDate" => "creation_date" ]
CREATE TABLE test ( id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, creation_date DATETIME DEFAULT NULL, column_of_event TINYINT(1) NOT NULL, PRIMARY KEY(id) ) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB