CREATE TABLE user ( id int, name varchar(20) ); INSERT INTO user (id, name) VALUES (1, 'Alex C.'), (2, 'Max T.'); CREATE TABLE group ( id int, name varchar(20) ); INSERT INTO group (id, name) VALUES (1, 'Admin'), (2, 'Moderator'), (3, 'User'); CREATE TABLE foto ( id int, name varchar(20), filename varchar(50) ); INSERT INTO foto (id, name, filename) VALUES (1, 'My PC', 'my-pc-2012.jpg'), (2, 'Friends', 'P1000766.JPG'), (3, 'Friends Again', 'P1000767.JPG'); CREATE TABLE group_user ( group_id int, user_id int ); INSERT INTO group_user (group_id, user_id) VALUES (1, 2), (2, 2), (3, 2), (1, 3); CREATE TABLE foto_user ( foto_id int, user_id int ); INSERT INTO foto_user (foto_id, user_id) VALUES (1, 1), (2, 2), (3, 2), (3, 1);We want show all admins fotos.
SELECT group.name, user.name, foto.name, foto.filename FROM group LEFT JOIN group_user ON (group.id = group_user.group_id) LEFT JOIN user ON (user.id = group_user.user_id) LEFT JOIN foto_user ON (user.id = foto_user.user_id) LEFT JOIN foto ON (foto.id = foto_user.foto_id) WHERE group.id = 1; group.name | user.name | foto.name | foto.filename Admin | Max T. | My PC | my-pc-2012.jpg Admin | Max T. | Friends Again | P1000767.JPG
// get the info $group = Model_Group::find()->related('user')->related('user.foto')->where('id', '=', 1)->get(); // output some data echo $group->name; foreach ($group->user as $user) { echo "Foto's of user:", $user->name; foreach($user->foto as $foto) { echo "* ", $foto->name, $foto->filename; } }
It looks like you're new here. If you want to get involved, click one of these buttons!