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

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

PHP中文网
Release: 2016-05-25 17:00:18
Original
1432 people have browsed it

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();
Copy after login
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template