在下面的查询中,我想搜索名称和协议编号,但在 OR 模式下,如果名称或协议编号匹配,则查找,但它不能像这样工作
async findAll(filter: DataFilterDto): Promise<ApiResponseDto> {
const take = filter.count || 10;
const pageNo = filter.pageNo > 0 ? filter.pageNo - 1 : 0;
const skip = pageNo * take;
let where: FindOptionsWhere<AgreementEntity> = {};
if (HelperService.isValueValid(filter.id)) {
where["id"] = filter.id;
}
if (HelperService.isValueValid(filter.enquiryId)) {
where["enquiryId"] = filter.enquiryId;
}
if (HelperService.isValueValid(filter.applicantId)) {
where["applicantId"] = filter.applicantId;
}
if (HelperService.isValueValid(filter.searchText)) {
const iLike = ILike(`%${filter.searchText}%`);
// where["name"] = iLike;
// where["agreementNumber"] = iLike;
// Create an OR condition for name and agreementNumber fields
where['_OR'] = [
{ 'name': iLike },
{ 'agreementNumber': iLike }
];
}
const query = {
where: where,
relations: ["enquiryInfo", "enquiryInfo.applicantInfo"],
order: { id: SortOrder.Desc },
take,
skip,
};
let [result, total] = await this.agreementsRepo.findAndCount(query);
return { data: result, count: total };
}
尝试修改创建 OR 条件的部分,如下所示:
// ...
if (HelperService.isValueValid(filter.searchText)) {
const iLike = ILike(`%${filter.searchText}%`);
// Create an OR condition for name and agreementNumber fields
where['OR'] = [
{ 'name': iLike },
{ 'agreementNumber': iLike }
];
}
// ...
确保将“_OR”替换为“OR”。在 TypeORM 中定义 OR 条件的正确键是“OR”。