I got 2 relational tables:
table "categories"
id int(11)
title varchar(255)
table "posts"
id int(11)
title varhcar(255)
category_id int(11) // foreign key
If I select the "categories" table, I would like to get a PHP array with al the categories (as in "SELECT * categories") but including an inner array with all its posts:
Array (
/* first category */
[0] = Array (
[id] => 1
[title] => "Rock"
/* all its posts */
[posts] => Array (
[0] = Array(
[id] = 100
[title] = "Rock post title"
[category_id] = 1
)
[1] = Array(
[id] = 101
[title] = "Other rock post title"
[category_id] = 1
)
)
/* second category */
[1] = Array (
)
/* ... */
)
If I just made a "join" query I get all the results combined, something like:
id title id title category_id
1 Rock 100 "Rock post title" 1
2 Rock 101 "Other rock post" 1
3 Rock 102 "Final rock post" 1
I don't want to make multiple queries, because I think is inefficient.
Is there anyway to achive the desire result with one query?
I know CakePHP manage to return relational tables results in this format, so I'm looking to achieve the same result.