Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class name collision when using codeigniter 2.0 with Datamapper?

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

like image 900
ahmed Avatar asked Mar 03 '11 04:03

ahmed


2 Answers

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

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

  • http://codeigniter.com/user_guide/general/models.html#loading

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();
like image 23
Jakub Avatar answered Nov 09 '22 14:11

Jakub


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.

like image 143
Phil Sturgeon Avatar answered Nov 09 '22 12:11

Phil Sturgeon