Start a new topic

Collection and Relations



Maybe some one can help me to get to the right approach.



Question:

Is it possible to have an relation inside a collection , which doesn't point to the _id property of the related collection (One to Many / Deal has Many Products) ?



Example (Currently Not Working):

- IndoorProducts:{ _type: 'KinveyRef', _collection : 'IndoorProducts', IndoorDeals_id: records[i].get('IndoorDeals_id') }.



What works (OneToOne):

- IndoorProducts:{ _type: 'KinveyRef', _collection : 'IndoorProducts', _id: records[i].get('IndoorDeals_id') }



What would be the right approach ...not using the relation functionality ...



Thanks for your help,

Markus


The best approach would be to set the kinveyRef on the child record, pointing to the parent. You still have a one-to-many relationship, the kinveyRef is simply on the other side. However you didn't clarify if each child only has one parent, this method would only work if so.



I cannot be sure (I know it works on the User collection though), but I think you can do an array of kinveyRef objects as well on the parent record for a one-to-many relationship.



What JS framework are you using (obviously besides Kinvey)? It looks like it could be Sencha, if so let me know and we can share out a method we wrote that acts as a kinveyRef/relations manager.
Answering your questions:

-Yes each child only has one parent

- Yes Im using Sencha 2.3 (Currently writing a custom proxy for Kinvey)



Would be great if you could share the relations manager ;)



The `_id` field is required. If you want to do one-to-many relations, you can do what OhmzTech suggested. You can also embed multiple KinveyRefs in an array:



```

[

{ type: 'KinveyRef', _collection: 'IndoorProducts', _id: 'indoorDealsId1' },

{ type: 'KinveyRef', _collection: 'IndoorProducts', _id: 'indoorDealsId2' },

{ type: 'KinveyRef', _collection: 'IndoorProducts', _id: 'indoorDealsId3' }

]

```
Then I would just recommend going with method #1, setting a parent/owner column on the children and giving each a kinveyRef back to the parent record.



Go ahead and contact us through our website (www.ohmztech.com) regarding the proxy/relations code.
I put the KinveyRef on the child collection, but how can I do the following query: Show me all products from one deal, with the products as a array of the deal.
Have you taken a look at this yet?

http://devcenter.kinvey.com/html5/guides/datastore#relationaldataretrieve



Basically, you want to specify a relations object in the options on the Kinvey Data Store get. This relations object should be key/value pairs, where key is the your column name (containing the kinveyRef) and value is the name of the actual collection. Then the returned response will contain your nested references with fields resolved.
ANSWERING YOUR QUESTIONS YES I DID ;)



Since my Kinveyref is on the child object (Products) how would a query look like ..



What I want is : Getting all Products for One Deal



var query = new Kinvey.Query();

query.equalTo('deal._id', 'deal_id');

var promise = Kinvey.DataStore.find('events', query, {

relations : { deal: 'products' },

success : function(response) {

...

}

});



Does that work even the KinveyRef is on the product collection ?



Thanks

That looks absolutely correct. My apologies for my last message here (now deleted) as it seems you are indeed using the correct underscore, but the post formatting is changing it.



In this situation though, it doesn't really make sense to resolve the relations. You would be getting the same deal data for every single product, essentially wasting resources and slowing the query. Since you have to know the _id of deal already, I'm guessing you probably already have that record. If not, in this case, it's probably best to just fetch it as a single separate request.
[

{ _type: 'KinveyRef', _collection: 'IndoorProducts', _id: 'indoorDealsId1' },

{ _type: 'KinveyRef', _collection: 'IndoorProducts', _id: 'indoorDealsId2' },

{ _type: 'KinveyRef', _collection: 'IndoorProducts', _id: 'indoorDealsId3' }

]
Login or Signup to post a comment