# YII sharded ext **Repository Path**: fagework/YII-sharded-ext ## Basic Information - **Project Name**: YII sharded ext - **Description**: YII 分库分表扩展,支持自定义的库表拆分方式,支持主从库配置 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 9 - **Created**: 2014-05-08 - **Last Updated**: 2020-12-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## YII 分库分表扩展 优化数据库连接,减少数据库连接次数及必连主库的限制,优化 实例化AR类、使用 Yii::app()->db->createCommand() 时连接数据库的操作 查询缓存方案,缓存sql中得SHOW语句,减少数据库查询次数 ### 配置 array( 'db'=>array( 'class'=>'CDbConnectionExt_v3', 'connectionString' => 'mysql:host=127.0.0.1;dbname=dbname;', 'slaves'=>array( array('connectionString'=>'mysql:host=127.0.0.1;dbname=dbname;charset=utf8;', 'username'=>'test', 'password'=>'test'), ), 'emulatePrepare' => true, 'waitSession' => 15, 'username' => 'test', 'password' => 'test', 'charset' => 'utf8', ), 'dbConnectionManager' => require(dirname(__FILE__).'/dbManager.php'), ) /** * 添加: protected/config/dbManager.php * 参考 test/dbManager.php */ /** * 示例: test/User.php * 具体的model 使用gii工具生成,模版在 gii目录, * sharded 分库分表 model 对应的模版 * sharded_normal 为普通model 模版 */ ?> ### 使用 findAll(); $data = $model->dbConnection->createCommand() ->select("*") ->from($model->tableName()) ->where('uid>1') ->limit(10) ->queryAll(); $db = Yii::app()->dbConnectionManager->sharded($uid); // 调用sharded方法 设置库和表 $data = $db->createCommand() ->select("*") ->from('user_contact_'.$db->shardedTableKey) ->where('uid>1') ->limit(10) ->queryAll();