Doctrine2 column type integer of length 255 doesn't apply to DB

被刻印的时光 ゝ 提交于 2020-01-15 12:36:05

问题


I'm using Doctrine 2 for my DB and I'm currently trying to set a length for one of my column that has an "integer" type but it just doesn't seem to be taken.

/** @Column(type="integer", length=255) */
protected $number;

When I check my database it still says "int(11)" instead of "int(255)". I haven't found anyone experiencing that problem on google or maybe my keywords ain't working so good. I've seen a few example of people using length=XXX and not complaining about it so I thought it would be the way to go but so far no good...

I'm always dropping/rebuilding the whole DB and proxies etc so I doubt it comes from there.


回答1:


It is because you cannot have such a big INT in MySQL.

Try using the type BIGINT instead, it goes from -9223372036854775808 to 9223372036854775807 (signed) or from 0 to 18446744073709551615 (unsigned).

Doctrine version:

/** @Column(type="bigint") */
protected $number; 

Manual




回答2:


Seen this misunderstanding very often. So, to clarify @meze (see the comments above).

The size of any MySQL integer type is about the display width which comes into play with ZEROFILL.

A select on a ZEROFILL column leads to a value at least left padded with 0 up to the size / display width of the column.

A simple example to explain it fully. Look at the returned output.

CREATE TABLE `foo`
(
    `col_1`    INT(1),
    `col_1_Z`  INT(1) ZEROFILL,
    `col_11`   INT(11),
    `col_11_Z` INT(11) ZEROFILL
);

INSERT INTO `foo`
VALUES
    (42, 42, 42, 42);

SELECT
    col_1,
    col_1_Z,
    col_11,
    col_11_Z
FROM
    `foo`;

/* returned output */

|       col_1 |     col_1_Z |      col_11 |    col_11_Z |
|-------------|-------------|-------------|-------------|
|          42 |          42 |          42 | 00000000042 |


来源:https://stackoverflow.com/questions/10597819/doctrine2-column-type-integer-of-length-255-doesnt-apply-to-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!