php 学说 2 + 无符号值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7692686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:11:40  来源:igfitidea点击:

Doctrine 2 + unsigned value

phpdoctrine-orm

提问by Krzysztof Trzos

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

是否可以unsigned integer在 Doctrine 2 中指定列类型?

采纳答案by gremo

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

你可以,但你会失去便携性。使用columnDefinition属性并将其设置为integer unsigned. 实际代码取决于您使用的内容。

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”.

columnDefinition:在列名之后开始并指定完整(不可移植!)列定义的 DDL SQL 片段。此属性允许使用高级 RMDBS 功能。但是,您应该谨慎使用此功能及其后果。如果您使用“columnDefinition”,SchemaTool 将不再正确检测列上的更改。

Doctrine reference: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#column

教义参考:https: //www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#column

回答by yvoyer

/**
 * @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)

Yaml 中的定义(主键从 2.4 开始)

id:
  type: integer
  options:
    unsigned: true

回答by Harmon Wood

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

小数将允许如此大的数字并允许您保留 SchemaTool,只需将比例设置为 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

这么大的无符号数会让你的大脑爆炸!含教义 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.

ORM 有一个固有的问题。您如何采用仅某些 RDBMS 支持的数据类型并允许您以任何方式使用它。好吧,当谈到 Doctrine 2 和无符号数字时,他们有点懒惰。

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.

我想要做的就是存储我的 64 位 Facebook ID。那有多难?好吧,我的 RDBMS 是 mySQL,所以我真正需要的是一个未签名的 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.

columnDefinition:在列名之后开始并指定完整(不可移植!)列定义的 DDL SQL 片段。此属性允许使用高级 RMDBS 功能。但是,您应该谨慎使用此功能及其后果。如果您使用“columnDefinition”,SchemaTool 将不再正确检测列上的更改。基本上,此功能可让您将不受支持的内容自由地放入列定义中。制作无符号数字在技术上不受支持!更不用说我的开发和 QA 部署系统严重依赖 SchemaTool。我们可以感谢 Doctrine 和 sqlite3 的懒惰开发人员的组合,为这个疯狂小镇的小金块。

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.

这立即引发了谷歌搜索。如果没有必要,我不喜欢思考。我发现了什么?每个人都在使用 varchars。VARCHARS!?!?我差点心脏病发作。那是不可接受的。

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.

从而进入十进制。这是完美的。存储大小是可变的,它以二进制形式存储,因此索引速度非常快。我们只是将小数精度设置为零,瞧。ORM 可以将其移植到任何 RDBMS,它足够大,我们不必关心不受支持的签名/未签名问题,而且速度快如闪电。decimal(20,0) 应该可以很好地处理我们 18 千万亿 446 千万亿 744 万亿 737.9551615 亿的 Facebook 规模。

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

回答by FlameStorm

Doctrine 1 documentationand Doctrine 2 documentationsaid you can do it by this ways:

Doctrine 1 文档Doctrine 2 文档说你可以通过以下方式做到这一点:

PHP annotations:

PHP注解:

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

Yaml: (see docs)

Yaml:(见文档

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

Hope this helps to someone to save a time ;)

希望这有助于某人节省时间;)

回答by Mohammed Yassine CHABLI

I have the same problem with doctrine 2. and i solve it the same way, but there are some advantages :

我对教义 2 有同样的问题,我以同样的方式解决它,但有一些优点:

/**
 * @ORM\Column(name="id", type="integer", options={"unsigned"=true})
 */

Running diff migration bin/console doctrine:migration:diffwill do some magic.In fact, if the column was a foreign key in other tables, the doctrine is able to find them out and apply the same update row as the concerned column.

运行 diff migration bin/console algorithm :migration:diff会产生一些魔法。事实上,如果该列是其他表中的外键,则该准则能够找到它们并应用与相关列相同的更新行。

You just need in your migration to remove foreign keys before and add them after the column type transformation.

您只需要在迁移之前删除外键并在列类型转换之后添加它们。