[quote]http://blog.csdn.net/jake451/article/details/6117272
需求:原网站功能庞大,数据库服务器与数据库众多
问题:YII单DB方式已经满足不了我们的需求,急切需要对YII进行扩展设计,支持多DB方式
解决方法:
1、在protected/components目录下新建Ar.php脚本文件。
view plaincopy to clipboardprint?
01.<?php
02.class Ar extends CActiveRecord
03.{
04. static $database = array();
05. public $dbname = 'db';
06. public function __construct($scenario='insert', $dbname = '')
07. {
08. if (!emptyempty($dbname))
09. $this->dbname = $dbname;
10.
11. parent::__construct($scenario);
12. }
13.
14. public function getDbConnection()
15. {
16. $dbname = $this->dbname;
17. if (self::$database[$dbname]!==null)
18. {
19. return self::$database[$dbname];
20. }
21. else
22. {
23. if ($this->dbname == 'db')
24. {
25. self::$database[$dbname] = Yii::app()->getDb();
26. }
27. else
28. {
29. self::$database[$dbname] = Yii::app()->$dbname;
30. }
31.
32. if(self::$database[$dbname] instanceof CDbConnection)
33. {
34. self::$database[$dbname]->setActive(true);
35. return self::$database[$dbname];
36. }
37. else
38. throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));
39. }
40. }
41.}
<?php
class Ar extends CActiveRecord
{
static $database = array();
public $dbname = 'db';
public function __construct($scenario='insert', $dbname = '')
{
if (!empty($dbname))
$this->dbname = $dbname;
parent::__construct($scenario);
}
public function getDbConnection()
{
$dbname = $this->dbname;
if (self::$database[$dbname]!==null)
{
return self::$database[$dbname];
}
else
{
if ($this->dbname == 'db')
{
self::$database[$dbname] = Yii::app()->getDb();
}
else
{
self::$database[$dbname] = Yii::app()->$dbname;
}
if(self::$database[$dbname] instanceof CDbConnection)
{
self::$database[$dbname]->setActive(true);
return self::$database[$dbname];
}
else
throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));
}
}
}
2、数据库配置
为了方便与清晰我们的数据库配置文件,我们将数据库配置文件与main.php文件进行分离。
(1)首先我们看看main.php文件的配置,代码如下
view plaincopy to clipboardprint?
01.<?php
02.$config = array(
03. 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
04. 'name'=>'My Web Application',
05. //...此处代码已省略,去掉components里面的db文件配置
06. );
07.
08.$database = @include_once dirname(__FILE__).'/database.php';
09.if(!emptyempty($database))
10.{
11. $config['components'] = @array_merge($config['components'], $database);
12.}
13.return $config;
<?php
$config = array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'My Web Application',
//...此处代码已省略,去掉components里面的db文件配置
);
$database = @include_once dirname(__FILE__).'/database.php';
if(!empty($database))
{
$config['components'] = @array_merge($config['components'], $database);
}
return $config;
(2)在配置文件目录(protected/config),新建database.php脚本文件,代码如下
view plaincopy to clipboardprint?
01.<?php
02.return array(
03. /** 默认数据库 */
04. 'db'=>array(
05. 'connectionString' => 'dblib:host=test;dbname=test',
06. 'emulatePrepare' => false,
07. 'username' => 'user',
08. 'password' => 'pass',
09. 'charset' => 'gbk',
10. ),
11. /** mysql配置 */
12. /*'shop'=>array(
13. 'connectionString' => 'mysql:host=mysql.data.com;dbname=test1',
14. 'emulatePrepare' => true,
15. 'username' => 'root',
16. 'password' => '123456',
17. 'charset' => 'utf8',
18. 'tablePrefix' => '',
19. ),*/
20.
21. /** mssql配置 */
22. 'test1'=>array(
23. 'connectionString' => 'dblib:host=testHostname;dbname=test1',
24. 'emulatePrepare' => false,
25. 'username' => 'user',
26. 'password' => 'pass',
27. 'charset' => 'gbk',
28. ),);
<?php
return array(
/** 默认数据库 */
'db'=>array(
'connectionString' => 'dblib:host=test;dbname=test',
'emulatePrepare' => false,
'username' => 'user',
'password' => 'pass',
'charset' => 'gbk',
),
/** mysql配置 */
/*'shop'=>array(
'connectionString' => 'mysql:host=mysql.data.com;dbname=test1',
'emulatePrepare' => true,
'username' => 'root',
'password' => '123456',
'charset' => 'utf8',
'tablePrefix' => '',
),*/
/** mssql配置 */
'test1'=>array(
'connectionString' => 'dblib:host=testHostname;dbname=test1',
'emulatePrepare' => false,
'username' => 'user',
'password' => 'pass',
'charset' => 'gbk',
),);
(3)在使用的时候,我们需要对model文件进行简单的修改。
a、将继承文件类换成Ar
b、给model添加属性$dbname设置所在数据库配置文件名称(不设置则为默认DB)
实例代码如下:
view plaincopy to clipboardprint?
01.<?php
02.class test extends Ar
03.{
04. //设置处理所在数据库,不设置为默认DB
05. $dbname = 'test1';
06. //...省略代码若干行
07.} [/quote]
需求:原网站功能庞大,数据库服务器与数据库众多
问题:YII单DB方式已经满足不了我们的需求,急切需要对YII进行扩展设计,支持多DB方式
解决方法:
1、在protected/components目录下新建Ar.php脚本文件。
view plaincopy to clipboardprint?
01.<?php
02.class Ar extends CActiveRecord
03.{
04. static $database = array();
05. public $dbname = 'db';
06. public function __construct($scenario='insert', $dbname = '')
07. {
08. if (!emptyempty($dbname))
09. $this->dbname = $dbname;
10.
11. parent::__construct($scenario);
12. }
13.
14. public function getDbConnection()
15. {
16. $dbname = $this->dbname;
17. if (self::$database[$dbname]!==null)
18. {
19. return self::$database[$dbname];
20. }
21. else
22. {
23. if ($this->dbname == 'db')
24. {
25. self::$database[$dbname] = Yii::app()->getDb();
26. }
27. else
28. {
29. self::$database[$dbname] = Yii::app()->$dbname;
30. }
31.
32. if(self::$database[$dbname] instanceof CDbConnection)
33. {
34. self::$database[$dbname]->setActive(true);
35. return self::$database[$dbname];
36. }
37. else
38. throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));
39. }
40. }
41.}
<?php
class Ar extends CActiveRecord
{
static $database = array();
public $dbname = 'db';
public function __construct($scenario='insert', $dbname = '')
{
if (!empty($dbname))
$this->dbname = $dbname;
parent::__construct($scenario);
}
public function getDbConnection()
{
$dbname = $this->dbname;
if (self::$database[$dbname]!==null)
{
return self::$database[$dbname];
}
else
{
if ($this->dbname == 'db')
{
self::$database[$dbname] = Yii::app()->getDb();
}
else
{
self::$database[$dbname] = Yii::app()->$dbname;
}
if(self::$database[$dbname] instanceof CDbConnection)
{
self::$database[$dbname]->setActive(true);
return self::$database[$dbname];
}
else
throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));
}
}
}
2、数据库配置
为了方便与清晰我们的数据库配置文件,我们将数据库配置文件与main.php文件进行分离。
(1)首先我们看看main.php文件的配置,代码如下
view plaincopy to clipboardprint?
01.<?php
02.$config = array(
03. 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
04. 'name'=>'My Web Application',
05. //...此处代码已省略,去掉components里面的db文件配置
06. );
07.
08.$database = @include_once dirname(__FILE__).'/database.php';
09.if(!emptyempty($database))
10.{
11. $config['components'] = @array_merge($config['components'], $database);
12.}
13.return $config;
<?php
$config = array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'My Web Application',
//...此处代码已省略,去掉components里面的db文件配置
);
$database = @include_once dirname(__FILE__).'/database.php';
if(!empty($database))
{
$config['components'] = @array_merge($config['components'], $database);
}
return $config;
(2)在配置文件目录(protected/config),新建database.php脚本文件,代码如下
view plaincopy to clipboardprint?
01.<?php
02.return array(
03. /** 默认数据库 */
04. 'db'=>array(
05. 'connectionString' => 'dblib:host=test;dbname=test',
06. 'emulatePrepare' => false,
07. 'username' => 'user',
08. 'password' => 'pass',
09. 'charset' => 'gbk',
10. ),
11. /** mysql配置 */
12. /*'shop'=>array(
13. 'connectionString' => 'mysql:host=mysql.data.com;dbname=test1',
14. 'emulatePrepare' => true,
15. 'username' => 'root',
16. 'password' => '123456',
17. 'charset' => 'utf8',
18. 'tablePrefix' => '',
19. ),*/
20.
21. /** mssql配置 */
22. 'test1'=>array(
23. 'connectionString' => 'dblib:host=testHostname;dbname=test1',
24. 'emulatePrepare' => false,
25. 'username' => 'user',
26. 'password' => 'pass',
27. 'charset' => 'gbk',
28. ),);
<?php
return array(
/** 默认数据库 */
'db'=>array(
'connectionString' => 'dblib:host=test;dbname=test',
'emulatePrepare' => false,
'username' => 'user',
'password' => 'pass',
'charset' => 'gbk',
),
/** mysql配置 */
/*'shop'=>array(
'connectionString' => 'mysql:host=mysql.data.com;dbname=test1',
'emulatePrepare' => true,
'username' => 'root',
'password' => '123456',
'charset' => 'utf8',
'tablePrefix' => '',
),*/
/** mssql配置 */
'test1'=>array(
'connectionString' => 'dblib:host=testHostname;dbname=test1',
'emulatePrepare' => false,
'username' => 'user',
'password' => 'pass',
'charset' => 'gbk',
),);
(3)在使用的时候,我们需要对model文件进行简单的修改。
a、将继承文件类换成Ar
b、给model添加属性$dbname设置所在数据库配置文件名称(不设置则为默认DB)
实例代码如下:
view plaincopy to clipboardprint?
01.<?php
02.class test extends Ar
03.{
04. //设置处理所在数据库,不设置为默认DB
05. $dbname = 'test1';
06. //...省略代码若干行
07.} [/quote]