yii2的强制读某个库,看一下这个资料:https://github.com/yiisoft/yii2/issues/4799
https://github.com/yiisoft/yii2/blob/master/framework/db/Connection.php#L1054
/**
* Executes the provided callback by using the master connection.
*
* This method is provided so that you can temporarily force using the master connection to perform
* DB operations even if they are read queries. For example,
*
* ```php
* $result = $db->useMaster(function ($db) {
* return $db->createCommand('SELECT * FROM user LIMIT 1')->queryOne();
* });
* ```
*
* @param callable $callback a PHP callable to be executed by this method. Its signature is
* `function (Connection $db)`. Its return value will be returned by this method.
* @return mixed the return value of the callback
* @throws \Exception|\Throwable if there is any exception thrown from the callback
*/
public function useMaster(callable $callback)
{
if ($this->enableSlaves) {
$this->enableSlaves = false;
try {
$result = call_user_func($callback, $this);
} catch (\Exception $e) {
$this->enableSlaves = true;
throw $e;
} catch (\Throwable $e) {
$this->enableSlaves = true;
throw $e;
}
// TODO: use "finally" keyword when miminum required PHP version is >= 5.5
$this->enableSlaves = true;
} else {
$result = call_user_func($callback, $this);
}
return $result;
}