Datamapper and REST, outputting JOIN relationships in one go |
[eluser]gh0st[/eluser]
I have been following the Net tuts tutorial for the REST controller created by Phil Sturegon and I have got it working with my models using the Datamapper ORM. I have a company which has a many-to-many relationship with workers through a bridging/junction table called contracts. In a very basic test, I created a simple /ap/company/id/1 to grab the details of 1 company and this displays successfully as JSON without any problems. However, I am unsure of how to get Datamapper + REST to output the entire tree in one go. For example, I would like to output the company and all her employees with their respective contract details in one go. IE: Output the full join in a tree-like JSON format via the API showing all company and all the employees contracted to that company. The idea would be that I can then feed this information into my iPhone app at the other end and populate Core Data with it in one fell swoop. (Note: I do not intend to put the API on a live server, it's just going on my localhost and then I can separate my CMS from Core Data). What I have at the moment is this; Code: // This only outputs companies, but I want it to output a company and all her employees in a child node. I am able to do $c->stored and $c->workers->stored, but never together. I tried doing $c->stored + $c->workers->stored, but this is obviously wrong and should not be done this way. The output of the company json is like this; Code: {"id":1,"name":"Company A"} And I would like to append a list of employees under Company A's node, but I am not quite sure how to do this with Datamapper and the REST controller. Any hints and tips would be greatly appreciated.
[eluser]Davva[/eluser]
I would like to bump this question. I too am trying to output a tree-like json structure from a DataMapper object (with child objects). I my case, I have the following scenario: One account can have many clients that can have many projects. I would like to output json with all clients and projects for a given account. I know this code is not correct, but this is how I imagine it would sort of work: Code: $clients = new Client(); .. and the output would be: Code: [ How should I set this up in order to make it work? |
Welcome Guest, Not a member yet? Register Sign In |