The yii2 framework obtains sql statements and log methods

$query = InvCode::find()->where($where);
$sql = $query->createCommand()->getRawSql();


When we use YII2 to develop projects, we will check the currently executed SQL statements to troubleshoot errors, which is convenient for work, and we will record what we see, thank you seniors! Pay tribute to the seniors!

To obtain the current SQL statement, we can use getRawSql() to achieve it. The usage example is as follows:

举例:UserModel

$query = UserModel::find()->where(['status'=>1]);

echo $query->createCommand()->getRawSql();


Knowledge supplement

yii2 uses createCommand() to add, delete, modify and query

to query a single piece of data
 

$sql = "SELECT `name` FROM `table` WHERE id='7'";

$users=Yii::$app->db->createCommand($sql)->queryOne();


Query multiple data

$sql = "SELECT `name` FROM `table` WHERE name='$name'";

$users=Yii::$app->db->createCommand($sql)->queryAll();


change the data

Yii::$app->db->createCommand()->update('table', ['name' => $name], "id = {$id}")->execute();


//update The first parameter: table name The second parameter: the data to be modified The third data: modify the condition to

add data

Yii::$app->db->createCommand()->insert("table",array("name"=>'zhangsan',"age"=>'18'));

//insert first parameter: table name second parameter: data to be added

delete data

Yii::$app->db->createCommand()->delete('table', 'age = 30')->execute();


//delete first parameter: table name second parameter: delete condition

log:

Yii::info($log, __METHOD__);

Look at the info source code to find parameters:

public static function info($message, $category = 'application')
{
    static::getLogger()->log($message, Logger::LEVEL_INFO, $category);
}

Guess you like

Origin blog.csdn.net/hechenhongbo/article/details/121382768