我正在整理一个商店查找器,它在邮政编码的半径上工作。我已经使用标准查询和QoQ多次完成了这项工作,但现在尝试使用cf9 ORM将其组合在一起......但似乎已经达到了我最后一点的能力极限。
我正在撤出一个实体并对其进行处理。最后,我需要:
一个。如果记录不符合某个条件(距离大于用户指定的距离),则删除记录
或b。向记录添加新属性以存储距离。
所以最后,在我的实体中我想要的只是那些在用户指定范围内的商店,每条记录都包含计算出的距离。
查看我要做的最好方法是查看完整功能
任何建议都非常感谢!!
public function getByPostcodeRadius(required postcode="",
required radius=""){
//set some initial vals
rs = {};
geo = New _com.util.geo().init();
local.postcodeGeo = geo.getGeoCode("#arguments.postcode#, Australia");
local.nearbyStores = "";
local.returnStores = {};
//load stores
local.stores = entityload("stores");
//loop over all stores and return list of stores inside radius
for(i=1; i <= ArrayLen(local.stores); i++){
store = {};
store.id = local.stores[i].getID();
store.geoCode = local.stores[i].getGeoCode();
store.Lat = ListgetAt(store.geoCode,1);
store.Lng = ListgetAt(store.geoCode,2);
distance = geo.getDistanceByGeocode(local.postcodeGeo.Lat,local.postcodeGeo.Lng,store.Lat,store.Lng);
//************************
//HERE IS WHERE I AM STUCK.
if (distance LT arguments.radius){
//here I need to add a property 'distance' and set it's value
local.stores[i].distance = distance; // this adds it to the object, but not with the PROPERTIES
} else {
// here i need to remove the store from the object as it's distance was greater than the one passed in
arrayDeleteAt(local.stores,i); //this clearly isn't working, as positions are changing with each loop over
}
}
return local.stores;
}
答案 0 :(得分:2)
如果你从一个数组中删除一个对象,它会搞乱你的循环。
尝试向后循环:
var i = arrayLen( local.stores );
for ( i; i == 0; i-- )
或像这样循环
for ( var local.store in local.stores )
(这是粗略的代码,可能需要一些调整)
答案 1 :(得分:1)
我会从另一个角度来看待这个问题:
1)我不是从所有商店的数组中删除那些不匹配的数组,而是构建一个包含那些并返回它的数组。
2)如果距离是特定于每个查询而不是商店对象的属性,那么我不会尝试将其添加到商店,而只是将其与我正在为此返回的特定数据“关联”搜索范围。
将2放在一起,我将返回一个包含商店对象及其与所请求邮政编码的距离的结构数组。 (您可以返回商店对象和距离的单个结构,商店ID作为键,但我更喜欢使用数组。)
以下是我编写代码的方法(未经测试,因为我没有您的地理类或实体代码):
public array function getByPostcodeRadius(required postcode="", required radius=""){
hint="I return an array of structs each containing a store object within the requested radius and its distance from the requested post code"
// Geo settings
local.geo = New _com.util.geo().init();
local.postcodeGeo = local.geo.getGeoCode("#arguments.postcode#, Australia");
// initialise the array of structs to return, which will contain stores within the requested radius and their distance from the postcode
local.nearbyStores = [];
//load all stores
local.stores = entityload("stores");
//loop over all stores and add those inside the radius to the return array with their distance
for( var storeObject in local.stores ){
// determine the lat-lng for this store
local.storeLat = ListgetAt(storeObject.getGeoCode(),1);
local.storeLng = ListgetAt(storeObject.getGeoCode(),2);
// get the distance from the requested postcode
local.distance = local.geo.getDistanceByGeocode(local.postcodeGeo.Lat,local.postcodeGeo.Lng,local.storeLat,local.storeLong);
if (local.distance LT arguments.radius){
// create a struct of the store object and its distance and add to the nearby stores array
local.thisStore = {
store = storeObject
,distance = local.distance
};
ArrayAppend( local.nearbyStores,local.thisStore );
}
}
return local.nearbyStores;
}