Yii框架分表设计
2011-12-15 18:22
155 查看
需求:原网站功能庞大,数据库服务器与数据库众多,有部分数据单表已经无法满足我们的需求。我们便有了分表的需求
问题:YII单表方式已经满足不了我们的需求,急切需要对YII进行扩展设计,支持数据库分表设计
解决方法:1、新建protected/sinashowExt/JActiveRecord.php文件
.2、为了兼容以前的程序。我们重写protected/component/Ar.php文件来继承JActiveRecord类,代码如下:
问题:YII单表方式已经满足不了我们的需求,急切需要对YII进行扩展设计,支持数据库分表设计
解决方法:1、新建protected/sinashowExt/JActiveRecord.php文件
class JUnicomActiveRecord extends Ar { public $dbname = 'unicom1'; public $userIdKey = 'user_id'; protected $_userId; /** * 获得DBConnenction(non-PHPdoc) * @see CActiveRecord::getDbConnection() */ public function getDbConnection() { if ($this->hasProperty($this->userIdKey)) { $this->dbname = $this->chooseDb($this->{$this->userIdKey}); } elseif ($this->_userId) { $this->dbname = $this->chooseDb($this->_userId); } return parent::getDbConnection(); } /** * 选择数据库 */ public function chooseDb($userId) { $unicom = 'unicom2'; $userId = (float)$userId; switch ($userId) { case $userId >= 10000 && $userId <= 29999999: $unicom = 'unicom1'; break; case $userId >= 30000000 && $userId <= 39999999: $unicom = 'unicom2'; break; case $userId >= 40000000 && $userId <= 49999999: $unicom = 'unicom3'; break; case $userId >= 50000000 && $userId <= 59999999: $unicom = 'unicom4'; break; case $userId >= 60000000 && $userId <= 69999999: $unicom = 'unicom5'; break; case $userId >= 70000000 && $userId <= 89999999: $unicom = 'unicom6'; break; case $userId >= 210000000 && $userId <= 239999999: $unicom = 'unicom7'; break; case $userId >= 240000000 && $userId <= 269999999: $unicom = 'unicom8'; break; case $userId >= 270000000 && $userId <= 299999999: $unicom = 'unicom27'; break; case $userId >= 1000000000 && ($userId % 2) == 0: $unicom = 'unicom9'; break; case $userId >= 1000000000 && ($userId % 2) == 1: $unicom = 'unicom10'; break; } return $unicom; } /** * 设置用户ID * * @param int $user_id */ public function setUserId($user_id) { $this->_userId = $user_id; return $this; } }
.2、为了兼容以前的程序。我们重写protected/component/Ar.php文件来继承JActiveRecord类,代码如下:
class Ar extends JActiveRecord { }
相关文章推荐
- 三、YII框架多模块设计(modules)
- yii框架,命令行应用程序设计
- Yii框架设计计划任务脚本+linux下crontab执行
- Yii框架分库设计
- 二、YII框架多DB设计
- yii框架,命令行应用程序设计
- yii框架设计学习笔记(一)
- 我的测试自动化框架原则设计手册(时刻更新)
- 自己动手设计 PHP MVC 框架(一)——URL
- yii框架的rules
- 框架和设计模式的区别
- Android开发框架/库学习,设计模式之策略模式
- Winform XiaoCai.WinformUI 框架界面设计
- 应用框架的设计与实现学习手札之类工厂服务——反射
- Yii框架中jquery表单验证插件用法示例
- 框架设计相关概念和设计思路
- 从jQuery谈库与框架的设计之优劣
- 谈谈Android 程序框架设计
- Yii框架的挂件的使用
- 基于DDD的.NET开发框架 - ABP分层设计