Skip to content

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

Open
@qzsiniong

Description

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;
});

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions