FirmView
FirmView

Reputation: 3150

Naming codeigniter model

How should I name models when there is more than one table involved?

for example,

//for students table

class Students extends Model{

     function addStudent(){  
        //code  
     };

     function editStudent($id){  
        //code  
     }; 

     function deleteStudent($id){  
        //code  
     }; 

     function viewStudent($id){  
        //code  
     }; 

     function viewStudents(){  
        //code  
     };
}

//for professors table

class Professors extends Model{

     function addProfessor(){  
        //code  
     };

     function editProfessor($id){  
        //code  
     }; 

     function deleteProfessor($id){  
        //code  
     }; 

     function viewProfessor($id){  
        //code  
     }; 

     function viewProfessors(){  
        //code  
     };
}

If i want to display sudents along with their professors, should a separate model has to be written like,

 class StudentsProfessors extends Model{
       function get_students_professors(){
          // $query = SELECT s.name, p.name FROM students AS s, professors AS p WHERE s.id=p.id 
       }
 }

or, the above method, function get_students_professors can be written in Students class or Professors class.

What is the best practice?

Upvotes: 1

Views: 1431

Answers (2)

thpl
thpl

Reputation: 5910

CodeIgniters MVC implementation is one big bunch of [profanity removed]. What I would suggest is that you assemble the relations by yourself. Pass a instance of your Professors Model to your Students model and upside down.

So that you can do the following: $this->student->with('professor')->get(1); or something similar.

You might need to hack CI's Model in order to be able to achieve this.

There are already some implementations that look kinda solid (only seen from the docs). Take a look at this: https://github.com/jamierumbelow/codeigniter-base-model

Upvotes: 1

Robin Castlin
Robin Castlin

Reputation: 10996

Since this is a question with many different answers for many different reasons to why, the best thing would be to ask yourself what feels most logical and act accordingly. Worst case, you come back a year later and realizes "Why didn't I do like that instead", and you learn(ed) how to do it in the future.

In your case I would not create a new model. If you'd create a new model for every relation between tables, you'd end up with a lot of models to keep track on eventually.

Apart from your actual question, the camelCasing and the repetition of name is something I would change. I rather have $this->students->add() over $this->students->addStudent(), and I'd personally even merge both view functions to do the same thing but with additional WHERE and LIMIT is value is given.


I'd simply put that function inside the model with most relevance. In your example, they look equally relevant, so I would simply put in in Students with a function named join_with_professors, view_join_professors or similiar. I usually end up with the best function name during given circumstances.

Upvotes: 1

Related Questions