Select to view content in your preferred language

querying related tables

4148
17
Jump to solution
01-31-2012 11:14 AM
eddiequinlan
Deactivated User
Hopefully someone has a simple answer which eludes me.

I've read the documentation on feature layers, the queryRelatedFeatures, and various coding examples.  The concept/coding doesn't appear to be very difficult, however, I'm doing something wrong.

I'm trying to query a feature layer (parcel layer) and from there query the related table (sales table).   The user draws a graphic on the map, and from there the results are returned.  The feature layer is querying correctly.  It then queries the related table and this is where the problem begins.  The query relate does not fail; it's only returning the records (sales) from the first parcel number in the parcel layer (queried layer).  I've tried all kinds of coding variations and can't get past this point.  HELP!

sincerely,
Eddie

Here is the code.

[PHP]
private function SearchLayer(event:DrawEvent):void
{
   
sQuery = new Query();  
sQuery.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;
//sQuery.where = exp_use;  //search by use code
sQuery.geometry = event.graphic.geometry;  //search with geometry
   
my_layer.addEventListener(FeatureLayerEvent.SELECTION_COMPLETE, onComplete_FeatureLayerSelection);
//my_layer.useAMF = true;
my_layer.selectionColor = 0x08f36b;
my_layer.outFields = ["*"];

my_layer.selectFeatures(sQuery, FeatureLayer.SELECTION_NEW);  // search feature layer.......
}
  
private function onComplete_FeatureLayerSelection(e:FeatureLayerEvent):void
{
relatedQuery.objectIds = [e.features[0].attributes.FID];  //FID number in featurelayer
if (e.features.length > 0)
{
  my_layer.queryRelatedFeatures(relatedQuery, new AsyncResponder(onResult, onFault));
    
  function onResult(relatedRecords:Object, token:Object = null):void
  {
   var fset:FeatureSet = (relatedRecords[e.features[0].attributes.FID]);
   var objIds:Array = new Array();

   for each (var g:com.esri.ags.Graphic in fset.features)
   {
    objIds.push(g.attributes.SAPRICE);
    trace(g.attributes.SAPRICE);      
   }
     
   trace(objIds);
  }

  function onFault(info:Object, token:Object = null):void
  {
   trace("on fault issue");
  }
}
else
{
  Alert.show("No Records Found");
}
}
[/PHP]
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus
Eddie,

   Glad you got it working. Don't forget to click the Mark as answer check and to click the top arrow (promote) as shown below:

View solution in original post

0 Kudos
17 Replies
RobertScheitlin__GISP
MVP Emeritus
Eddie,

   The code is doing what you are telling it to do, this line of code tells it to query the related records for only the first returned graphic:

relatedQuery.objectIds = [e.features[0].attributes.FID];  //FID number in featurelayer


So you need to do a for loop on the  e.features

Don't forget to click the Mark as answer check and to click the top arrow (promote) as shown below:
0 Kudos
eddiequinlan
Deactivated User
Hi Robert,

As always thanx for your help.  I thought the same thing, so I tried to create a loop but wasn't getting the results I expected.  Obviously I coded the loop wrong.  Also, I was under the impression that maybe a loop wasn't needed; that the whole point of the "queryrelatedfeatures" was to use the array of selected features to then "reselect" on the related table????

I actually have written code to do just that and not use "queryrelatedfeatures".  But after viewing some code I thought it would be cleaner and easier.  Do you have an example of how you would loop thru the FID?

eddie
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Eddie,

   You have to loop through to get an array of ObjectIds

                var oidArr:Array = [];
                for (var rf:int=0; rf < e.features.length; rf++){
                    oidArr.push(e.features[rf].attributes.FID);
                }
                relatedQuery.objectIds = oidArr;
0 Kudos
eddiequinlan
Deactivated User
Hi Robert,

I had previously tried looping as you suggested, but I would only get one relate returned from the sale table (related table). I tried your loop and get the same results.  I can see that both methods create the correct array of ID numbers.  Here is my method:

var oIDs:Array = new Array();
    
for each(var g:com.esri.ags.Graphic in e.features)
{
        oIDs.push(g.attributes.FID);
}
relatedQuery.objectIds = oIDs;
trace(oIDs);  // shows array of numbers

Thanx,
Eddie
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Eddie,

   So Are you saying that each OID should bave multiple (one to many) results?
0 Kudos
eddiequinlan
Deactivated User
Yes.  Each Parcel Record returned from the query search on the FeatureLayer will have multiple records per Parcel Record returned from the Sale table.  It's kind of frustrating, because I know this concept isn't new.   I'm just missing something.

Eddie
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Eddie,

   Does the layer that you are querying for relates only have one relate defined?
0 Kudos
eddiequinlan
Deactivated User
Yes, one relate built in the .mxd.
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Eddie,

   So after doing some testing trying to use your original code and filling in the blanks. I was able to get multiple relate results back from one feature with out having the loop.

   Here is the app.
0 Kudos