Skip to content

Commit 08ac5b3

Browse files
committed
Check connection logging mode before logging the query
1 parent 9ac52d2 commit 08ac5b3

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

src/Jenssegers/Mongodb/Collection.php

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class Collection {
2525
public function __construct(Connection $connection, MongoCollection $collection)
2626
{
2727
$this->connection = $connection;
28-
2928
$this->collection = $collection;
3029
}
3130

@@ -38,34 +37,36 @@ public function __construct(Connection $connection, MongoCollection $collection)
3837
*/
3938
public function __call($method, $parameters)
4039
{
41-
$query = array();
40+
$start = microtime(true);
4241

43-
// Build the query string.
44-
foreach ($parameters as $parameter)
45-
{
46-
try
47-
{
48-
$query[] = json_encode($parameter);
49-
}
50-
catch (Exception $e)
51-
{
52-
$query[] = '{...}';
53-
}
54-
}
42+
$result = call_user_func_array([$this->collection, $method], $parameters);
5543

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

58-
$result = call_user_func_array(array($this->collection, $method), $parameters);
51+
$query = [];
5952

60-
// Once we have run the query we will calculate the time that it took to run and
61-
// then log the query, bindings, and execution time so we will report them on
62-
// the event that the developer needs them. We'll log time in milliseconds.
63-
$time = $this->connection->getElapsedTime($start);
53+
// Convert the query paramters to a json string.
54+
foreach ($parameters as $parameter)
55+
{
56+
try
57+
{
58+
$query[] = json_encode($parameter);
59+
}
60+
catch (Exception $e)
61+
{
62+
$query[] = '{...}';
63+
}
64+
}
6465

65-
// Convert the query to a readable string.
66-
$queryString = $this->collection->getName() . '.' . $method . '(' . join(',', $query) . ')';
66+
$queryString = $this->collection->getName() . '.' . $method . '(' . join(',', $query) . ')';
6767

68-
$this->connection->logQuery($queryString, array(), $time);
68+
$this->connection->logQuery($queryString, [], $time);
69+
}
6970

7071
return $result;
7172
}

tests/QueryTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,12 @@ public function testPaginate()
305305
$results = User::paginate(2);
306306
$this->assertEquals(2, $results->count());
307307
$this->assertNotNull($results->first()->title);
308+
$this->assertEquals(9, $results->total());
308309

309310
$results = User::paginate(2, array('name', 'age'));
310311
$this->assertEquals(2, $results->count());
311312
$this->assertNull($results->first()->title);
313+
$this->assertEquals(9, $results->total());
312314
}
313315

314316
/*

0 commit comments

Comments
 (0)