Skip to content

Commit 08f4995

Browse files
authored
Merge pull request mongodb#2069 from StevePorter92/query-event-fix
always call connection logQuery function to broadcast QueryExecuted event
2 parents 0bf3f03 + baa7332 commit 08f4995

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

src/Jenssegers/Mongodb/Collection.php

+20-22
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,32 @@ public function __call($method, $parameters)
4141
$start = microtime(true);
4242
$result = call_user_func_array([$this->collection, $method], $parameters);
4343

44-
if ($this->connection->logging()) {
45-
// Once we have run the query we will calculate the time that it took to run and
46-
// then log the query, bindings, and execution time so we will report them on
47-
// the event that the developer needs them. We'll log time in milliseconds.
48-
$time = $this->connection->getElapsedTime($start);
44+
// Once we have run the query we will calculate the time that it took to run and
45+
// then log the query, bindings, and execution time so we will report them on
46+
// the event that the developer needs them. We'll log time in milliseconds.
47+
$time = $this->connection->getElapsedTime($start);
4948

50-
$query = [];
49+
$query = [];
5150

52-
// Convert the query parameters to a json string.
53-
array_walk_recursive($parameters, function (&$item, $key) {
54-
if ($item instanceof ObjectID) {
55-
$item = (string) $item;
56-
}
57-
});
51+
// Convert the query parameters to a json string.
52+
array_walk_recursive($parameters, function (&$item, $key) {
53+
if ($item instanceof ObjectID) {
54+
$item = (string) $item;
55+
}
56+
});
5857

59-
// Convert the query parameters to a json string.
60-
foreach ($parameters as $parameter) {
61-
try {
62-
$query[] = json_encode($parameter);
63-
} catch (Exception $e) {
64-
$query[] = '{...}';
65-
}
58+
// Convert the query parameters to a json string.
59+
foreach ($parameters as $parameter) {
60+
try {
61+
$query[] = json_encode($parameter);
62+
} catch (Exception $e) {
63+
$query[] = '{...}';
6664
}
65+
}
6766

68-
$queryString = $this->collection->getCollectionName() . '.' . $method . '(' . implode(',', $query) . ')';
67+
$queryString = $this->collection->getCollectionName() . '.' . $method . '(' . implode(',', $query) . ')';
6968

70-
$this->connection->logQuery($queryString, [], $time);
71-
}
69+
$this->connection->logQuery($queryString, [], $time);
7270

7371
return $result;
7472
}

tests/CollectionTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public function testExecuteMethodCall()
2323
$mongoCollection->expects($this->once())->method('getCollectionName')->willReturn('name-collection');
2424

2525
$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
26-
$connection->expects($this->once())->method('logging')->willReturn(true);
2726
$connection->expects($this->once())->method('getElapsedTime')->willReturn($time);
2827
$connection->expects($this->once())->method('logQuery')->with($queryString, [], $time);
2928

0 commit comments

Comments
 (0)