Array Doctrine to text field (and not longtext)

2019-07-11 07:22发布

Using ORM Doctrine, I noticed that an array Doctrine generate a longtext field in database. Is there a way to generate text field ?

Thanks

/**
 * @var array
 *
 * @ORM\Column(name="my_field", type="array", nullable=true)
 */
private $myField;

2条回答
对你真心纯属浪费
2楼-- · 2019-07-11 07:48

The answer is to define a length for your field, e.g.

/**
 * @var array
 *
 * @ORM\Column(name="my_field", type="array", length=256, nullable=true)
 */
private $myField;    

Setting length to a value between 256-65535 will give you a TEXT field. The break points are defined in the Doctrine documentation:

http://doctrine-orm.readthedocs.org/projects/doctrine-dbal/en/latest/reference/types.html#id107

[TINYTEXT is chosen] if the column length is less or equal to 2 ^ 8 - 1 = 255.

[TEXT is chosen] if the column length is less or equal to 2 ^ 16 - 1 = 65535.

[MEDIUMTEXT is chosen] if the column length is less or equal to 2 ^ 24 - 1 = 16777215.

[LONGTEXT is chosen] if the column length is less or equal to 2 ^ 32 - 1 = 4294967295 or empty.

查看更多
我只想做你的唯一
3楼-- · 2019-07-11 08:05

Just for the record if anyone stumbles on this question : maximum size and storage requirements of a text field are two very different things.

In MySQL (and I guess other SQL engines), variable-length string types like TEXT or LONGTEXT are stored using a length prefix plus data. The length prefix requires from one to four bytes depending on the data type.

So, basically, the difference between TEXT and LONGTEXT is just the maximum length of the string the field can hold. On disk, it will take the same size (except for two or three bytes)

TINYBLOB, TINYTEXT             --> L + 1 bytes, where L < 2^8
BLOB, TEXT                     --> L + 2 bytes, where L < 2^16
MEDIUMBLOB, MEDIUMTEXT         --> L + 3 bytes, where L < 2^24
LONGBLOB, LONGTEXT             --> L + 4 bytes, where L < 2^32

Oh, and beware of the character set. In particular, when using the utf8 Unicode character set, you must keep in mind that not all characters use the same number of bytes and can require up to three bytes per character

Source : https://dev.mysql.com/doc/refman/5.1/en/storage-requirements.html

查看更多
登录 后发表回答