Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

不要直接修改$query->bindings #10

Open
qzsiniong opened this issue Mar 8, 2024 · 0 comments
Open

不要直接修改$query->bindings #10

qzsiniong opened this issue Mar 8, 2024 · 0 comments

Comments

@qzsiniong
Copy link

qzsiniong commented Mar 8, 2024

https://github.com/webman-php/log/blob/893917ee574de9bf008eaf24089c61ec843437c7/src/Middleware.php#L194C11-L194C47

$query->bindings[$i] = "'$binding'";

不要直接修改$query->bindings.

改为如下会好点:

$dispatcher->listen(QueryExecuted::class, function (QueryExecuted $query) {
  $sql = trim($query->sql);
  if (strtolower($sql) === 'select 1') {
      return;
  }
  $sql = str_replace("?", "%s", $sql);
  $bindings = $query->bindings; // <<<<<<<<<<<<<<<<<<<<<<<<<<
  foreach ($bindings as $i => $binding) {
      if ($binding instanceof \DateTime) {
          $bindings[$i] = $binding->format("'Y-m-d H:i:s'");
      } else {
          if (is_string($binding)) {
              $bindings[$i] = "'$binding'";
          }
      }
  }
  $log = $sql;
  try {
      $log = vsprintf($sql, $bindings);
  } catch (\Throwable $e) {}
  Context::get()->webmanLogs = (Context::get()->webmanLogs ?? '') . "[SQL]\t[connection:{$query->connectionName}] $log [{$query->time} ms]" . PHP_EOL;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant