user1315267
user1315267

Reputation:

Kohana 3.2 Using Auth Module on multiple databases

I'm using the Module Auth with ORM driver and native sessions.

The database config 'default' and 'customer_1' exists in application/config/database.php.

Before login i change the default database config with:

Kohana::$config->load('database')->default = Kohana::$config->load('database')->get('customer_1');

This does work before Module Auth login!

After setting the default database config:

if (Auth::instance()->login($_POST['username'], $_POST['password']) === TRUE) { Request::current()->redirect(); }

This results in the following error:

Table 'default_database.users' doesn't exist [ SHOW FULL COLUMNS FROM `users` ]

For some reason it use the initial default database config.

My Question: How do i set the default database for Module Auth ?

Upvotes: 0

Views: 1717

Answers (2)

matino
matino

Reputation: 17725

If you want Auth module to use different database then other models, you should use $_db_group as suggested by Gaurav Patel. However you should override only Auth ORM models (user, role and user_token), not ORM class:

APPATH/classes/model/user.php:

class Model_User extends Model_Auth_User
{
    protected $_db_group = 'customer_1';
}

APPATH/classes/model/role.php:

class Model_Role extends Model_Auth_Role
{
    protected $_db_group = 'customer_1';
}

APPATH/classes/model/user/token.php:

class Model_User_Token extends Model_Auth_User_Token
{
    protected $_db_group = 'customer_1';
}

Upvotes: 1

gspatel
gspatel

Reputation: 1128

Let's follow this through a bit.

You're actually using ORM/Auth and not just Auth. ORM in ORM/Auth is configured to use the default database if one isn't specified. It lets you override this option by overloading $_db_group in the ORM.php file.

Let's use Kohana's cascading filesystem to overwrite that. Make a new file: classes/auth.php . Insert this code:

<?php
class ORM extends Kohana_ORM {
    $_db_group = Kohana::$config->load('database')->get('customer_1');
}

All set.

Upvotes: 1

Related Questions