Page 1 of 1

Execute a second DB query for each entry returned by a first query

Posted: Sun May 05, 2013 8:33 pm
by Lars Eirik

I have a service which, upon accessing a given page, queries a collection in my database based on certain conditions, and the service returns a list of database entries (rows) as expected. One of the fields (columns) it returns is a pointer to an entry in another collection within my database. Now, for each of the entries returned by the first query, I would like to access this second collection and retrieve a field from the entry corresponding to the pointer. Finally, I would like to populate a mobile grid on the page with the entries returned by the first query and the corresponding results from the second query. How can I achieve this?


Execute a second DB query for each entry returned by a first query

Posted: Mon May 06, 2013 12:24 am
by maxkatz

If the initial request doesn't contain the data you need, you would need to make a second request.


Execute a second DB query for each entry returned by a first query

Posted: Mon May 06, 2013 7:40 am
by Lars Eirik

Yes, I figured that much. What I have tried so far is this:

  1. I have made a REST service which performs the first query. This returns a number of entries with multiple fields each, one of which is called 'Name_of_A', containing the name of the entry, and one which is called 'Ref_to_B', containing the pointer to the second DB collection.

  2. Within the Response parameter mapping of this first REST service I have mapped the 'Name_of_A' field to a field within a mobile grid on the page, and the 'Ref_to_B' field to a local variable.

  3. I have then made a second REST service where I have mapped the local variable 'Ref_to_B' to the 'where' field in the Request part of the query. In the response part, I have mapped the returned value 'Name_of_B' to a second field in the mobile grid on the page.

    The result of this is that the first column of the mobile grid is populated correctly with the 'Name_of_A' of each entry, but in the second column it shows the same value for 'Name_of_B' for all entries. They should have been different. It seems like the second REST service only receives a single value for 'Ref_to_B' and thus only retrieves a single value for 'Name_of_B'. So my question is: how can I make the second REST service iterate through all the entries returned by the first REST service?


Execute a second DB query for each entry returned by a first query

Posted: Tue May 07, 2013 9:13 pm
by Alena Prykhodko

Hello!

Sorry for the delay. We need some additional time to figure it out. We'll update.


Execute a second DB query for each entry returned by a first query

Posted: Wed May 08, 2013 12:11 pm
by Oleg Danchenkov

Hi Lars.
You should save all Ref_to_B to array and then on Success event of first service iterate through all saved values and invoke service for each value.
And one more question. How do you show results of second service? You cann't use mapping. You should use JS to show all results.


Execute a second DB query for each entry returned by a first query

Posted: Sun May 12, 2013 9:07 pm
by Lars Eirik

Thanks. That sounds good.

Now I have mapped the result of the first service to a local variable called 'result' and added a JS which iterates through the items in 'result' and fetches the rest of the data from the second collection. This works great. The problem now is, as you point out, to show the results.

As mentioned, I already have a mobile grid on the page with one row and two columns, each containing a label. So the question now is:

  1. how do I add rows to the grid and

  2. how do I reference the labels in the new rows?

    I have tried to look for examples that show this, but without luck. Do you know of such an example? If not, can you give me a few pointers as to how this is done?


Execute a second DB query for each entry returned by a first query

Posted: Thu Aug 08, 2013 8:26 pm
by Lars Eirik

After some failed attempts and no response here, I gave this up. But now I thought I would give it another go. Anyone who can help me?


Execute a second DB query for each entry returned by a first query

Posted: Thu Aug 08, 2013 9:22 pm
by maxkatz

To add data to already existing grid on page, you would need to do it programatically. Select the element in the DOM and add nodes to it.