我是想判断表是否存在,然后添加表
但提示 Call to a member function createCommand() on a non-object
请问要怎么处理?
<?php
namespace frontend\controllers;
use yii\web\Controller;
use yii\db\Migration;
use yii\db\Schema;
class TableController extends Controller
{
public function actionIndex()
{
$mir = new Migration();
$sch = new \yii\db\mysql\Schema;
$tableName='shang';
$table=$db->createCommand("SHOW TABLES LIKE '".$tableName."'")->queryAll();
if($table==null) {
echo '1';
}else{
echo '2';
}
// $mir->createTable('shang', [
// 'id' => 'pk',
// 'title' => $sch::TYPE_STRING . ' NOT NULL',
// 'content' => $sch::TYPE_TEXT,
// ]);
}
}
判断表是否存在有好多种方法,如:
判断表字段是否存在:
可以用migration里面的方法: