简体   繁体   English

Symfony原则-ManyToOne关系中的ID长度

[英]Symfony Doctrine - Id length in ManyToOne relationship

When I do a 'doctrine:migrations:diff', Symfony does not 'export' the field length, puts '255' instead and 'doctrine:migrations:migrate' does not work. 当我执行“ doctrine:migrations:diff”时,Symfony不会“导出”字段长度,而是放置“ 255”,而“ doctrine:migrations:migrate”不起作用。

My two entities ORM definitions: 我的两个实体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

I've tried to set the 'length' property inside the manyToOne definition with no success. 我试图在manyToOne定义中设置'length'属性,但没有成功。

(I use string type in Ids because I generate UUIDs) (我在Ids中使用字符串类型,因为会生成UUID)

IMO doctrine annotations are more convenient way to map your entities, but anyway I think you do something wrong with types of your fields. IMO学说注释是映射实体的更方便的方法,但是无论如何,我认为您对字段类型做错了。 The type attribute defines the mapping type to use for the column in database, so it should be string , integer , json_array etc. type属性定义用于数据库中列的映射类型,因此它应该是stringintegerjson_array 等。

According to Doctrine docs usually id field is defined separately from other fields like this: 根据Doctrine docs,通常将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

As it said in docs: "It has a generator tag nested inside, which specifies that the primary key generation mechanism should automatically use the database platform's native id generation strategy". 正如在文档中所说的:“它内部嵌套了一个generator标签,它指定主键生成机制应自动使用数据库平台的本机ID生成策略”。

But you can find other strategies here . 但是您可以在这里找到其他策略。

So you can try this: 因此,您可以尝试以下操作:

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

In my case I use annotations and it works fine: 就我而言,我使用注释,并且可以正常工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM