Posted By

shoelessone on 10/02/11


Tagged

php codeigniter


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

fireball70


CodeIgniter - basic question


 / Published in: PHP
 

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!

  1. //The basic example controller. This is the basis for the different examples
  2. Report_Controller {
  3.  
  4. public function list_classroom($classroomNumber){
  5.  
  6. $data["classroomInfo"] = $this->classroom_model->getClassroomInfo($classroomNumber);
  7.  
  8. }
  9.  
  10. }
  11.  
  12. Classroom_Model{
  13. //We're going to do this a few different ways!! Exciting!
  14. $this->load->model("student_model");
  15. $this->load->library("student_library");
  16.  
  17. public function getClassroomInfo($classroomNumber){
  18. $classroomInfo = array();
  19. $classroomInfo["classTitle"] = $this->db->query("SELECT....")....;
  20. $classroomInfo["teacher"] = $this->db->query("SELECT...")...;
  21.  
  22. //FIRST WAY
  23. $classroomInfo["students"] = $this->student_model->getStudentsInClass($classroomNumber);
  24.  
  25. //SECOND WAY!!
  26. $studens = array();
  27. $studentIDs = $this->db->query("SELECT * FROM students_table WHERE classroom = $classroomNumber");
  28. foreach($studentIDs as $studentID){
  29. $students[] = new Student($studentID);
  30. }
  31.  
  32. $classroomInfo["students"] = $students;
  33.  
  34. return $classroomInfo;
  35. }
  36.  
  37. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: fireball70 on February 15, 2012
  1. Do not load models in model. Load all models in the controller. My models do not load libraries too. All logic is in the controller,
  2. Separate your data and use model with simple queries: $data['classroominfo'] = $this->classroommodel->getcalssroominfobyid($classroomid); $data['classroomstudents'] = $this->classroommodel->getcalssroomstudentsbyid($classroomid);

  3. 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.

Posted By: fireball70 on February 15, 2012
  1. Do not load models in model. Load all models in the controller. My models do not load libraries too. All logic is in the controller,
  2. Separate your data and use model with simple queries: $data[‘classroominfo’] = $this→classroommodel→getcalssroominfobyid($classroomid); $data[‘classroomstudents’] = $this→classroommodel→getcalssroomstudentsbyid($classroomid);

  3. 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.

You need to login to post a comment.