Commit 08ac5b34 authored by Jens Segers's avatar Jens Segers

Check connection logging mode before logging the query

parent 9ac52d23
...@@ -25,7 +25,6 @@ class Collection { ...@@ -25,7 +25,6 @@ class Collection {
public function __construct(Connection $connection, MongoCollection $collection) public function __construct(Connection $connection, MongoCollection $collection)
{ {
$this->connection = $connection; $this->connection = $connection;
$this->collection = $collection; $this->collection = $collection;
} }
...@@ -38,34 +37,36 @@ class Collection { ...@@ -38,34 +37,36 @@ class Collection {
*/ */
public function __call($method, $parameters) public function __call($method, $parameters)
{ {
$query = array(); $start = microtime(true);
// Build the query string. $result = call_user_func_array([$this->collection, $method], $parameters);
foreach ($parameters as $parameter)
{
try
{
$query[] = json_encode($parameter);
}
catch (Exception $e)
{
$query[] = '{...}';
}
}
$start = microtime(true); if ($this->connection->logging())
{
// Once we have run the query we will calculate the time that it took to run and
// then log the query, bindings, and execution time so we will report them on
// the event that the developer needs them. We'll log time in milliseconds.
$time = $this->connection->getElapsedTime($start);
$result = call_user_func_array(array($this->collection, $method), $parameters); $query = [];
// Once we have run the query we will calculate the time that it took to run and // Convert the query paramters to a json string.
// then log the query, bindings, and execution time so we will report them on foreach ($parameters as $parameter)
// the event that the developer needs them. We'll log time in milliseconds. {
$time = $this->connection->getElapsedTime($start); try
{
$query[] = json_encode($parameter);
}
catch (Exception $e)
{
$query[] = '{...}';
}
}
// Convert the query to a readable string. $queryString = $this->collection->getName() . '.' . $method . '(' . join(',', $query) . ')';
$queryString = $this->collection->getName() . '.' . $method . '(' . join(',', $query) . ')';
$this->connection->logQuery($queryString, array(), $time); $this->connection->logQuery($queryString, [], $time);
}
return $result; return $result;
} }
......
...@@ -305,10 +305,12 @@ class QueryTest extends TestCase { ...@@ -305,10 +305,12 @@ class QueryTest extends TestCase {
$results = User::paginate(2); $results = User::paginate(2);
$this->assertEquals(2, $results->count()); $this->assertEquals(2, $results->count());
$this->assertNotNull($results->first()->title); $this->assertNotNull($results->first()->title);
$this->assertEquals(9, $results->total());
$results = User::paginate(2, array('name', 'age')); $results = User::paginate(2, array('name', 'age'));
$this->assertEquals(2, $results->count()); $this->assertEquals(2, $results->count());
$this->assertNull($results->first()->title); $this->assertNull($results->first()->title);
$this->assertEquals(9, $results->total());
} }
/* /*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment