Yii 关于AR分表

Yii 中AR没有提供分表的机制,在实际应用中,需要进行动态分表记录一些日志,所以得想办法,But,又不想从新造轮子脱离YII的机制,经过几个小时的努力,终于搞掂,直接贴代码:(代码是根据Yii 的shell自动生成的t_user的改造而来,所以你会看到几个t_user字符串,而实际上,没用到这些)


<?php

/**
 * This is the model class for table "t_user".
 *
 * The followings are the available columns in table 't_user':
 * @property string $userid
 * @property string $id
 * @property string $withdraw
 * @property string $amount
 * @property string $ctime
 */
class CollectRecord extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()//主要是对tableName进行改进
	{
        $date = time();
        $tablename = $this->getTableName($date);
		return $tablename;
	}

    private function getTableName($date){
        $time = intval($date) + 28800;//为何+28800自己琢磨下就知道了
        $tableend = floor($time/604800);//每周进行一次表的维护新建
        $tableName = __CLASS__.'_'.$tableend;
        $sql='SHOW FULL COLUMNS FROM '.$tableName;
        try
        {
            $tableExist=!!($this->getDbConnection()->createCommand($sql)->queryAll());
        }
        catch(Exception $e)
        {
            $tableExist= false;
        }

        if(!$tableExist){
            try {
                $db = $this->getDbConnection();
                $db->createCommand()->createTable($tableName, array(
                    'id' => 'pk',
                    'withdraw' => 'tinyint(1)',//true is Deposits false is withdrawals;
                    'amount' => 'decimal(20,4)',
                    'userid' => 'int(11)',
                    'ctime' => 'int(11)',
                ));
            }catch (\yii\db\Exception $exc){
                return false;
            }
        }
        return $tableName;
    }
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('withdraw,amount,cuserid', 'required'),
			array('cuserid,ctime', 'numerical', 'integerOnly'=>true),
			array('userid, ctime', 'length', 'max'=>11),
			array('amount', 'length', 'max'=>20),
			array('withdraw', 'length', 'max'=>4),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('userid, amount, withdraw, ctime,id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
            'id' => 'Id',
            'withdraw' => 'Withdraw Or Deposit',
            'amount' => 'Amount',
            'userid' => 'User',
            'ctime' => 'Create Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('userid',$this->userid,true);

		$criteria->compare('amount',$this->amount,true);

		$criteria->compare('id',$this->id,true);

		$criteria->compare('withdraw',$this->withdraw,true);

		$criteria->compare('ctime',$this->ctime,true);

		return new CActiveDataProvider($this->tableName(), array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * @return t_user the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}


验证通过,使用上也没出什么问题,如果有什么没注意到的,还请赐教~

展开阅读全文

没有更多推荐了,返回首页