Home >php教程 >PHP源码 >YII 分库分表扩展(支持主从)

YII 分库分表扩展(支持主从)

PHP中文网
PHP中文网Original
2016-05-25 17:00:181428browse

php代码

<?php

// 根据uid进行分库分表
$uid = 10;
$model = UserContact::model($uid); // 覆盖了原有参数,改为传人分库分表所依据的值
$data = $model->findAll();

$data = $model->dbConnection->createCommand()
 ->select("*")
 ->from($model->tableName())
 ->where(&#39;uid>1&#39;)
 ->limit(10)
 ->queryAll();

$db = Yii::app()->dbConnectionManager->sharded($uid); // 调用sharded方法 设置库和表
$data = $db->createCommand()
 ->select("*")
 ->from(&#39;user_contact_&#39;.$db->shardedTableKey)
 ->where(&#39;uid>1&#39;)
 ->limit(10)
 ->queryAll();
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn