zf2 doctrine2如何在实体列中使用tinyint数据类型

Dev*_*per 12 php doctrine-orm zend-framework2

我正在使用带有ZF2的Doctrine 2 ORM.

/**
 * 
 * @ORM\Column(type="tinyint", options={"default" = 1})
 */
protected $isActive;
Run Code Online (Sandbox Code Playgroud)

如何创建tinyint类型的列,正如我在支持数据类型的学说中看到的那样,它不存在.

Commandline># ./vendor/bin/doctrine-module orm:validate-schema

[Mapping]  FAIL - The entity-class 'Application\Entity\User' mapping is invalid:
* The field 'Application\Entity\User#isActive' uses a non-existant type 'tinyint'.

  [Doctrine\DBAL\DBALException]
  Unknown column type "tinyint" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the known types with \Doctrine\DBAL\Types\Type::getTypeMap(). If this
  error occurs during database introspection then you might have forgot to register all database types for a Doctrine Type. Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseT
  ypes(). If the type name is empty you might have a problem with the cache or forgot some mapping information.


orm:validate-schema
Run Code Online (Sandbox Code Playgroud)

Dev*_*per 16

使用columnDefinition,虽然它不是一个理想的解决方案,但可以达到目的.

/**
 * 
 * @ORM\Column(columnDefinition="TINYINT DEFAULT 1 NOT NULL")
 */
protected $isActive;
Run Code Online (Sandbox Code Playgroud)

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

此外,您应该记住" type "属性仍然处理PHP和数据库值之间的转换.如果在用于表之间连接的列上使用此属性,则还应该查看@JoinColumn.


cpt*_*tnk 7

Imo你应该只使用列类型bool Doctrine然后将它转换为mysql中的tinyint.

/**
 * @var bool
 * @ORM\Column(type="boolean")
 *
 * @Form\Exclude()
 */
protected $isActive;
Run Code Online (Sandbox Code Playgroud)

您还可以定义默认值,如下所示:

...
protected $isActive = true;
...
Run Code Online (Sandbox Code Playgroud)

但是,那么你应该在你的填充中设置.

  • boolean只有两个值(true,false)我需要两个以上,Integer不是最优解.tinyint可以在mysql中用于此目的,smallint是doctrine2中最接近tinyint的选项.或使用columndefination (2认同)

edi*_*igu 6

tinyintDoctrine 2中没有类型.原因很简单:

Doctrine类型定义PHP和SQL类型之间的转换,独立于您使用的数据库供应商.Doctrine附带的所有映射类型都在受支持的数据库系统之间完全可移植.

你应该选择其中一个:

  • integer:将SQL INT映射到PHP整数的类型.
  • smallint:将数据库SMALLINT映射到PHP整数的类型.
  • bigint:将数据库BIGINT映射到PHP字符串的类型.

官方文档:http://docs.doctrine-project.org/en/latest/reference/basic-mapping.html#doctrine-mapping-types

  • 那么,如何使用具有tinyint的现有数据库呢? (6认同)
  • 我认为~99.9%的应用程序从不更改数据库供应商,因此仅仅为此删除功能是无意义的imho (4认同)