我在typeorm中设置了以下querybuilder。
const sql = this.attendanceRepository
.createQueryBuilder("attendance")
.leftJoin("attendance.child", "child")
.select("attendance")
.addSelect("CONCAT_WS(' ', child.firstName, child.middleName, child.lastName)", "childName")
.addSelect("child.class")
.where("attendance.id= :id", { id: id})
.getRawOne()
const Result = await sql
该querybuilder产生以下SQL
SELECT `attendance`.`id` AS `attendance_id`, `attendance`.`child_id` AS `attendance_child_id`,
`attendance`.`attendance_cd` AS `attendance_attendance_cd`, `attendance`.`come_home_time` AS `attendance_come_home_time`, `attendance`.`late_time` AS `attendance_late_time`, `attendance`.`memo` AS `attendance_memo`, `attendance`.`created_at` AS `attendance_created_at`,
`attendance`.`updated_at` AS `attendance_updated_at`, `attendance`.`deleted_at` AS `attendance_deleted_at`, `child`.`class` AS `child_class`,
CONCAT_WS(' ', `child`.`first_name`, `child`.`middle_name`, `child`.`last_name`) AS `childName`
FROM `attendances` `attendance`
LEFT JOIN `children` `child`
ON `child`.`id`=`attendance`.`child_id`
WHERE ( `attendance`.`id`= ? )
AND ( `attendance`.`deleted_at` IS NULL )
它返回了folloiwng数据。
{
"attendance_id": 2,
"attendance_child_id": 4,
"attendance_attendance_cd": 3,
"attendance_come_home_time": "11:50:00",
"attendance_late_time": "23:40:00",
"attendance_memo": "test",
"attendance_created_at": "2020-10-16T13:33:00.053Z",
"attendance_updated_at": "2020-10-19T10:34:55.000Z",
"attendance_deleted_at": null,
"child_class": "S",
"childName": "hikaru hikaru"
}
但是我想要的结果是跟随
{
"id": 2,
"child_id": 4,
"attendance_cd": 3,
"come_home_time": "11:50:00",
"late_time": "23:40:00",
"memo": "test",
"created_at": "2020-10-16T13:33:00.053Z",
"updated_at": "2020-10-19T10:34:55.000Z",
"deleted_at": null,
"child_class": "S",
"childName": "hikaru hikaru"
}
如何删除前缀attendance_
?
如果有人有意见,请告诉我。
谢谢
答案 0 :(得分:0)
我认为您应该使用 getOne
而不是 getRawOne
,它会自动映射。
const sql = this.attendanceRepository
.createQueryBuilder("attendance")
.leftJoin("attendance.child", "child")
.select("attendance")
.addSelect("CONCAT_WS(' ', child.firstName, child.middleName, child.lastName)", "childName")
.addSelect("child.class")
.where("attendance.id= :id", { id: id})
.getOne();
const Result = await sql;
希望能帮到你!