CodeIgniter Forums
The Controlller and Model same filename -> wrong ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: The Controlller and Model same filename -> wrong ? (/showthread.php?tid=17549)

Pages: 1 2 3


The Controlller and Model same filename -> wrong ? - El Forum - 04-08-2009

[eluser]Snoob[/eluser]
I have the controller and model is same file name(ex user.php)'
And when I call model user:
Code:
$this->load->model('User');    
    $this->User->a();
it show a blank page. I changed the filename of model to usera.php, and it ok. Can one explant me why? (Sorry about my bad english)


The Controlller and Model same filename -> wrong ? - El Forum - 04-08-2009

[eluser]Mike Ryan[/eluser]
You can't have two classes with identical names loaded simultaneously. Try calling your controller User and your model Usermodel. When loading the model, use:
Code:
$this->load->model('Usermodel','user');
$this->user->some_function_in_model();



The Controlller and Model same filename -> wrong ? - El Forum - 04-08-2009

[eluser]Snoob[/eluser]
Thanks you


The Controlller and Model same filename -> wrong ? - El Forum - 04-08-2009

[eluser]Kromack[/eluser]
Quote:You can’t have two classes with identical names loaded simultaneously. Try calling your controller User and your model Usermodel. When loading the model, use:
$this->load->model('Usermodel','user');
$this->user->some_function_in_model();

You mean that your model is called "User" an his filename is "Usermodel" ?


The Controlller and Model same filename -> wrong ? - El Forum - 04-08-2009

[eluser]xwero[/eluser]
No you can set the object name that is added to the CI superobject, that is what Mike did.


The Controlller and Model same filename -> wrong ? - El Forum - 04-08-2009

[eluser]jedd[/eluser]
Snoob, Kromack, et al

If you're new to CI, and especially if you're new(ish) to PHP, you should probably avoid renaming Models in this way - it's likely to lead to confusion.

I think a superior approach is to spend a moment more on the design of your system - remember that Controllers are your user-facing component, and each should reflect and encapsulate the functions of a resource within your system. Your Models are your interface to your data, and should reflect the way your models will need to retrieve, store and modify that data. Yes, there are times that these two things are in perfect alignment, but it's not a requirement that they are, and it's often easier to conceptualise things if you keep the two goals, or intents, of Models and Controllers in mind.

It is also an excellent way of avoiding nomenclature clashes. User is the most frequent one that people stumble over. I find the '_model' or '_mdl' suffix feels cumbersome. I've found that thinking of users as People or Members makes things easier, as I can still have a users table (and maybe a model to manage that table), and my People/Member/etc controller will then manage the human related aspects of my site.

Just a different response to the problem of having a User controller, and a User_model model that you rename to User.


The Controlller and Model same filename -> wrong ? - El Forum - 04-08-2009

[eluser]xwero[/eluser]
Users is people!!!??? Wink


The Controlller and Model same filename -> wrong ? - El Forum - 04-08-2009

[eluser]Kromack[/eluser]
@jedd : /ignore (because my level of english don't allow me to answer correctly)

Quote:No you can set the object name that is added to the CI superobject, that is what Mike did.

Yes, sorry I read too speed, I didn't see the second parameter. Thx.


The Controlller and Model same filename -> wrong ? - El Forum - 04-08-2009

[eluser]drewbee[/eluser]
I always make all models name appended with a _model.php

So Login extends Controller

and

Login_model extends Model


The Controlller and Model same filename -> wrong ? - El Forum - 04-08-2009

[eluser]jedd[/eluser]
Hi drewbee - can I ask which way you design - do your models reflect user interactions, or do your controllers reflect your database schema?