Page 1 of 1

How to return sub elements and populate two lists in one query to two tables

Posted: Tue May 06, 2014 11:09 am
by aggregate

Hello

I have two tables. I know that we cannot perform joins like in relational database. I would like to return the sub elements for each parent record. The database structure is as follows

User-- Parent --Parent -- ID Deck
UserHasIDs-- -- Junction -- Child -- IDs

My goal is to get the child records and display them as a list within the parent list

List 1 Item 1
--Parent record 1
-- Name
-- Junction ID
List 2 Item 1
--Child Record
--Name
--Id
List 1 Item 2
-- Parent record 2
-- Name
-- Junction ID
List 2 Item 1
--Child Record
--Name
--Id
List 2 Item 2
--Child Record
--Name
--Id

I can get the parent and the junction id in the first query.Then in a separate query I can get the Child. (I am using the MongoDB). The issue is that when the result comes back, it updates not just the list item that it is nested in, but the all of the lists in each of the items.

What I am looking for is a way to do a dynamic query for each record returned.

My next step is to push all the elements into local storage and use jquery to write into each listitem. I am hoping there is an easier solution than that.

Thanks for your guidance


How to return sub elements and populate two lists in one query to two tables

Posted: Tue May 06, 2014 12:02 pm
by Evgene Karachevtsev

Hello,

You can use type "pointer" for child id: http://docs.appery.io/documentation/b...
and include related object in your query: http://docs.appery.io/documentation/b...


How to return sub elements and populate two lists in one query to two tables

Posted: Tue May 06, 2014 3:31 pm
by aggregate

I can get the child object for one record. The issue is that I need the child objects for each record returned in the parent query. How can I get that?

Basically for each result returned I use the pointer of the child to find the related object record. I need to take all of the related object records, tie them to their parents, then have them show up in the list.

Based on your tutorials - take the Clickable List tutorial and instead of showing the details on a separate list, I want the details to show up right on the first page, within each item, without having to initiate the second query. Does that make sense.

Example

Tables
Books, Authors, BookHasAuthors (FK Books, FK, Authors)

Title of Book: New Book 1
Authors: Author 1, Author 3

Title of Book: New Book 2
Authors: Author 1, Author 4

Title of Book: New Book 3
Authors: Author 2


How to return sub elements and populate two lists in one query to two tables

Posted: Wed May 07, 2014 12:40 am
by aggregate

Example 2

Tables
Books, Authors, BooksHasAuthors, Categories

Books -- Categories
Authors -- BooksHasAuthors
Books --- BooksHasAuthors

I want to return:

Book, Author, and Category in one list item.

To do this currently I have a list within a list to handle the many-many between Categories and Authors that is created through the Book record.

Hope that helps!


How to return sub elements and populate two lists in one query to two tables

Posted: Wed May 07, 2014 7:05 pm
by Kateryna Grynko

Hi,

Classical RDBMS's way of using relation "many-to-many" isn't good in MongoDB:
http://stackoverflow.com/questions/23...
Maybe the best way is to think about changing database part to more common in MongoDB document-repository solutions.


How to return sub elements and populate two lists in one query to two tables

Posted: Sat May 10, 2014 4:06 am
by aggregate

Hi Katya,

Thanks this was very helpful. I have implemented the Array field as an element on my object and a method to update this array on the parent record. It works when I pass the following syntax through the web service definition section where you can test the request \ and automatically create the response. I can see that the record is updated in the database. However when I attempt to implement by passing the same object (below), I get an error stating that it is "Invalid value for type 'array'".

I just enter this exact value into the field on the rest service I am calling. What is the correct format?

{"__op":"AddUnique","objects":["add header","add footer"]}}


How to return sub elements and populate two lists in one query to two tables

Posted: Mon May 12, 2014 9:37 am
by Kateryna Grynko

Hi,

You use correct format apart from you have unnecessary close braces.