1

I have this join select with doctrine:

->select("concat(c.name, ' ', c.second_name) as name_client","c.id as id_client", "p.id as id_project", "s.id as id_status","p.renew_date","p.domain")
                ->from("clients", "c")
                ->leftJoin("c", "projects", "p", "c.id = p.id_client")
                ->leftJoin("c", "todos", "t", "t.id = c.id")
                ->orderBy('p.renew_date', 'ASC');

I need to have a json output like this:

[
 {
   "id":1,
   "name_client" : "Tim",
   "projects" : [
      {
        "id" : 1
        "domain" : "www.test.com"
      },
      {
        "id" : 1
        "domain" : "www.test.com"
      }
    ],
    "todos":  [
      {
        "id" : 1
        "text" : "do something"
      },
      {
        "id" : 1
        "text" : "do something"
      }
    ]
 }
]

How can i loop my data to get this array structure? This is my array data:

Array
(
    [0] => Array
        (
            [name_client] => Aaaaaaa
            [id_todo] => 
            [text] => 
            [id_client] => 9
            [id_project] => 13
            [renew_date] => 
            [domain] => xxx.it
        )

    [1] => Array
        (
            [name_client] => Bbbbbb
            [id_todo] => 
            [text] => 
            [id_client] => 8
            [id_project] => 12
            [renew_date] => 2016-01-23
            [domain] => vvvv.it
        )

    [2] => Array
        (
            [name_client] => Dddddddd
            [id_todo] => 
            [text] => 
            [id_client] => 1
            [id_project] => 1
            [renew_date] => 2016-03-19
            [domain] => eeeeee.it
        )

    [3] => Array
        (
            [name_client] => Assssass
            [id_todo] => 
            [text] => 
            [id_client] => 5
            [id_project] => 8
            [renew_date] => 2016-03-26
            [domain] => erreerr.net 
        )

    [4] => Array
        (
            [name_client] => Eeeeeeee
            [id_todo] => 
            [text] => 
            [id_client] => 3
            [id_project] => 5
            [renew_date] => 2016-04-27
            [domain] => rrrrrrr.it
        )

    [5] => Array
        (
            [name_client] => Edsdee
            [id_todo] => 
            [text] => 
            [id_client] => 7
            [id_project] => 10
            [renew_date] => 2016-07-19
            [domain] => rrrrrrr.com
        )

    [6] => Array
        (
            [name_client] => Dddddddd
            [id_todo] => 
            [text] => 
            [id_client] => 6
            [id_project] => 9
            [renew_date] => 2016-07-27
            [domain] => rrrrrrrrr.it
        )

    [7] => Array
        (
            [name_client] => wwwwwwwww
            [id_todo] => 
            [text] => 
            [id_client] => 4
            [id_project] => 6
            [renew_date] => 2016-09-24
            [domain] => rrttttrrr.it 
        )

    [8] => Array
        (
            [name_client] => yyyyyyyy
            [id_todo] => 1
            [text] => Todo!!!!!
            [id_client] => 2
            [id_project] => 4
            [renew_date] => 2016-09-29
            [domain] => uuuuuuuuu.it 
        )

)

Thanks.

1
  • With the current query, what output do you get? Can you add it to your question so we know where the discrepancies lie? Commented Jan 25, 2016 at 17:56

1 Answer 1

1

can you please display what your "select" returns whith a print_r please, in order to have the data structure ?

Try this :

$clients = array();
foreach ($queryResults as $entry) {
    if (! isset($clients[$entry['id_client']])) {
        $clients[$entry['id_client']] = array(
            'id' => $entry['id_client'],
            'name_client' => $entry['name_client'],
            'todos' => array(),
            'projects' => array()
        );
    }
    if (! empty($entry['id_todo'])) {
        $clients[$entry['id_client']]["todos"][] = array(
            'id' => $entry['id_todo'],
            'text' => $entry['text']
        );
    }
    if (! empty($entry['id_project'])) {
        $clients[$entry['id_client']]["projects"][] = array(
            'id' => $entry['id_project'],
            'domain' => $entry['text']
        );
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.