YII 分库分表扩展(支持主从)
程序员文章站
2022-05-04 08:36:15
...
php代码
<?php // 根据uid进行分库分表 $uid = 10; $model = UserContact::model($uid); // 覆盖了原有参数,改为传人分库分表所依据的值 $data = $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();