DamienHarper/auditor-bundle

Unsupported operand types: string - int (Doctrine)

knallcharge opened this issue · 3 comments

Q A
auditor-bundle version 5.2 (also 5.x-dev)
PHP version 8.2.10
Database MySQL vs MSSQL

Summary

I have the auditor-bundle installed and configured for one Entity only. On my dev-system (Windows, XAMPP, MySQL) everything works as expected, but when I deploy the code to our stage-server (which, unfortunately, is a Windows-Server and the database a Microsoft SQL Server), I get an error as soon as changes to a monitored entity is written by auditor.

Current behavior

Any change to a monitored entity (which should be tracked by auditor) results in this message:
Uncaught PHP Exception TypeError: "Unsupported operand types: string - int".
The error occurs here: vendor\doctrine\dbal\src\Driver\SQLSrv\Statement.php line 210
I'm aware that the issue occurs in Doctrine rather than in the bundle, but as everything else works with MSSQL, I thought that maybe the connection between auditor and Doctrine maybe the problem ... ?
(The error suggests that two incompatible types are trying to be concatenated without proper type casting? just guessing ...)

The issue disappears when I set "enabled" to "false" in dh_auditor.yaml, as expected.
I tried it with different entities, as the first one where the error occurred is the largest in the project, but the error persists even with small entities that just have 3 or even 2 ("id" and "name") fields.

How to reproduce

Use Auditor-Bundle with MSSQL.

Expected behavior

Auditor should work with MySQL as well as MSSQL.

Here's a stacktrace of the error:

