【问题标题】:Kohana 3.2 Using Auth Module on multiple databasesKohana 3.2 在多个数据库上使用 Auth 模块
【发布时间】:2023-04-04 06:14:01
【问题描述】:

我正在使用带有 ORM 驱动程序和本机会话的模块身份验证。

数据库配置 'default' 和 'customer_1' 存在于 application/config/database.php 中。

在登录之前,我更改了默认数据库配置:

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

这在模块验证登录之前确实有效!

设置默认数据库配置后:

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

这会导致以下错误:

表 'default_database.users' 不存在 [ SHOW FULL COLUMNS FROM `users` ]

由于某种原因,它使用初始默认数据库配置。

我的问题:如何为 Module Auth 设置默认数据库?

【问题讨论】:

    标签: kohana authentication


    【解决方案1】:

    让我们稍微了解一下。

    您实际上使用的是 ORM/Auth 而不仅仅是 Auth。 ORM/Auth 中的 ORM 配置为使用默认数据库(如果未指定)。它允许您通过在 ORM.php 文件中重载 $_db_group 来覆盖此选项。

    让我们使用 Kohana 的级联文件系统来覆盖它。创建一个新文件: classes/auth.php 。插入此代码:

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

    一切就绪。

    【讨论】:

    • 嘿,感谢您的回复 Gaurav Patel。代码是: class ORM extends Kohana_ORM { function __construct() { $this-&gt;_db_group = Kohana::$config-&gt;load('database')-&gt;get('customer_1'); } } 但是当 Auth::instance() 被调用时,这会导致错误:
      Class 'Auth' not found
    • 您是否在引导程序中加载了 Auth 模块?
    【解决方案2】:

    如果您希望 Auth 模块使用与其他模型不同的数据库,您应该按照Gaurav Patel 的建议使用$_db_group。但是,您应该只覆盖 Auth ORM 模型(用户、角色和 user_token),而不是 ORM 类:

    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';
    }
    

    【讨论】:

    • 这很完美!但是,如何在调用 Auth::instance()->login() 之前更改 $_db_group ?
    • 扩展ORM类,为$_db_group添加setter并在登录前调用
    • 我试过这个,但它没有解决问题:APPATH/classes/auth/orm: class Auth_ORM extends Kohana_Auth_ORM { public function set_db_group($db_group) { $this-&gt;_db_group = $db_group; } } 在调用 Auth::instance()-&gt;login() 之前:Auth::instance()-&gt;set_db_group('customer_2'); 也不是使用不同的数据库然后其他模型,整个应用程序正在更改为另一个数据库,但是 Modulde Auth 只是坚持使用默认值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    • 2013-03-20
    相关资源
    最近更新 更多