I have my mapped entity named Product, with just two columns: id
and name
.
If I add another column manually, i.e. stock_qty
directly with an SQL statement, the schema update tool will remote it, of course.
How can I prevent Doctrine remove my custom columns, not mapped to my entity?
Related issues: https://github.com/doctrine/doctrine2/issues/6434
You can define either a onSchemaColumnDefinition or a onSchemaAlterTableRemoveColumn event and ignore these columns.
Using the onSchemaColumnDefinition Event
In your bundle, create eg. src/AppBundle/EventListener/MyEventListener.php:
namespace AppBundle\EventListener;
use Doctrine\DBAL\Event\SchemaColumnDefinitionEventArgs;
use Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs;
class MyEventListener
{
public function onSchemaAlterTableRemoveColumn(SchemaAlterTableRemoveColumnEventArgs $args)
{
$tableColumn = $args->getColumn();
$table = $args->getTableDiff();
if($table->name == 'tableName' && $tableColumn->getName() == 'columnName') {
$args->preventDefault();
}
}
}
Then, register your event listener in your services:
services:
AppBundle\EventListener\MyEventListener:
tags:
- { name: doctrine.event_listener, event: onSchemaAlterTableRemoveColumn }
Similar, you could do this:
Using the onSchemaAlterTableRemoveColumn Event
In your bundle, create eg. src/AppBundle/EventListener/MyEventListener.php:
namespace AppBundle\EventListener;
use Doctrine\DBAL\Event\SchemaColumnDefinitionEventArgs;
use Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs;
class MyEventListener
{
public function onSchemaColumnDefinition(SchemaColumnDefinitionEventArgs $args)
{
$tableColumn = $args->getTableColumn();
$table = $args->getTable();
if($table == 'tableName' && $tableColumn['Field'] == 'columnName') {
$args->preventDefault();
}
}
}
Then, register your event listener in your services.yml:
services:
AppBundle\EventListener\MyEventListener:
tags:
- { name: doctrine.event_listener, event: onSchemaColumnDefinition }
When running:
php bin/console doctrine:schema:update --force
should ignore the specified column(s). ("tableName"."columnName" in this case will not be deleted)
References:
Doctrine schema events
Doctrine Event Listeners