【发布时间】:2021-11-17 17:13:09
【问题描述】:
请用非常简单的语言和适当的例子进行解释,我已经按照官方文档进行了但我没有理解清楚。
【问题讨论】:
请用非常简单的语言和适当的例子进行解释,我已经按照官方文档进行了但我没有理解清楚。
【问题讨论】:
EntityRepository 是一种从数据库访问实体的便捷方式,因为它处理单个实体,它带有实体名称,因此我们不必在 find()、findAll() 中传递它来电。
例子:
import { EntityRepository } from '@mikro-orm/postgresql';
import { User } from './entities/user.entity';
@Injectable()
export class UserService {
constructor(
@InjectRepository(User)
private readonly userRepository: EntityRepository<User>,
) {}
async getUserByPhone(phone: string) {
return await this.userRepository.findOne({ phone }); //we are not passing any entity here while making a database call.
}
}
而 EntityManager 处理所有实体。这就是为什么在使用 find() 和 findAll() 调用时,我们需要提及我们想要访问的实体。
例子:
return await orm.em.findOne(User, { phone: phoneNumber }); //we have to pass User entity here while making a database call.
【讨论】: