I am trying to set a new field to my entity which is an array of booleans. I defined it like this:
/**
* @var bool[] $groupe_jours Selected days for the groups
* @ORM\Column(type="array")
*/
protected $groupe_jours;
I added the initialization in the constructor:
/** @ignore */
public function __construct()
{
$this->groupe_jours = array();
}
Now I want to make the migration
php app/console doctrine:migrations:diff
php app/console doctrine:migrations:migrate
This updates correctly the database:
ALTER TABLE licensee ADD groupe_jours LONGTEXT NOT NULL COMMENT '(DC2Type:array)'
However, when I try to reload my pages I get an error like this:
Could not convert database value "" to Doctrine Type array
This is because the array type requires a specific string when the array is empty, like: 'a:0:{}'
What is the best way to make sure that the migration updates the column correctly ?
If you want to write a Doctrine migration to add a not null array field with a default value, you can use this following syntax :
$table = $schema->getTable('my_table');
$table->addColumn('my_field', 'array', [
'notnull' => true,
'comment' => '(DC2Type:array)',
'default' => serialize([]),
]);