Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] DX_AUTH class name conflicts with Doctrine
#1

[eluser]RedLeader[/eluser]
Hi all,

Okay, so I've got a new CI project and I immediately set it up with DX_AUTH as I am comfortable with how it deals with users. I also set up Doctrine as I love how Doctrine works too.

And here lies an interesting problem!

I made teh mistake (?) off loading the CI-Doctrine plugin (http://ellislab.com/forums/viewthread/98746/) in MY_Controller constructor. However, when I try to login and DX_AUTH loads its user model so it can check who I am, I get the following error:
Code:
Fatal error: Cannot redeclare class Users in C:\www\projectx\system\application\models\dx_auth\users.php on line 184

The problem is that because I have Doctrine loading in the MY_Controller constructor it has already loaded it's own model and class called "Users" causing PHP to throw this error because DX_AUTH's User class wants to load. Obviously this could happen for any of DX_AUTH's tables as Doctrine has built it's own model files from the entire database (so not just "users" but "ci_session", "users_temp", etc, etc).

I can solve this by NOT loading Doctrine so early and perhaps just being a bit more specific as to where I load DX_AUTH and Doctrine but I am worried there may be a point where I'll want both!

Has anyone else experienced this problem? If so what was your solution?

I am tempted to rename DX_AUTH's classes so this conflict can never happen. Basically try to "namespace" them by prefixing them with "dx_"?

Thanks for any help you can offer.
#2

[eluser]Daniel Moore[/eluser]
I have already had to go through "prefixing". I personally think when any "plugin" like DX_AUTH uses names that are common, like "users" etc., that they should always be prefixed with something that would be unique to that particular plug-in. Otherwise, there may come a day where a plug-in will bite you with this.

I always use prefixes when coding in case it becomes something I'll want to share in the future.

Just my 2 cents.

Of course, when you do this to a plug-in, you will have to do it again anytime you download a bug-fix or upgrade for that plug-in as well.

Best of luck, whatever you decide.
#3

[eluser]xwero[/eluser]
i wonder why DX_AUTH has a model named users? How will a developer know where the model comes from?

I think naming the library's model after the library would be the most sensible thing to do.
#4

[eluser]RedLeader[/eluser]
Thanks guys.

DX_AUTH does has the option to add a tablename prefix to it's tables and it suggests "dx_" as the prefix but it defaults to no prefix. However this prefix is for the tables themselves (in the database) and as such Doctrine would also pick up on these names and the same problem would occur.

So I have left the tablenames the same, NOT added a prefix in the DX_AUTH config file and simply prefixed all the model php files in "models/dx_auth" folder with "dx_". I also needed to prefix all the model classes in each of those files with "Dx_" so they matched the filename and CI runs them properly.

So the files are called:
Code:
/system/application/models/dx_auth/dx_login_attempts.php
/system/application/models/dx_auth/dx_permissions.php
/system/application/models/dx_auth/dx_roles.php
/system/application/models/dx_auth/dx_user_autologin.php
/system/application/models/dx_auth/dx_user_profile.php
/system/application/models/dx_auth/dx_user_temp.php
/system/application/models/dx_auth/dx_users.php

And each model class is now like this:
Code:
<?php

class Dx_login_Attempts extends Model
{

    function __construct()
    {
        parent::Model();
        
        // Other stuff
        $this->_prefix = $this->config->item('DX_table_prefix');
        $this->_table = $this->_prefix . $this->config->item('DX_login_attempts_table');
    }

...
(NOTE: I changed the constructor from PHP4 to the PHP5 __construct() because I prefer it)

I then had to search and replace any mention of "$this->ci->load->model('dx_auth/[name_of_class])" to "$this->ci->load->model('dx_auth/dx_[name_of_class])" in the DX_AUTH library file: "/system/application/libraries/DX_Auth.php"

Now it all seems to work... Smile




Theme © iAndrew 2016 - Forum software by © MyBB