duanbing6955
2013-09-11 16:18
浏览 103
已采纳

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

I am using Doctrine 2 ORM with ZF2.

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

How can I create tinyint type of column, as I can see in support data type of doctrine, it does not exist.

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
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongtong848825 2013-09-11 16:27
    已采纳

    Use columnDefinition, though it is not an ideal solution but serve the purpose.

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

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

    Additionally you should remember that the “type” attribute still handles the conversion between PHP and Database values. If you use this attribute on a column that is used for joins between tables you should also take a look at @JoinColumn.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongpu1331 2013-09-11 16:35

    There are 2 approaches here, i faced all most similar problem, Doctrine allows you to create any data type that you think you need or that is not avaialable in its packages. 2nd Approach is to use Small Int which may not be optimal solution but i think it servers the puropose. I have seen some developers use Int type as well but still it may not be optimal solution.

    评论
    解决 无用
    打赏 举报
  • doukang1962 2013-09-12 07:36

    Imo you should just use the column type bool Doctrine then will convert this into tinyint in mysql for you.

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

    You could also define a default value like so:

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

    But rather then doing that you should set in in your populate.

    评论
    解决 无用
    打赏 举报
  • dpdx51205 2013-09-13 11:20

    There is no tinyint type in Doctrine 2. Reason is straightforward:

    A Doctrine type defines the conversion between PHP and SQL types, independent from the database vendor you are using. All Mapping Types that ship with Doctrine are fully portable between the supported database systems.

    You should pick one of these:

    • integer: Type that maps a SQL INT to a PHP integer.
    • smallint: Type that maps a database SMALLINT to a PHP integer.
    • bigint: Type that maps a database BIGINT to a PHP string.

    Official docs here: http://docs.doctrine-project.org/en/latest/reference/basic-mapping.html#doctrine-mapping-types

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题