Skip to content
Advertisement

Force doctrine datatype choice, from tinytext to varchar

I’ve describe my entity with short text properties with fixed length. When I use the command make:migration, it generates an SQL code with TINYTEXT datatype. In my case it will be more accurate to use VARCHAR, as my properties have short fixed length.

I’ve search, but I find no satisfying solutions.

I would like to indicate to Doctrine to use VARCHAR for this texts. Is it possible? How?

If I can’t : I’ve see that in the generated php code for migration that it suggests to make myself edit on the SQL generated. Could I change TINYTEXT into VARCHAR here? and will doctrine take it into account for future modifications?

<?php
...
/**
 * @ORMEntity(repositoryClass=UserRepository::class)
 */
class User
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMColumn(type="text", length=64, unique = true)
     */
    private $mail;

    /**
     * @ORMColumn(type="text", length=64)
     */
    private $hash;

    /**
     * @ORMColumn(type="text", length=32)
     */
    private $salt;

    ...
}

MySQL

CREATE TABLE user (
    id INT AUTO_INCREMENT NOT NULL, 
    mail TINYTEXT NOT NULL, 
    hash TINYTEXT NOT NULL, 
    salt TINYTEXT NOT NULL, 
    UNIQUE INDEX UNIQ_8D93D6495126AC48 (mail), PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB

Advertisement

Answer

Doctrine maps type="text", length=64 to mysql’s TINYTEXT.

You are looking for the string type.

/**
 * @ORMColumn(type="string", length=64, unique=true)
 */
private $mail;

This will be mapped to VARCHAR(64)

See https://www.doctrine-project.org/projects/doctrine-dbal/en/2.10/reference/types.html#string for further reference.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement