2012-03-16 1 views
1

나는 우편 번호의 반경에서 작동하는 상점 찾기를 구성하고 있습니다. 저는이 표준 쿼리와 QoQ를 사용하여 여러 번 했었지만 이제는 cf9 ORM을 사용하여 하나를 만들려고했습니다 ...하지만 마지막 비트로 내 능력의 한계에 도달 한 것처럼 보였습니다.ORM 엔티티 - 레코드를 제거하고 속성을 추가하십시오.

저는 엔티티를 꺼내 처리 중입니다. 마지막에는 다음을 수행해야합니다.

a. 특정 기준을 충족시키지 못하면 레코드를 제거하십시오 (사용자가 지정한 거리보다 멀음)

OR 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

나는 다른 각도에서이 문제를 접근하는 것 (즉, 대략적인 코드의 일부 개조하면 되겠 어해야 할 수도 있습니다)이

for (var local.store in local.stores) 

처럼 반복 :

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

+0

나는이 cflib udf (http://cflib.org/udf/ArrayOfStructsSort)로 정렬 작업을 끝냈다. 많은 도움을 주셔서 감사합니다 !! – Jason

+0

Jason이 결국 효과가 좋았습니다. 어쩌면 단순한 구조체 또는 2D 배열이 더 쉬울 수도 있습니다. Btw, 내 코드에서 몇 가지 unscoped vars 수정했습니다 : geo local.geo로 참조되어야합니다. – CfSimplicity

2

개체를 배열에서 삭제하면 루프가 엉망이됩니다.

뒤쪽 루프 중 하나를 시도해보십시오

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

또는