我正在使用
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[];
}
聊天室消息实体
@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;
}
我们可以通过在
'relation.subrelation'
数组本身中使用 relations
来加载子关系,如下所示:
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
await getRepository(UserEntity)
.createQueryBuilder('user')
.leftJoinAndSelect('user.profile', 'profile')
.leftJoinAndSelect('profile.images', 'images')
.getMany()
await getRepository(UserEntity).find({
relations: ['profile', 'profile.images']
})
我确实通过将其设置为 true 来获得关系的关系:
return await brandRepo.findAndCount({
order: { updated_at: "desc" },
skip,
take: limit,
relations: {
products: {
sizes: true,
brands: true,
},
},
});
更新v0.4.0
Typeorm 正在抛弃
findOne
,您应该使用 findOneBy
进行没有关系的查询,如果您想要关系,只需使用 find
。
const users = await userRepository.find({
where: { /* conditions */ },
relations: { /* relations */ }
})
// users[0] if you want a first row
除了已接受的解决方案之外,这种语法也是可能的:
关系:{activeUsers:true,消息:{fromUser:true}}
这里是 @gradii/fedaco orm 如何实现此功能。 use 可以通过方法使用多重关系
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
我必须添加
loadEagerRelations: true
标志。所以我在这里添加它以防有人遇到这个问题。
this.entityManager.findOne(ChatRoomEntity, {
where: {id: roomToJoin.id},
relations: ['activeUsers', 'messages', 'messages.fromUser'],
loadEagerRelations: true,
}).then(roomEntity => {
...