使用 MySQL,我正在尝试ST_Distance_Sphere
使用QueryBuilder
.
我有一个实体:
import { Entity, PrimaryKey, Property } from "mikro-orm";
@Entity({ tableName: "studio" })
export default class StudioEntity {
@PrimaryKey()
public id!: number;
@Property()
public name!: string;
@Property({ columnType: "point srid 4326" })
public geometry!: object;
}
我正在尝试:
export default class StudioStore {
private studioRepository: EntityRepository<StudioEntity>;
public constructor(ormClient: OrmClient) {
this.studioRepository = ormClient.em.getRepository(StudioEntity);
}
public async findPage(first: number): Promise<StudioEntity[]> {
const query = this.studioRepository.createQueryBuilder().select("*");
query.addSelect(
"ST_Distance_Sphere(`e0`.`geometry`, ST_GeomFromText('POINT(28.612849 77.229883)', 4326)) as distance",
);
query.orderBy({ distance: "ASC" });
return query.limit(first).getResult();
}
}
但我得到一个 ORM 错误:
尝试通过不存在的属性 StudioEntity.distance 进行查询
所以,我尝试向实体添加一个属性:
@Property({ persist: false })
public distance?: number;
但现在我得到一个 MySQL 错误:
“订单子句”中的未知列“e0.distance”
这是生成的 SQL 查询:
[query] select `e0`.*, ST_Distance_Sphere(`e0`.`geometry`, ST_GeomFromText('POINT(28.612849 77.229883)', 4326)) as distance from `studio` as `e0` order by `e0`.`distance` asc limit 5 [took 4 ms]