1

I use MariaDB for a Symfony project and have setup a computed column with:

ALTER TABLE history_event ADD quote_status_change SMALLINT AS (JSON_VALUE(payload, '$.change_set.status[1]'));

When I run Doctrine migrations with bin/console doctrine:schema:update, the computed column is dropped, probably because it doesn't appear anywhere in the HistoryEvent entity class.

How can I prevent Doctrine from dropping computed columns when I run migrations ?

3
  • 1
    I'm afraid you might be out of luck: github.com/doctrine/doctrine2/issues/6434
    – malarzm
    Jun 12, 2018 at 11:05
  • @malarzm If you post this as an answer, I will gladly accept it. It was the right answer. Thanks for your help ! Jun 20, 2018 at 16:33
  • Use migrations for the win 🏅 stop using the schema tool.
    – Mike Doe
    Feb 6, 2019 at 18:52

2 Answers 2

4

I solved this in doctrine 2.10 using the OnSchemaColumnDefinition Event. My code looked something like this:

public function onSchemaColumnDefinition(SchemaColumnDefinitionEventArgs $eventArgs)
{
    if ($eventArgs->getTable() === 'my_table') {
        if (!in_array($eventArgs->getTableColumn()['field'], ['id', 'column_1', 'column_2'])) {
            $eventArgs->preventDefault();
        }
    }
}

In my case I'm using Symfony 4.2 so I set the event listener class up as per.

1
  • this should be the answer, worked for us as well on Symfony 3.4. For the sake of completeness, this is the yaml part: framework.listener.schema: class: SymfonyBundle\EventListener\DoctrineSchemaListener tags: - { name: doctrine.event_listener, event: onSchemaColumnDefinition} Mar 4, 2019 at 9:09
1

I'm afraid you might be out of luck, there is an open feature request but it's not implemented yet: https://github.com/doctrine/doctrine2/issues/6434

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.