Design pattern to handle queries using multiple models

I am presented with a dilemma while trying to re-designing the class structure for my PHP/MySQL application to make it more elegant and conform it to the SOLID principle.

The problem goes like this:

Let as assume, there is an abstract class called person which has certain properties to define a generic person, such as name, age, date of birth etc.

There are two classes, student, and teacher, that implements this abstract class. They add their own unique properties to it.

I have designed all the three classes to include all the operational logic (details of which are not relevant in context of the question).

Now, I need to create views/reports/data grids which contain details from multiple classes, for example, say, a list of all students doing projects in Chemistry mentored by a teacher whose name is the parameter to the query.

This is just one example of a view, there are many different views in the application, which uses data from 3-4 tables, and each of them have multiple input parameters to generate them.

Considering this particular example, I have written the relevant query using JOIN and the results are as expected and proper, now here is the dilemma:

Keeping in mind the single responsibility principle, where should I keep this query? It does not belong to either Student class, or Teacher class or any other classes currently present.

a) Should I create a new class, say dataView class, and design it as a MVC pattern and keep the query there? What about the other views? how do they fit in this architecture?

b) Should I not keep the query in code at all, and make it DB View ?

c) Am I completely wrong in the approach? If so what is the right approach?

My considerations are as follows:

a) should be easy to add new views later on if requirement comes, without having to copy-paste-modify code

b) would like to make it as loosely coupled as possible so that if minor db structure changes happen, it does not break

I did google searches on report design and OOP report generators, but all the result seem to focus on the visual design of the report rather than fetching the data. I have already taken care of the visual aspect of the report using MVC with html templates.

I am sure this is a very fundamental problem with known solution, but I am somehow not able to find it (maybe searching with wrong keyword).

Edit1: Modified the title to make it more relevant

Edit2: The accepted answer got me thinking in the right direction and identify my design flaws, which eventually led me to find this question and the solution in Stack Overflow which gave me the detailed answer to clear the confusion.

Your title does not seem to mach your question, and your question has little to do with reporting, so that might explain why you couldn’t find much.

Your problem start to emerge here:

I have designed all the three classes to include all the operational logic (details of which are not relevant in context of the question).

I don’t think you should put “operational logic” (do you mean data access operations like get, update and delete?) in your model classes.

for example, say, a list of all students doing projects in Chemistry mentored by a teacher whose name is the parameter to the query.

To me, it sounds like you’re looking for the repository pattern. Something like:

class StudentRepository
{
    private $_database;

    function GetStudentByID($id)
    {
        return $_database..[query with $id];
    }

    function GetStudentsForSubjectAndTeacher($subjectID, $teacherID)
    {
        return $_database..[query with $subjectID, $teacherID];
    }
}

Now everywhere you want to find all students for a given subject and teacher, you just create an instance of this repository and call the appropriate method.

1

Well, if i understand the query, your dilemma is where to put this query and not whether the query is to be put in a model class considering you are following MVC pattern.

My take:
If you see the query, you are ultimately fetching students. Fetching students can be based on projects, male, female, class, age, teachers etc.

I feel, the model should be student_model.php which has various methods, where in your query would go

eg:
fetchMaleStudents(), fetchFemaleStudents(), fetchStudentsForProjects(projectName), fetchStudentsForTeacher(teachersName) and so on.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật