我希望有一个模块有自己的数据库连接,不同于Yii中的主应用程序

时间:2015-05-25 10:08:00

标签: php yii module

我的模型在 booksImported 模块

../保护/模块/ booksImported /模型/ Importedbooks.php

class Importedbooks extends CActiveRecord
{
 public function getDbConnection()
 {
    return Yii::app()->myModuleDb;
 }
}

我的 booksImported 模块

../保护/模块/ booksImported / BooksImportedModule.php

class BooksImportedModule extends CWebModule
{

public function init()
{
    // this method is called when the module is being created
    // you may place code here to customize the module or the application

    // import the module-level models and components
    $this->setImport(array(
        'booksImported.models.*',
        'booksImported.components.*',
    ));
}

public function beforeControllerAction($controller, $action)
{
    if(parent::beforeControllerAction($controller, $action))
    {
        // this method is called before any module controller action is performed
        // you may place customized code here
        return true;
    }
    else
        return false;
}
}

我的main.php,我创建了一个名为 myModuleDb 的新组件,我设法使用这个组件创建模型

    // database settings are configured in database.php
     'db'=>require(dirname(__FILE__).'/database.php'),

     'myModuleDb'=>array(
            'connectionString'=>'mysql:host=localhost;dbname=dbimports',
            'username' => 'my_username',
            'password' => 'my_password',
            'class'=>'CDbConnection'
            ),

当我尝试访问我的模块控件 localhost / testYii / booksImported / booksImported / index 时,我得到 CException,Property" BooksImportedModule.db"未定义 d。

0 个答案:

没有答案