Skip to content

Commit ce84991

Browse files
committed
bug #49079 [DoctrineBridge] fix issue with missing stopwatch events (dmaicher)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [DoctrineBridge] fix issue with missing stopwatch events | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix doctrine/DoctrineBundle#1622 | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> <!-- Replace this notice by a short README for your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> This fixes doctrine/DoctrineBundle#1622 We were not triggering the stopwatch events/sections properly when using prepared statements. Now it works: ![image](https://user-images.githubusercontent.com/921145/214097708-2986e3c9-262e-4b7e-903e-9b7e0dda208e.png) ![image](https://user-images.githubusercontent.com/921145/214097936-67948cb3-63ad-4eff-b53e-f8ff9e12a7f0.png) Commits ------- 52f4f7b [DoctrineBridge] fix issue with missing stopwatch events
2 parents 596dcc3 + 52f4f7b commit ce84991

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/Symfony/Bridge/Doctrine/Middleware/Debug/Connection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public function prepare(string $sql): DriverStatement
4444
parent::prepare($sql),
4545
$this->debugDataHolder,
4646
$this->connectionName,
47-
$sql
47+
$sql,
48+
$this->stopwatch,
4849
);
4950
}
5051

src/Symfony/Bridge/Doctrine/Middleware/Debug/Statement.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Doctrine\DBAL\Driver\Result as ResultInterface;
1616
use Doctrine\DBAL\Driver\Statement as StatementInterface;
1717
use Doctrine\DBAL\ParameterType;
18+
use Symfony\Component\Stopwatch\Stopwatch;
1819

1920
/**
2021
* @author Laurent VOULLEMIER <laurent.voullemier@gmail.com>
@@ -26,14 +27,16 @@ final class Statement extends AbstractStatementMiddleware
2627
private $debugDataHolder;
2728
private $connectionName;
2829
private $query;
30+
private $stopwatch;
2931

30-
public function __construct(StatementInterface $statement, DebugDataHolder $debugDataHolder, string $connectionName, string $sql)
32+
public function __construct(StatementInterface $statement, DebugDataHolder $debugDataHolder, string $connectionName, string $sql, Stopwatch $stopwatch = null)
3133
{
3234
parent::__construct($statement);
3335

3436
$this->debugDataHolder = $debugDataHolder;
3537
$this->connectionName = $connectionName;
3638
$this->query = new Query($sql);
39+
$this->stopwatch = $stopwatch;
3740
}
3841

3942
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool
@@ -59,12 +62,20 @@ public function execute($params = null): ResultInterface
5962
// clone to prevent variables by reference to change
6063
$this->debugDataHolder->addQuery($this->connectionName, $query = clone $this->query);
6164

65+
if ($this->stopwatch) {
66+
$this->stopwatch->start('doctrine', 'doctrine');
67+
}
68+
6269
$query->start();
6370

6471
try {
6572
$result = parent::execute($params);
6673
} finally {
6774
$query->stop();
75+
76+
if ($this->stopwatch) {
77+
$this->stopwatch->stop('doctrine');
78+
}
6879
}
6980

7081
return $result;

src/Symfony/Bridge/Doctrine/Tests/Middleware/Debug/MiddlewareTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,6 @@ public function testWithParamBound(callable $executeMethod)
161161
$executeMethod($stmt);
162162

163163
// Debug data should not be affected by these changes
164-
$product = 'product2';
165-
$price = 13.5;
166-
$stock = 4;
167-
168164
$debug = $this->debugDataHolder->getData()['default'] ?? [];
169165
$this->assertCount(2, $debug);
170166
$this->assertSame('INSERT INTO products(name, price, stock) VALUES (?, ?, ?)', $debug[1]['sql']);
@@ -242,6 +238,14 @@ static function (Connection $conn): bool {
242238
return $conn->rollBack();
243239
},
244240
],
241+
'prepared statement' => [
242+
static function (Connection $conn, string $sql): Result {
243+
return $conn->prepare($sql)->executeQuery();
244+
},
245+
static function (Connection $conn): bool {
246+
return $conn->commit();
247+
},
248+
],
245249
];
246250
}
247251

0 commit comments

Comments
 (0)