博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Yii 关于AR分表
阅读量:5974 次
发布时间:2019-06-19

本文共 3418 字,大约阅读时间需要 11 分钟。

hot3.png

<?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);

}

}

转载于:https://my.oschina.net/yonghan/blog/533061

你可能感兴趣的文章
python基础之迭代器和生成器
查看>>
【Latex】插入空格的几种方式
查看>>
linux中的crondtab定时计划任务
查看>>
sendmail服务器加密与身份认证
查看>>
恢复数据块坏块
查看>>
The 59 Top Influencers in IT Security
查看>>
我的友情链接
查看>>
cacti登陆密码忘记解决办法
查看>>
我的友情链接
查看>>
java.sql.SQLTimeoutException: Timeout after 30001ms of waiting for a connection.
查看>>
第六章 核心API
查看>>
八、配置webmail--EXTMAIL
查看>>
Linux特殊权限
查看>>
iphone使用开源代码Asyncsocket进行socket编程 之二
查看>>
基于 Linux 3.X 内核的专用操作系统秒开缓存系统
查看>>
【MongoDB for Java】Java操作MongoDB
查看>>
Python 模块笔记
查看>>
System Center Service Manager中Runbook的状态
查看>>
windows平台下gvim中文支持
查看>>
一种windows***方法
查看>>