phparrayssymfony

Default array value for a migration


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 ?

  1. Can I specify a default value for the migration
  2. Can I put a default value in the field definition, that the migration should use ?
  3. Should I update the database by hand ?

Solution

  • I sort of found an answer, but I am quite sure that there are better ones.

    In the migration file that was created, in function public function up(Schema $schema), after:

        $this->addSql('ALTER TABLE licensee ADD groupe_jours LONGTEXT NOT NULL COMMENT \'(DC2Typ
    

    I added:

        $this->addSql('UPDATE licensee SET groupe_jours = \'a:0:{}\'');
    

    But I am quite sure that there are better solutions, as this one looks like a hack...