微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

yii分表的一些分享

 public function checkTable($id = 1)
    {
        $table_name = 'te_' . $id;//你的表名
        $tableArr = Yii::$app->device->createCommand("show tables ")->queryAll();
        //判断是否存在值是否存在二维数组中
        $arr = array_column($tableArr, 'Tables_in_shop');//shop 代表当前数据库名
        if (!in_array($table_name, $arr)) {
            $this->createTable($tableName);
        }
        return $tableName;
    }
 /**
     * 创建表
     * @param type $tableName
     * @return type
     */
    private function createTable($tableName)
    {
        // 内部事务
        $transaction = Yii::$app->device->beginTransaction();
        try {
            $result = Yii::$app->device->createCommand("
                              CREATE TABLE `{$tableName}` (
                              `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
                              `name`varchar(50) DEFAULT NULL ,
                              `age` int(3) DEFAULT NULL ,
                              `sex` int(2) DEFAULT NULLCOMMENT '性别' ,
                               `ip` varchar(11) DEFAULT NULL COMMENT '上次登录Ip',
                              `createtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                              `lasttime` int(11) DEFAULT NULL COMMENT '上次登录时间时间',
                              PRIMARY KEY (`id`),
                              KEY `time` (`time`) USING BTREE
                            ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='飞行日志';
                       ")->execute();
            $transaction->commit();
        } catch (\Exception $e) {
            throw  $e;
            $transaction->rollBack();
        }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