I've got 2 tables:
+-----------+ +------------------------------------+--------------+
+ persons | | photos |
+-----------| +---------------------------------------------------+
+ id | name + | id | person_id | path | title |
+-----------+ +---------------------------------------------------+
+ 1 | Tom + | 1 | 2 | ~fred/me.png | Yo, it's Me! |
+ 2 | Fred + | 2 | 2 | ~fred/my_wife.png | I'm Susan |
+ 3 | Jack + | 3 | 1 | ~tom/my_dog.jpg | a woof |
+-----------+ +---------------------------------------------------+
which has are in this relationship:
Person hasMany Photo <-> Photo belongsTo Person
I'd like to list all the persons with their photos (even if someone does not have one, like Jack) and order by photos' title.
What SQL query (MySQL) should I write for this? Can I use joins in a one-to-many relationship?
PS: Just as an information, I'd like to be able to construct a such array with the records:
$persons = Array(
[0] => Array(
[id] => 1,
[name] => 'Tom',
[Photo] => Array(
[0] => Array(
[id] => 3,
[person_id] => 1,
[path] => '~tom/my_dog.jpg',
[title] => 'a woof' // 1st
)
)
),
[1] => Array(
[id] => 2,
[name] => 'Fred',
[Photo] => Array(
[0] => Array(
[id] => 2,
[person_id] => 2,
[path] => '~fred/my_wife.png',
[title] => "I'm Susan" // 2nd
),
[0] => Array(
[id] => 1,
[person_id] => 2,
[path] => '~fred/me.png',
[title] => "Yo, it's Me!" // 3rd
)
)
),
[2] => Array(
[id] => 3,
[name] => 'Jack',
[Photo] => Array()
)
)
Many thanks!