Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3928

Doctrine Add column onSchemaCreateTable Event

$
0
0

I have some ManyToMany table relations.

I want to add a new column "created_at" automatically.

Following Symfony and Doctrine Documentation, i tired this :

app/config/services_dev.yaml

[...]    App\Event\Listener\ManyToManyListener:        tags:            - { name: doctrine.event_listener, event: onSchemaCreateTable }

app/src/Event/Listener/ManyToManyListener.php

[.....]class ManyToManyListener implements EventSubscriber{    public function getSubscribedEvents()    {        return ['onSchemaCreateTable'];    }    public function onSchemaCreateTable(SchemaCreateTableEventArgs  $event)    {        $columns = $event->getTable()->getColumns();        if (count($columns) <= 2 && !array_key_exists("created_at", $columns)) {            $tableName = $event->getTable()->getName();            $sql = "ALTER TABLE $tableName ADD COLUMN created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP;";            $event->addSql($sql);              //dump($sql);        }    }}

I can dump my SQL code inside, it works.

I also tried this code (inside the if statement)

$event->getTable()->addColumn("created_at","datetime",                ["default" => "CURRENT_TIMESTAMP"]            );

This never execute the SQL statement.For example, while php bin/console doctrine:schema:update --dump-sql, I can't see my query.


Viewing all articles
Browse latest Browse all 3928

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>