8

I am using CI2.0 with PHP 5.3

I just started to use “Datamapper ORM” and it is excellent!! however their is a one big problem regarding the classes’ names

I have a database table called “users” so my dm model is “user” and also I have a controller with the same name “user”?

so using the “user” model within the “user” controller is imposible!!

what is the best way to solve this problem?

Many Thanks

best regards

tereško
  • 58,060
  • 25
  • 98
  • 150
ahmed
  • 14,316
  • 30
  • 94
  • 127

6 Answers6

16

One of the drawbacks in CodeIgniter is that you cannot name a controller, model or library the same thing. This is mainly a PHP issue as obviously you cannot name anything the same, but it can be avoided in two ways.

  1. PHP Namespaces - no can do, they are PHP 5.3 only and 90% of the community would kick off if they were implemented.
  2. Controller Prefixes - This is something I would love to add, but... well it would break stuff for everyone. We'll have to wait until 2.1 at least for a change that big.

For now all I can recommend is you name your models and libraries carefully.

Controller - Users
Library - User
Model - User_model | User_m

It's annoying, but just one of those things for now.

Phil Sturgeon
  • 30,637
  • 12
  • 78
  • 117
  • thanks I would like to go for namespaces to solve this problem however it is not possible -with Datamapper- as far as I know, see this http://codeigniter.com/forums/viewthread/149380/ thank again – ahmed Mar 03 '11 at 23:19
  • Nope, Namespaces involve changing how CodeIgniter and Datamapper work fundamentally, so you _m on your model. Much easier :) – Phil Sturgeon Mar 04 '11 at 10:17
3

so using the “user” model within the “user” controller is imposible!!

Umm no it isn't, you need to check the UserGuide more carefully ;)

You may give your model a name other than what it is orginaly defined as:

If you would like your model assigned to a different object name you can specify it via the second parameter of the loading function:

$this->load->model('Model_name', 'fubar');    
$this->fubar->function();
Jakub
  • 20,418
  • 8
  • 65
  • 92
  • Thanks I am aware of the this part of the user guide however it will not work in my case because "DataMapper Models are very different than CodeIgniter Models. Unlike CI models, you should never load them explicitly (Datamapper ORM handles that automatically), and they should never be added to autoload." http://datamapper.wanwizard.eu/pages/models.html you dont load datamapper models like other codeigniter models, thanks for trying I really appreciate – ahmed Mar 03 '11 at 04:49
  • @Jakub your answer is wrong.@ahmed asked about using codeIgniter with ORM. – waqas Jan 17 '13 at 06:48
0

I would suggest :

  • Controller - Users or/and User
  • Library - Users_lib or Users_library or User_lib or User_library
  • Model - Users_model or Users_m or User_model or User_m

Keep the words User and Users for the controller so you keep nice url.

Just be consistent with the use of plural or singular for the model and controller.

Tchoupi
  • 14,560
  • 5
  • 37
  • 71
Parisiam
  • 59
  • 1
  • 1
0

I named in case of 'Project' like that:

Controller:
Project extends MY_Controller extends CI_Controller

Model:
ProjectModel extends Model

In View
project/projectManager // as main content
project/_projectItem // with _ underscore as subView

That works fine at all.

Stefan Brinkmann
  • 965
  • 8
  • 11
0

The way I handle it is to end all my controller name's with _controller.

I use datamapper as well so I had a lot of name collisions. For users I have a User (model) and a User_Controller.

Then i simply add:

$route['user'] = 'user_controller';

to the config/routes.php file and voila.

Only downside of this is that if you have a large project with a lot of pages (like me) you end up with a huge routes.php file especially if you have a lot of functions that have a lot of parameters, the routing can become quite a pain in the ass.

sashkello
  • 17,306
  • 24
  • 81
  • 109
Haskabab
  • 65
  • 2
  • 7
0

All you need to do is change your controller file name and class name to "Users". You don't need to change your model name.

controller: users
model: user
db table: users

Take a look at the Datamapper docs.

David Xia
  • 5,075
  • 7
  • 35
  • 52