Welcome Guest, Not a member yet? Register   Sign In
Datamapper and REST, outputting JOIN relationships in one go
#1

[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.
// filename: /modules/myapp/controllers/api.php
// ...
    function company_get()
    {
        $id = $this->get('id');
        
        if(!$id)
        {
            $this->response(NULL, 400);
        }
        
        $companiesList = new Company();
        $c = $companiesList->get_by_id($id);
            
        /*
                // Just debugging
        foreach($companiesList as $c):
        print $c->name;
        endforeach;
        */
        
        // Get all the included join fields for the worker
        $c->workers->include_join_fields()->get();
        
        
        /*
                // This should output all the join between workers and contracts where join_salary and join_length are coming from contracts
        foreach($c->workers as $w):
            print 'ID: '. $w->id;
            print '<br>Name: '. $w->name;
            print '<br>Payment: '. $w->join_salary; // $s->join_payment;
            print '<br>Length: '. $w->join_length;
            print '<br>';
        endforeach;
        */
        
        if($c)
        {
            // In datamapper I had to use stored
            $this->response($c->stored, 200); // 200 being the HTTP response code
        }

        else
        {
            $this->response(array('error' => 'Could not be found'), 404);
        }
    }

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.
#2

[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();
$clients
->select('name', 'id')
->where('account_id', $account_id)
->include_related('project')
->get();

echo $clients->all_to_json();

.. and the output would be:
Code:
[
     {id: 1, name: 'Name of client 1', projects: [
          {id: 1, name: 'Name of project 1'},
          {id: 2, name: 'Name of project 2'},
          {id: 3, name: 'Name of project 3'},
          ... more projects follow here
     ]},
     {id: 2, name: 'Name of client 2', projects: [
          {id: 1, name: 'Name of project'},
          {id: 2, name: 'Name of project'},
          {id: 3, name: 'Name of project'},
          ...
     ]},
     ... more clients follow here

]

How should I set this up in order to make it work?




Theme © iAndrew 2016 - Forum software by © MyBB