【问题标题】:Typeorm how to get relations of relationsTypeorm如何获取关系的关系
【发布时间】:2019-11-25 11:50:41
【问题描述】:

我正在使用 entitymanager.findOne 方法获取对象 ChatRoomEntity。 ChatRoomEntity 具有变量messages,这是一个 OneToMany - ManyToOne 关系。我没有任何问题可以选择,但是如何获取发送消息的用户。它是 MessageEntity 上的一个变量,具有 OneToMany 关系。

所以基本上我想选择一个房间和它的所有消息。但是所有消息也应该在fromUser 上有它们的值。 我这样选择房间:

this.entityManager.findOne(ChatRoomEntity, {where: {id: roomToJoin.id}, relations: ['activeUsers', 'messages']}).then(roomEntity => {
// some code
}

这里是我的实体:

用户实体


@Entity()
export class UserEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @CreateDateColumn()
  registrationDate: Date;

  @ManyToMany(type => ChatRoomEntity, room => room.activeUsers, {cascade: true})
  @JoinTable()
  activeChatRooms: ChatRoomEntity[];

  @OneToMany(type => ChatRoomMessageEntity, msg => msg.fromUser)
  chatRoomMessages: ChatRoomMessageEntity[];
}

聊天室实体

@Entity()
export class ChatRoomEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column('varchar', {nullable: true})
  title: string;

  @OneToMany(type => ChatRoomMessageEntity, chatrmsg => chatrmsg.chatRoom)
  messages: ChatRoomMessageEntity[];

  @ManyToMany(type => UserEntity, user => user.activeChatRooms)
  activeUsers: UserEntity[];

}

ChatRoomMessageEntity

@Entity()
export class ChatRoomMessageEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column('varchar', {nullable: true})
  message: string;

  @CreateDateColumn()
  creationDate: Date;

  @ManyToOne(type => UserEntity, user => user.chatRoomMessages)
  fromUser: UserEntity;

  @ManyToOne(type => ChatRoomEntity, chatRoom => chatRoom.messages)
  chatRoom: ChatRoomEntity;

}

【问题讨论】:

    标签: mysql typeorm


    【解决方案1】:

    我们可以通过在relations 数组本身中使用'relation.subrelation' 来加载子关系,如下所示:

    relations: ['relation1', 'relation2', 'relation2.subrelation1']

    因此,对于您的情况,您可以简单地执行以下操作,而不是使用 join:

    this.entityManager.findOne(ChatRoomEntity, {
            where: {id: roomToJoin.id},
            relations: ['activeUsers', 'messages', 'messages.fromUser'],
          }).then(roomEntity => {
    ...
    

    此处指定:https://github.com/typeorm/typeorm/blob/master/docs/find-options.md#basic-options

    【讨论】:

    • 非常感谢!这对我帮助很大!
    • 很高兴它帮助了你:)
    • 这正是我想要的!
    【解决方案2】:

    使用查询构建器

    await getRepository(UserEntity)
       .createQueryBuilder('user')
       .leftJoinAndSelect('user.profile', 'profile')
       .leftJoinAndSelect('profile.images', 'images')
       .getMany()
    

    使用 FindOptions

    await getRepository(UserEntity).find({ 
       relations: ['profile', 'profile.images'] 
    })
    

    【讨论】:

      【解决方案3】:

      这里是@gradii/fedaco orm 如何实现此功能。 使用可以通过方法with使用多重关系

      export class User extends Model {
        @HasOneColumn({related: Profile})
        profile;
      }
      export class Profile extends Model {
        @HasOneColumn({related: Image})
        image;
      }
      export class Image extends Model {
        name;
      }
      

      急切的加载

      User.createQuery().with('profile.image').get()
      

      延迟加载

      const user = await User.createQuery().first();
      const image await (await user.profile).image
      

      【讨论】:

        猜你喜欢
        • 2021-10-17
        • 1970-01-01
        • 1970-01-01
        • 2021-02-20
        • 2021-06-10
        • 2020-06-30
        • 2022-01-22
        • 1970-01-01
        • 2020-03-21
        相关资源
        最近更新 更多