2012-03-16 48 views
1

我正在放置一个从邮政编码在半径上工作的商店查找程序。我已经使用标准查询和QoQ完成了很多次,但现在试图使用cf9 ORM将它们放在一起......但似乎已经达到了我最后一点的能力极限。ORM实体 - 删除记录,并添加一个属性

我拉出一个实体并处理它。最后,我需要:

a。如果记录不符合特定标准(距离大于用户指定的范围),请删除记录

或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; 
} 

回答

1

我会从不同的角度来处理这个:

1)不是从所有商店的数组中删除那些不是m atch,我会建立一个数组,并返回它。

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; 
} 
+0

CF Simplicty ..这工作,好像我要采取的办法,但我似乎无法找到一种方法,命令由“距离”的结果。我可以使用由EntityLoad()返回的对象,但似乎无法解决当我将数组放到此过程的后面时如何排序结果。我玩过ArraySort(),但这并不能解决问题。任何指针?再次感谢CfSimplicity! – Jason 2012-03-18 13:06:49

+0

我最终用这个cflib udf(http://cflib.org/udf/ArrayOfStructsSort)排序..所有的作品都是一种享受。非常感谢您的帮助!! – Jason 2012-03-18 13:17:54

+0

杰森最后非常高兴。毕竟,一个简单的结构或二维数组可能会更容易。顺便说一句,我已经纠正了我的代码中的几个无与伦比的变量:地理应该被引用为local.geo。 – CfSimplicity 2012-03-18 16:32:48

2

如果你从一个数组中删除一个对象,它会弄乱你的循环。

尝试要么向后循环:

var i = arrayLen(local.stores); 
for (i; i == 0; i--) 

或钩织这样

for (var local.store in local.stores) 

(这是粗略的代码,并可能需要一些调整)