1

我有 2 个实体:采访和评论。Interview 与 Comment 具有一对多的单向关系。

这是我用于评论的 yaml 映射文件:

Entities\Comment:
  type: entity
  table: Comment
  repositoryClass: Repositories\CommentRepository

  fields:
    id:
      type: integer
      id: true
      generator:
        strategy: AUTO
    parentid:
      type: integer
      nullable: false
      column: parentid
    isactive:
      type: integer
      nullable: false
      column: isactive
    isremoved:
      type: integer
      nullable: false
      column: isremoved
    removaldate:
      type: datetime
      nullable: true
      column: removaldate
    user_name:
      type: string
      length: 255
      nullable: false
      column: user_name
    user_email:
      type: string
      length: 255
      nullable: false
      column: user_email
    user_avatar:
      type: string
      length: 255
      nullable: false
      column: user_avatar
    comment:
      type: text
      nullable: false
      column: comment
    creationdate:
      type: datetime
      nullable: false
      column: creationdate
    rating:
      type: integer
      nullable: false

这是我面试的 yaml 映射文件:

Entities\Interview:
  type: entity
  table: Interview
  repositoryClass: Repositories\InterviewRepository

  fields:
    id:
      type: integer
      id: true
      generator:
        strategy: AUTO
    isremoved:
      type: integer
      nullable: false
      column: isremoved
    removaldate:
      type: datetime
      nullable: true
      column: removaldate
    creationdate:
      type: datetime
      nullable: false
      column: creationdate
    rating:
      type: integer
      nullable: false
    anonstitle:
      type: string
      length: 1000
      nullable: false
      column: anonstitle
    anons:
      type: text
      nullable: false
      column: anons
    anonsphoto:
      type: string
      length: 255
      nullable: true
      column: anonsphoto
    interviewtitle:
      type: string
      length: 1000
      nullable: false
      column: interviewtitle
    interview:
      type: text
      nullable: true
      column: interview
    interviewphoto:
      type: string
      length: 255
      nullable: true
      column: interviewphoto
  manyToMany:
    comments:
      targetEntity: Comment
      joinTable:
        name: interviews_comments
        joinColumns:
          interview_id:
            referencedColumnName: id
        inverseJoinColumns:
          comment_id:
            referencedColumnName: id
            unique: true

所以在将模式加载到数据库后,我有 3 个表。其中 2 个是实体表,一个是关系表,它只有 2 列:interview_id、comment_id。但是在为一些采访保留评论对象之后,我在连接表中看不到任何内容。找不到原因。

4

1 回答 1

0

Interview 与 Comment 具有一对多的单向关系。

不,因为您将这种关系定义为manyToManyEntities\Interview而不是在拥有方单向oneToMany

ManyToMany mapping requeres a join table. Because one Interview can have many Comment and one Comment can have many Interview. This problem cannot be sloved with additional attributes in database tables, so the additional mapping table is created.

Solution: If you want to have a one Interview with many Comment, but one Comment have one Interview, you have to correct the mapping in yaml Entities\Interview to oneToMany without joinTable (as you defined and it is created).

于 2013-03-23T16:16:46.050 回答