I am having a bit of trouble figuring out what goes where in my application. I guess this is something of a fundamental lack of understanding I have, and I'm hoping somebody might be able to clear things up. This is all very much pseudo code.
In this example let's assume that I'm writing a program that keeps track of students grades, classroom attendance, etc. A school has a collection of classrooms, and a classroom has a collection of students. I'm going to create a basic controller, model, as well as another class I'll call Student.
Please note that the point of this is that I'm not sure if the Student class should be there, or if the Student model should be all I use, or if the Student class should have it's own ability to access the DB. In this simple example I'm simply looking at the student, but the same questions could apply to the Classroom, the School, etc. AGAIN, THIS IS PSEUDO CODE SO THERE ARE A FEW PLACES THAT I AM WRITING GARBAGE CODE TO ASK A QUESTION!
//The basic example controller. This is the basis for the different examples Report_Controller { public function list_classroom($classroomNumber){ $data["classroomInfo"] = $this->classroom_model->getClassroomInfo($classroomNumber); } } Classroom_Model{ //We're going to do this a few different ways!! Exciting! $this->load->model("student_model"); $this->load->library("student_library"); public function getClassroomInfo($classroomNumber){ $classroomInfo["classTitle"] = $this->db->query("SELECT....")....; $classroomInfo["teacher"] = $this->db->query("SELECT...")...; //FIRST WAY $classroomInfo["students"] = $this->student_model->getStudentsInClass($classroomNumber); //SECOND WAY!! $studentIDs = $this->db->query("SELECT * FROM students_table WHERE classroom = $classroomNumber"); foreach($studentIDs as $studentID){ $students[] = new Student($studentID); } $classroomInfo["students"] = $students; return $classroomInfo; } }
Comments
Subscribe to comments
You need to login to post a comment.

Separate your data and use model with simple queries: $data['classroominfo'] = $this->classroommodel->getcalssroominfobyid($classroomid); $data['classroomstudents'] = $this->classroommodel->getcalssroomstudentsbyid($classroomid);
In CI you must use a little hack to use something like $students[] = new Student($studentID)...but in your example it is a bad practice to populate objects in this way. Use queries like above.
Separate your data and use model with simple queries: $data[‘classroominfo’] = $this→classroommodel→getcalssroominfobyid($classroomid); $data[‘classroomstudents’] = $this→classroommodel→getcalssroomstudentsbyid($classroomid);
In CI you must use a little hack to use something like $students[] = new Student($studentID)…but in your example it is a bad practice to populate objects in this way. Use queries like above.