Welcome Guest, Not a member yet? Register   Sign In
Model naming conventions
#1

[eluser]rob897[/eluser]
I am trying to keep my models in their own subdirectories based on each controller..
So some of my controllers are:

ftp.php
accounts.php

so the models are as follows:
/system/application/models/ftp/request.php
/system/application/models/accounts/request.php

but the problem is when I try to load one of them within each others model, I get the Cannot redeclare class Request Error... I am assuming this is due to the classes having the same class name?

thanks
-rob
#2

[eluser]Alex.[/eluser]
Yep, to avoid naming conflicts try to use more specific names like FTPrequest
#3

[eluser]Dam1an[/eluser]
do you actually have more then one model per controller? I wouldn't bother with a seperate folder for each one unless they tend to have >3 models in each, I prefer to just follow a basic naming scheme.
I have 1 model per controller, and its always name_model.php, eg user.php, post.php etc
#4

[eluser]rob897[/eluser]
Yes I only have 1 model per controller, but theres some methods throughout some of the controllers that I access.. I will just rename them and possibly pull them out of the subfolders as well..

thanks
-rob
#5

[eluser]louis w[/eluser]
What if you ive it a different name when loading it?

(from user guide)

Code:
$this->load->model('Model_name', 'fubar');
$this->fubar->function();
#6

[eluser]rob897[/eluser]
Tried that and it didn't work.

-rob
#7

[eluser]the_fury[/eluser]
[quote author="rob897" date="1211504273"]Cannot redeclare class Request Error... I am assuming this is due to the classes having the same class name?

thanks
-rob[/quote]

Yep - that's exactly that's happening. Just rename them to FtpRequest.php, AccountRequest.php, etc. and you'll be golden.
#8

[eluser]the_fury[/eluser]
[quote author="louis w" date="1211526447"]What if you ive it a different name when loading it?

(from user guide)

Code:
$this->load->model('Model_name', 'fubar');
$this->fubar->function();
[/quote]

That won't do anything, 'fubar' would just be the variable name. Even though you're naming it 'fubar' it's still a 'Model_name' object.

take a look at Loader.php (CI_Loader.model) to see what's going on...

Code:
function model($model, $name = '', $db_conn = FALSE)
{
   ...
   $CI->$name = new $model();
   $CI->$name->_assign_libraries();
        
   $this->_ci_models[] = $name;    
}
#9

[eluser]Milos Dakic[/eluser]
I was searching around the forum to find the best way to name my models as well. Heaps of people have their little methods and ways which they are comfortable with.

I found it easier to name them as part of each module (as I'm using Modular Extension). So a models file will be specific to a module:

application/modules/module/models/module_model.php

In this you would replace "module" with the module name you are creating (eg. people). Then you would name the model. For this the best example is using the "People" module, which would make your model file people_global.php. Therefore this model could be used for all global aspects of the People module.

You could also make it more specific:

people_friends.php

Or something in that manner.

Its an easy way to remember which model is used for what, but is also useful if you take away the modular part of your system. If that is done all your models will stay in tact and can all be placed in the same folder.

Hope this helps someone who was looking for an easy naming convention.




Theme © iAndrew 2016 - Forum software by © MyBB