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

问题描述 投票:12回答:4

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

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

我怎样才能创建tinyint类型的列,正如我在support data type of doctrine中看到的那样,它不存在。

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
php doctrine-orm zend-framework2
4个回答
16
投票

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

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

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

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


7
投票

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

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

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

...
protected $isActive = true;
...

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


6
投票

在Doctrine 2中没有tinyint类型。原因很简单:

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


1
投票

这里有两种方法,我遇到了所有类似的问题,Doctrine允许您创建任何您认为需要或在其包中不可用的数据类型。第二种方法是使用Small Int,这可能不是最佳解决方案,但我认为它服务于puropose。我见过一些开发人员也使用Int类型,但它仍然可能不是最佳解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.