I'm designing a fairly complex web application using MySQL and PHP, and I need to write a CRUD interface for the database. I have many tables, with various one-to-many and many-to-many relationships between them. To keep things simple, let's focus on three tables: "students", "tutors", and "sessions". The operations for accessing/modifying records in individual tables are fairly straightforward, i.e.:
createStudent($name, $email, ...)
fetchStudent($student_id)
updateStudent($student_id, $name, $email, ...)
deleteStudent($student_id)
And similar functions for the other tables. I also have some browsing functions, with optional filter parameters:
fetchStudents($limit, $sort, ...)
fetchSessions($limit, $sort, ...)
These also seem fairly straightforward. However, I am struggling with a "sane" design for organizing the browsing functions that involve more than one table. My current approach is something like this:
// Fetch a list of tutors for a specified student
fetchStudentTutors($student_id, $limit, $sort, ...)
// Fetch a list of students for a specified tutor
fetchTutorStudents($student_id, $limit, $sort, ...)
// Fetch a list of sessions for a specified student
fetchStudentSessions($student_id, $limit, $sort, ...)
// Fetch a list of sessions with a specified tutor for a specified student
fetchStudentSessionsWithTutor($student_id, $tutor_id, $limit, $sort, ...)
However, the possible types of queries, and thus necessary functions, theoretically grow exponentially (or perhaps factorially) with each new table (in reality some relations will not exist, like mapping multiple tutors to a single session). My question is, is there a better approach? Should I try to create a single function, like:
// $tutor_id, $student_id are also optional parameters
fetchSessions($tutor_id, $student_id, $limit, $sort, ...)
where the constraints on other tables are part of the filtering? In this case, should I try to built a single, monster query inside said function that considers all of these filters? Or, should I code a bunch of if-else blocks to handle all possible permutations?
Better yet, is there an existing project that can make it easier to tackle this kind of problem in PHP and SQL?