Doctrine 2 + unsigned value

2019-03-09 06:01发布

Is it possible to specify a column type of unsigned integer in Doctrine 2?

4条回答
手持菜刀,她持情操
2楼-- · 2019-03-09 06:33

Doctrine 1 documentation and Doctrine 2 documentation said you can do it by this ways:

PHP annotations:

/**
 * @Column(type="integer", name="some_field" options={"unsigned":true})
 */
protected $someField;

Yaml: (see docs)

MyEntity:
  fields:
    someField:
      type: integer
      column: some_field
      options:
        unsigned: true

Hope this helps to someone to save a time ;)

查看更多
Bombasti
3楼-- · 2019-03-09 06:40

Decimals will allow for such large numbers and allow you to keep the SchemaTool, just set the scale to 0.

<?php
/**
 * @Column(type="decimal", precision=20, scale=0, nullable=false, unique=true)
 */
Protected $facebookId;

Checkout a full writeup on why here. [EDIT](link does not work) I have pasted the article below. It was written by me anyways ;)

unsigned numbers so large your brain will explode! w/Doctrine 2

ORMs have an inherent problem. How do you take a datatype only some RDBMSs support and allow you to use it anyways. Well when it comes to Doctrine 2 and unsigned numbers they got a little lazy.

All I want to do is store my 64bit facebook IDs. How hard is that? Well my RDBMS is mySQL so all I really need is an unsigned bigint.

<?php
/**
 * @Column(type="bigint", nullable=false, unique=true, columnDefinition="unsigned")
 */
Protected $facebookId;

This seems find and dandy until you read this:

columnDefinition: DDL SQL snippet that starts after the column name and specifies the complete (non-portable!) column definition. This attribute allows to make use of advanced RMDBS features. However you should make careful use of this feature and the consequences. SchemaTool will not detect changes on the column correctly anymore if you use “columnDefinition”. Basically this feature let’s you free form unsupported things into the column definition. Making unsigned numbers technically UN-SUPPORTED! Not to mention my dev and QA deployment systems rely heavily on the SchemaTool. We can thank a combination of lazy developers at Doctrine and sqlite3 for this little nugget of crazy town.

This immediately prompted a google search. I don’t like thinking if I don’t have to. What did I find? Everybody’s using varchars. VARCHARS!?!? I about had a heart attack. That was just unacceptable.

Thus enters decimal. It’s perfect. The storage size is variable and it’s stored in binary so indexing is super fast. We just set the decimal precision to zero and voilà. The ORM can port this to any RDBMS, it’s big enough for us not to care about the unsupported signed/unsigned issue and it’s lightning fast. decimal(20,0) should handle our facebook size of eighteen quintillion four hundred and forty six quadrillion seven hundred and forty four trillion seventy three billion seven hundred and nine million five hundred and fifty one thousand six hundred and fifteen quite nicely.

<?php
/**
 * @Column(type="decimal", precision=20, scale=0, nullable=false, unique=true)
 */
Protected $facebookId;
查看更多
手持菜刀,她持情操
4楼-- · 2019-03-09 06:41

You can but you will loose portability. Use columnDefinition attribute and set it to integer unsigned. The actual code depends on what you are using.

columnDefinition: DDL SQL snippet that starts after the column name and specifies the complete (non-portable!) column definition. This attribute allows to make use of advanced RMDBS features. However you should make careful use of this feature and the consequences. SchemaTool will not detect changes on the column correctly anymore if you use “columnDefinition”.

查看更多
乱世女痞
5楼-- · 2019-03-09 06:47
/**
 * @ORM\Column(name="id", type="integer", options={"unsigned"=true})
 */

There is no place in the doc (that I've seen) that speak about this, but it works.

Update

Definition in Yaml (as of 2.4 for primary key)

id:
  type: integer
  options:
    unsigned: true
查看更多
登录 后发表回答