【问题标题】:Symfony Doctrine - Id length in ManyToOne relationshipSymfony Doctrine - ManyToOne 关系中的 ID 长度
【发布时间】:2018-04-23 10:18:11
【问题描述】:

当我执行 'doctrine:migrations:diff' 时,Symfony 不会“导出”字段长度,而是输入 '255' 并且 'doctrine:migrations:migrate' 不起作用。

我的两个实体 ORM 定义:

App\Resources\Domain\Stage:
type: entity
fields:
    id:
        type: stage_id
        id: true
        length: 36
    name:
        type: string
manyToOne:
    educationalAreaId:
        targetEntity: EducationalArea
        joinColumn:
            name: educationalAreaId
            referencedColumnName: id

App\Resources\Domain\EducationalArea:
type: entity
fields:
    id:
        type: educational_area_id
        id: true
        length: 36
    name:
        type: string

我尝试在 manyToOne 定义中设置“长度”属性,但没有成功。

(我在 Ids 中使用字符串类型,因为我生成 UUID)

【问题讨论】:

  • 我不熟悉实体的 yaml 定义,但看起来 types 的 id 是错误的。应该是string。您的types 值看起来像列名,因此您应该为此使用column: stage_id。要生成 UUID,您可以使用 generator: { strategy: UUID }
  • @PavelAlazankin 这些类型是其他实体。另一方面,我在提供的链接中没有找到任何解决方案。还是谢谢。

标签: php symfony doctrine-orm


【解决方案1】:

IMO 原则注释是映射实体的更方便的方法,但无论如何我认为你的字段类型有问题。 type 属性定义了用于数据库中列的映射类型,所以它应该是stringintegerjson_array etc.

根据 Doctrine 文档,通常 id 字段与其他字段分开定义,如下所示:

App\Resources\Domain\Stage:
  type: entity
  table: products
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    name:
      type: string
manyToOne:
    educationalAreaId:
        targetEntity: EducationalArea
        joinColumn:
            name: educationalAreaId
            referencedColumnName: id

正如它在文档中所说:“它有一个嵌套在里面的生成器标签,它指定主键生成机制应该自动使用数据库平台的原生 id 生成策略”。

但你可以找到其他策略here

所以你可以试试这个:

App\Resources\Domain\Stage:
  type: entity
  table: products
  id:
    id:
      type: string
      length: 36
      generator:
        strategy: UUID
...

在我的情况下,我使用注释并且效果很好:

/**
 * @var string
 * @ORM\Column(name="id", type="string", length=36, options={"fixed":true})
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="UUID")
 */
private $id;

【讨论】:

    猜你喜欢
    • 2014-09-27
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多