您的位置:首页 > 编程语言 > PHP开发

Yii分表插件Par(Partition Active Record)[原创]

2013-08-05 00:00 531 查看

PAR

An extension of Yii ORM to support partition tables.

特点

基于表的一个字段计算hash并分表,方便使用,初始化时指定需要hash的字段即可。

使用方法

将par.php放在components目录下,需要分表支持的model继承Par类,并定义$tablename静态变量作为表的未分表名称。代码如下:

Controller

class IndexController extends Controller
{
public function actionTest()
{
$test = new Test();
$test->name='a test name';
$test->save();
{{or}}
$test = Test::model()->findByPk(3);
var_dump($test->name);
}
}

Model

class Test extends Par
{
public static $tablename = 'test';    //未分表前的表名
public static function model($opts = null, $entry = array(), $className=__CLASS__)
{
return parent::model($opts, $entry, $className);
}
public function rules()
{
return array(
array('url, name', 'safe'),
);
}
}


源码

class Par extends CActiveRecord
{
const STRING_HASH = 1;
const DATE_HASH = 2;
protected static $tablename = 'pars';
protected $hashnum = 10;
protected $hashtype = self::DATE_HASH;
protected $_key;
/**
* Constructor.
* @param array $entry=array($key=>$value) key is the partition field name.
* @param array $opts some optitions: hash_num=>the hash num default 10.
*/
public function __construct($opts = array(), $entry = array()){
if($opts === null){
return;
}
$this->parseParams($opts, $entry);
parent::__construct();
}
public static function model($opts = null, $entry = array(), $className=__CLASS__)
{
$model = new $className($opts, $entry);
return $model;
}
public function tableName()
{
return static::$tablename;
}
protected function parseParams($opts, $entry){
if(isset($opts['hash_num'])){
$this->hashnum = $opts['hash_num'];
}
if(isset($opts['hash_type'])){
$this->hashtype = $opts['hash_type'];
}
if(!empty($entry)){
$key = key($entry);
$value = $entry[$key];
$this->_key = $value;
}
static::$tablename .= '_'.$this->getHashTablename();
}
//根据字符串hash到十进制,计算表hash值
protected function getHashTablename(){
switch($this->hashtype){
case self::STRING_HASH:
$str = base_convert(md5($this->_key),16, 10);
$key_hash = substr($str, strlen($str)-1, 1);
$table_hash = $key_hash % $this->hashnum;
break;
case self::DATE_HASH:
$table_hash = date('Y_m');

3ff0
break;
default:
break;
}
return $table_hash;
}
}

源码地址:https://github.com/fate2013/par
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Yii 分表 Par ORM