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();
}
}