Coldfusion ORM实体-删除记录并添加属性

Coldfusion ORM实体-删除记录并添加属性,coldfusion,coldfusion-9,Coldfusion,Coldfusion 9,我正在组装一个以邮政编码为半径的商店搜寻器。我已经用标准查询和QOQ做过很多次了,但是现在我试图用cf9 ORM把它们组合起来。。。但最后一点似乎已经达到了我能力的极限 我正在提取一个实体并处理它。最后,我需要: a。如果记录不符合特定条件(距离大于用户指定的距离),则删除该记录 或者b。将新特性添加到记录以存储距离 最后,我希望实体中的所有存储都在用户指定的范围内,每个记录都包含计算出的距离 查看我要做的事情的最好方法是查看完整的函数 非常感谢您的任何建议 public function ge

我正在组装一个以邮政编码为半径的商店搜寻器。我已经用标准查询和QOQ做过很多次了,但是现在我试图用cf9 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;
}
公共函数getByPostcodeRadius(必需的邮政编码=”,
所需半径=”){
//设置一些初始值
rs={};
geo=New_com.util.geo().init();
local.postcodeGeo=geo.getGeoCode(“#arguments.postcode#,Australia”);
local.nearbyStores=“”;
local.returnStores={};
//负载存储
local.stores=实体加载(“存储”);
//遍历所有门店并返回radius内的门店列表

对于(i=1;i如果从数组中删除一个对象,它将打乱循环

尝试反向循环:

var i = arrayLen( local.stores );
for ( i; i == 0; i-- )
还是像这样循环

for ( var local.store in local.stores )

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

我会从另一个角度来处理这个问题:

1) 我不会从所有存储的数组中删除那些不匹配的存储,而是构建一个由匹配的存储组成的数组并返回它

2) 如果距离是特定于每个查询的,而不是存储对象的属性,那么我不会尝试将其添加到存储中,而是将其与我为此搜索返回的特定数据“关联”

把2放在一起,我将返回一个结构数组,其中包含store对象及其与请求的邮政编码之间的距离(您可以只返回store对象和距离的单个结构,其中store ID为键,但我更喜欢使用数组)

以下是我编写代码的方式(未测试,因为我没有您的地理类或实体代码):


CF Simplicty..这很有效,似乎是我想要采取的方法,但我似乎找不到按“距离”对结果排序的方法。我可以使用EntityLoad()返回的对象,但似乎无法确定在这个过程的后面获取数组时如何对结果排序。我使用了ArraySort(),但这不起作用。有什么指针吗?再次感谢CfSimplicity!我最后用这个cflib udf()进行排序…一切都很好。非常感谢你的帮助!!很高兴它最终成功了,Jason。也许一个简单的结构或二维数组可能会更容易。顺便说一句,我已经在代码中更正了几个未定义范围的变量:geo应该被引用为local.geo。
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;
}