Doctrine Symfony Migration unable to correctly detect custom Tinyint type length in MySql DB

103 views Asked by At

I have defined a custom doctrine tinyint type for my Symfony 6 application like so:

namespace App\DoctrineTypes;

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;

/**
 * Class TinyIntType
 *
 * @package App\DoctrineTypes
 */
class TinyIntType extends Type
{
    protected string $name = 'tinyint';

    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
    {
        return sprintf('TINYINT(%d)', $column['length']);
    }

    public function convertToPHPValue(mixed $value, AbstractPlatform $platform): int
    {
        return (int)$value;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function requiresSQLCommentHint(AbstractPlatform $platform): bool
    {
        return true;
    }
}

In my doctrine.yaml I got:

doctrine:
    dbal:
        types:
            tinyint: App\DoctrineTypes\TinyIntType

I then make use of the type in an entity like so:

    /**
     * @ORM\Column(
     *     type="tinyint",
     *     name="retrieveflag",
     *     length=4,
     *     options={"default"=0},
     *     nullable=false
     * )
     */
    private ?int $retrieve_flag = 0;

After I ran migrations on this for the first time the DB showed the column correctly like so:

`retrieveflag` tinyint(4) NOT NULL DEFAULT '0' COMMENT '(DC2Type:tinyint)',

However, every time I run

php bin/console doctrine:migrations:diff

I get this in my migration:

    public function up(Schema $schema): void
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->addSql('ALTER TABLE my_table CHANGE retrieveflag retrieveflag TINYINT(4) DEFAULT \'0\' NOT NULL COMMENT \'(DC2Type:tinyint)\'');
    }

    public function down(Schema $schema): void
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->addSql('ALTER TABLE my_table CHANGE retrieveflag retrieveflag TINYINT(0) DEFAULT \'0\' NOT NULL COMMENT \'(DC2Type:tinyint)\'');
    }

So even though the DB contains the retrieveflag field at the correct length (4), the migration seems to think it's 0 and wants to change it.

How do I show doctrine that the DB state matches the entity defined length?

I am using:

  • PHP 8.1
  • MySql 5.7
  • Symfony 6.1.6
  • Doctrine Bundle 2.7.0
  • Doctrine Migrations Bundle 3.2.2
0

There are 0 answers