我想创建一个新表,然后使用 create 命令将值插入其中。这是我尝试过的代码
if ( $model->save() )
{
$first_val = $model->num_start - 1;
$connection = \Yii::$app()->db;
$transaction = $connection->beginTransaction();
try
{
$q = "CREATE TABLE range_{$model->id}( id INT(11) NOT NULL AUTO_INCREMENT,PRIMARY KEY (id) ); " ;
$connection->createCommand($q)->execute(); // new table created
$transaction->commit();
if ( $model->num_start > 1 )
{
$r = "INSERT INTO range_{$model->id}( id ) VALUES ({$first_val});" ;
$connection->createCommand($r)->execute(); // new value inserted
$transaction->commit();
}
}
catch (Exception $e)
{
// react on exception
$transaction->rollback();
}
return $this->redirect( [ 'index' ] );
}
当我尝试运行此代码时出现错误Call to undefined method Yii::app()
如何在 Yii2 中使用 create 命令?