来自docs
如果您需要在建立连接后立即执行SQL查询(例如,设置时区或字符集),则可以在[[yii\db\Connection :: EVENT_AFTER_OPEN]]事件处理程序中执行此操作.
return [
// ...
'components' => [
// ...
'db' => [
'class' => 'yii\db\Connection',
// ...
'on afterOpen' => function($event) {
// $event->sender refers to the DB connection
$event->sender->createCommand("SET SQL_BIG_SELECTS = 1")->execute();
}
],
],
// ...
];
Run Code Online (Sandbox Code Playgroud)
或者在查询之前运行一次SQL查询:
$connection = \Yii::$app->getDb();
$res = $connection->createCommand("SET SQL_BIG_SELECTS = 1")->execute();
Run Code Online (Sandbox Code Playgroud)