TypeError:
Unsupported operand types: string - int

  at C:\(...)\vendor\doctrine\dbal\src\Driver\SQLSrv\Statement.php:210
  at Doctrine\DBAL\Driver\SQLSrv\Statement->prepare()
     (C:\(...)\vendor\doctrine\dbal\src\Driver\SQLSrv\Statement.php:162)
  at Doctrine\DBAL\Driver\SQLSrv\Statement->execute()
     (C:\(...)\vendor\doctrine\dbal\src\Driver\Middleware\AbstractStatementMiddleware.php:69)
  at Doctrine\DBAL\Driver\Middleware\AbstractStatementMiddleware->execute()
     (C:\(...)\vendor\doctrine\dbal\src\Logging\Statement.php:98)
  at Doctrine\DBAL\Logging\Statement->execute()
     (C:\(...)\vendor\doctrine\dbal\src\Driver\Middleware\AbstractStatementMiddleware.php:69)
  at Doctrine\DBAL\Driver\Middleware\AbstractStatementMiddleware->execute()
     (C:\(...)\vendor\symfony\doctrine-bridge\Middleware\Debug\Statement.php:68)
  at Symfony\Bridge\Doctrine\Middleware\Debug\Statement->execute()
     (C:\(...)\vendor\doctrine\dbal\src\Statement.php:190)
  at Doctrine\DBAL\Statement->execute()
     (C:\(...)\vendor\doctrine\dbal\src\Statement.php:249)
  at Doctrine\DBAL\Statement->executeStatement()
     (C:\(...)\vendor\damienharper\auditor\src\Provider\Doctrine\Persistence\Helper\DoctrineHelper.php:84)
  at DH\Auditor\Provider\Doctrine\Persistence\Helper\DoctrineHelper::executeStatement()
     (C:\(...)\vendor\damienharper\auditor\src\Provider\Doctrine\DoctrineProvider.php:136)
  at DH\Auditor\Provider\Doctrine\DoctrineProvider->persist()
     (C:\(...)\vendor\damienharper\auditor\src\EventSubscriber\AuditEventSubscriber.php:38)
  at DH\Auditor\EventSubscriber\AuditEventSubscriber->onAuditEvent()
     (C:\(...)\vendor\symfony\event-dispatcher\EventDispatcher.php:220)
  at Symfony\Component\EventDispatcher\EventDispatcher->callListeners()
     (C:\(...)\vendor\symfony\event-dispatcher\EventDispatcher.php:56)
  at Symfony\Component\EventDispatcher\EventDispatcher->dispatch()
     (C:\(...)\vendor\damienharper\auditor\src\Provider\Doctrine\Auditing\Transaction\TransactionProcessor.php:49)
  at DH\Auditor\Provider\Doctrine\Auditing\Transaction\TransactionProcessor->notify()
     (C:\(...)\vendor\damienharper\auditor\src\Provider\Doctrine\Auditing\Transaction\TransactionProcessor.php:233)
  at DH\Auditor\Provider\Doctrine\Auditing\Transaction\TransactionProcessor->audit()
     (C:\(...)\vendor\damienharper\auditor\src\Provider\Doctrine\Auditing\Transaction\TransactionProcessor.php:84)
  at DH\Auditor\Provider\Doctrine\Auditing\Transaction\TransactionProcessor->update()
     (C:\(...)\vendor\damienharper\auditor\src\Provider\Doctrine\Auditing\Transaction\TransactionProcessor.php:150)
  at DH\Auditor\Provider\Doctrine\Auditing\Transaction\TransactionProcessor->processUpdates()
     (C:\(...)\vendor\damienharper\auditor\src\Provider\Doctrine\Auditing\Transaction\TransactionProcessor.php:40)
  at DH\Auditor\Provider\Doctrine\Auditing\Transaction\TransactionProcessor->process()
     (C:\(...)\vendor\damienharper\auditor\src\Provider\Doctrine\Auditing\Transaction\TransactionManager.php:32)
  at DH\Auditor\Provider\Doctrine\Auditing\Transaction\TransactionManager->process()
     (C:\(...)\vendor\damienharper\auditor\src\Provider\Doctrine\Auditing\Event\DoctrineSubscriber.php:45)
  at DH\Auditor\Provider\Doctrine\Auditing\Event\DoctrineSubscriber->DH\Auditor\Provider\Doctrine\Auditing\Event\{closure}()
     (C:\(...)\vendor\damienharper\auditor\src\Provider\Doctrine\Auditing\Logger\Middleware\DHConnection.php:27)
  at DH\Auditor\Provider\Doctrine\Auditing\Logger\Middleware\DHConnection->commit()
     (C:\(...)\vendor\doctrine\dbal\src\Connection.php:1464)
  at Doctrine\DBAL\Connection->doCommit()
     (C:\(...)\vendor\doctrine\dbal\src\Connection.php:1422)
  at Doctrine\DBAL\Connection->commit()
     (C:\(...)\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php:472)
  at Doctrine\ORM\UnitOfWork->commit()
     (C:\(...)\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php:403)
  at Doctrine\ORM\EntityManager->flush()
     (C:\(...)\src\Repository\EntityRepository.php:121)
  at App\Repository\EntityRepository->save()
     (C:\(...)\src\Controller\IndexController.php:234)
  at App\Controller\IndexController->index()
     (C:\(...)\vendor\symfony\http-kernel\HttpKernel.php:182)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw()
     (C:\(...)\vendor\symfony\http-kernel\HttpKernel.php:76)
  at Symfony\Component\HttpKernel\HttpKernel->handle()
     (C:\(...)\vendor\symfony\http-kernel\Kernel.php:197)
  at Symfony\Component\HttpKernel\Kernel->handle()
     (C:\(...)\vendor\symfony\runtime\Runner\Symfony\HttpKernelRunner.php:35)
  at Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner->run()
     (C:\(...)\vendor\autoload_runtime.php:29)
  at require_once('C:\\(...)\\vendor\\autoload_runtime.php')
     (C:\(...)\public\index.php:5)

I did some more research on this an found the cause and the solution to the problem:
The issue is that MSSQL can't handle named parameters the way MySQL does in Doctrine. So preparing the statement in src/Provider/Doctrine/DoctrineProvider.php of auditor will mess up the query for MSSQL, resulting in the mentioned problems.

This is the query from the statement:
INSERT INTO entityname_audit (type, object_id, discriminator, transaction_hash, diffs, blame_id, blame_user, blame_user_fqdn, blame_user_firewall, ip, created_at) VALUES (:type, :object_id, :discriminator, :transaction_hash, :diffs, :blame_id, :blame_user, :blame_user_fqdn, :blame_user_firewall, :ip, :created_at)

And this is the one that'll work for MSSQL (while still working for MySQL!):
INSERT INTO entityname_audit (type, object_id, discriminator, transaction_hash, diffs, blame_id, blame_user, blame_user_fqdn, blame_user_firewall, ip, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Of course the binding of the parameters has to be changed to integer-keys to make this work, but this can be done with some minor changes.
I have created a pull request in auditor that you may want to check out (DamienHarper/auditor#182).

Fixed now in auditor master branch.
Thanks @knallcharge for the contrib