首页 > 活着 > Yii 读写分离 分表分库

Yii 读写分离 分表分库

实现一主一从,一主多从,多主多从的读写分离 。支持DAO,AR,其中Query builder只完成部分,需完善

调用

Java代码
1.//AR
2.class User extends ActiveRecord
3.{
4. public $useDbName= ‘passport';
5. //$this->dbWrite();
6.}
7.//$connection = $model->getDbConnection();
8.//DAO
9.$connection = Yii::app()->db->setDbName(‘passport’);
10.$count = $connection->createCommand(‘SELECT COUNT(*) FROM film’)->queryScalar();
11.$sql = ‘SELECT film_id,title FROM film';
12.$dataProvider=new CSqlDataProvider($sql,array(
13. ‘db’=> $connection,
14. ‘totalItemCount’=>$count,
15.));
16.Yii::app()->db->getLastInsertID();
17.
18.//Query Builder
19.$user = Yii::app()->db->setDbName(‘passport’)->createCommand()
20. ->select(‘id, username, profile’)
21. ->from(‘tbl_user u’)
22. ->join(‘tbl_profile p’, ‘u.id=p.user_id’)
23. ->where(‘id=:id’, array(‘:id’=>$id))
24. ->queryRow();
//AR
class User extends ActiveRecord
{
public $useDbName= ‘passport';
//$this->dbWrite();
}
//$connection = $model->getDbConnection();
//DAO
$connection = Yii::app()->db->setDbName(‘passport’);
$count = $connection->createCommand(‘SELECT COUNT(*) FROM film’)->queryScalar();
$sql = ‘SELECT film_id,title FROM film';
$dataProvider=new CSqlDataProvider($sql,array(
‘db’=> $connection,
‘totalItemCount’=>$count,
));
Yii::app()->db->getLastInsertID();

//Query Builder
$user = Yii::app()->db->setDbName(‘passport’)->createCommand()
->select(‘id, username, profile’)
->from(‘tbl_user u’)
->join(‘tbl_profile p’, ‘u.id=p.user_id’)
->where(‘id=:id’, array(‘:id’=>$id))
->queryRow(); 查看当前使用的主从库配置

Java代码
1.Yii::app()->db->setDbName(‘passport’);
2.//print_r(Yii::app()->getComponent(‘passport’));
Yii::app()->db->setDbName(‘passport’);
//print_r(Yii::app()->getComponent(‘passport’)); 分表AR

Java代码
1.public function primaryKey()
2.{
3. return ‘id';
4.}
5.private $tableName = “servicesmsremindrecord”;
6.public function updateMeta($code)
7.{
8. $this->tableName = “service_sms_remind_record_”.str_pad(($code%100), 2, ‘0’, STR_PAD_LEFT);
9. $this->refreshMetaData();
10. return $this;
11.}
12.//TestUserDayHealth::model($code)->with(‘user’)->findAll(); 循环放数组
public function primaryKey()
{
return ‘id';
}
private $tableName = “servicesmsremindrecord”;
public function updateMeta($code)
{
$this->tableName = “service_sms_remind_record_”.str_pad(($code%100), 2, ‘0’, STR_PAD_LEFT);
$this->refreshMetaData();
return $this;
}
//TestUserDayHealth::model($code)->with(‘user’)->findAll(); 循环放数组

读写分离.rar (11.2 KB)
下载次数: 9

实现一主一从,一主多从,多主多从的读写分离 。支持DAO,AR,其中Query builder只完成部分,需完 […]

  1. 还没有评论
评论提交中, 请稍候...

留言

(Spamcheck Enabled)

Trackbacks & Pingbacks ( 0 )
  1. 还没有 trackbacks