Skip to content

Commit 3df57e2

Browse files
committed
Compatible with pecl-mongodb
1 parent a2f4eca commit 3df57e2

File tree

5 files changed

+78
-105
lines changed

5 files changed

+78
-105
lines changed

src/Jenssegers/Mongodb/Collection.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php namespace Jenssegers\Mongodb;
22

33
use Exception;
4-
use MongoCollection;
4+
use MongoDB\Collection as MongoCollection;
55

66
class Collection {
77

@@ -38,7 +38,6 @@ public function __construct(Connection $connection, MongoCollection $collection)
3838
public function __call($method, $parameters)
3939
{
4040
$start = microtime(true);
41-
4241
$result = call_user_func_array([$this->collection, $method], $parameters);
4342

4443
if ($this->connection->logging())

src/Jenssegers/Mongodb/Connection.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
<?php namespace Jenssegers\Mongodb;
2-
3-
use MongoClient;
4-
1+
<?php
2+
namespace Jenssegers\Mongodb;
3+
use MongoDB;
54
class Connection extends \Illuminate\Database\Connection {
65

76
/**
@@ -37,7 +36,7 @@ public function __construct(array $config)
3736
$this->connection = $this->createConnection($dsn, $config, $options);
3837

3938
// Select database
40-
$this->db = $this->connection->{$config['database']};
39+
$this->db = $this->connection->selectDatabase($config['database']);
4140

4241
$this->useDefaultPostProcessor();
4342
}
@@ -148,16 +147,15 @@ protected function createConnection($dsn, array $config, array $options)
148147
{
149148
$driverOptions = $config['driver_options'];
150149
}
151-
152-
return new MongoClient($dsn, $options, $driverOptions);
150+
return new MongoDB\Client($dsn, $options, $driverOptions);
153151
}
154152

155153
/**
156154
* Disconnect from the underlying MongoClient connection.
157155
*/
158156
public function disconnect()
159157
{
160-
$this->connection->close();
158+
unset($this->connection);
161159
}
162160

163161
/**
@@ -193,7 +191,14 @@ protected function getDsn(array $config)
193191

194192
// The database name needs to be in the connection string, otherwise it will
195193
// authenticate to the admin database, which may result in permission errors.
196-
return "mongodb://" . implode(',', $hosts) . "/{$database}";
194+
$auth = '';
195+
if(isset($username) && $username)
196+
$auth .= $username;
197+
if(isset($password) && $password)
198+
$auth .= ':'.$password;
199+
if($auth)
200+
$auth .= '@';
201+
return "mongodb://" . $auth . implode(',', $hosts) . "/{$database}";
197202
}
198203

199204
/**

src/Jenssegers/Mongodb/Model.php

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
use Jenssegers\Mongodb\Relations\EmbedsMany;
1111
use Jenssegers\Mongodb\Relations\EmbedsOne;
1212
use Jenssegers\Mongodb\Relations\EmbedsOneOrMany;
13-
use MongoDate;
14-
use MongoId;
1513
use ReflectionMethod;
16-
14+
use MongoDB;
1715
abstract class Model extends BaseModel {
1816

1917
use HybridRelations;
@@ -54,8 +52,8 @@ public function getIdAttribute($value)
5452
$value = $this->attributes['_id'];
5553
}
5654

57-
// Convert MongoId's to string.
58-
if ($value instanceof MongoId)
55+
// Convert MongoDB\BSON\ObjectID's to string.
56+
if ($value instanceof MongoDB\BSON\ObjectID)
5957
{
6058
return (string) $value;
6159
}
@@ -150,15 +148,15 @@ protected function embedsOne($related, $localKey = null, $foreignKey = null, $re
150148
}
151149

152150
/**
153-
* Convert a DateTime to a storable MongoDate object.
151+
* Convert a DateTime to a storable MongoDB\BSON\UTCDateTime object.
154152
*
155153
* @param DateTime|int $value
156-
* @return MongoDate
154+
* @return MongoDB\BSON\UTCDateTime
157155
*/
158156
public function fromDateTime($value)
159157
{
160-
// If the value is already a MongoDate instance, we don't need to parse it.
161-
if ($value instanceof MongoDate)
158+
// If the value is already a MongoDB\BSON\UTCDateTime instance, we don't need to parse it.
159+
if ($value instanceof MongoDB\BSON\UTCDateTime)
162160
{
163161
return $value;
164162
}
@@ -169,7 +167,8 @@ public function fromDateTime($value)
169167
$value = parent::asDateTime($value);
170168
}
171169

172-
return new MongoDate($value->getTimestamp());
170+
171+
return new MongoDB\BSON\UTCDateTime($value->getTimestamp());
173172
}
174173

175174
/**
@@ -180,8 +179,8 @@ public function fromDateTime($value)
180179
*/
181180
protected function asDateTime($value)
182181
{
183-
// Convert MongoDate instances.
184-
if ($value instanceof MongoDate)
182+
// Convert MongoDB\BSON\UTCDateTime instances.
183+
if ($value instanceof MongoDB\BSON\UTCDateTime)
185184
{
186185
return Carbon::createFromTimestamp($value->sec);
187186
}
@@ -202,11 +201,11 @@ protected function getDateFormat()
202201
/**
203202
* Get a fresh timestamp for the model.
204203
*
205-
* @return MongoDate
204+
* @return MongoDB\BSON\UTCDateTime
206205
*/
207206
public function freshTimestamp()
208-
{
209-
return new MongoDate;
207+
{
208+
return round(microtime(true) * 1000);
210209
}
211210

212211
/**
@@ -298,7 +297,7 @@ protected function getAttributeFromArray($key)
298297
*/
299298
public function setAttribute($key, $value)
300299
{
301-
// Convert _id to MongoId.
300+
// Convert _id to MongoDB\BSON\ObjectID.
302301
if ($key == '_id' and is_string($value))
303302
{
304303
$builder = $this->newBaseQueryBuilder();
@@ -337,7 +336,7 @@ public function attributesToArray()
337336
// nicely when your models are converted to JSON.
338337
foreach ($attributes as $key => &$value)
339338
{
340-
if ($value instanceof MongoId)
339+
if ($value instanceof MongoDB\BSON\ObjectID)
341340
{
342341
$value = (string) $value;
343342
}
@@ -355,29 +354,6 @@ public function attributesToArray()
355354
return $attributes;
356355
}
357356

358-
/**
359-
* Determine if the new and old values for a given key are numerically equivalent.
360-
*
361-
* @param string $key
362-
* @return bool
363-
*/
364-
protected function originalIsNumericallyEquivalent($key)
365-
{
366-
$current = $this->attributes[$key];
367-
368-
$original = $this->original[$key];
369-
370-
// Date comparison.
371-
if (in_array($key, $this->getDates()))
372-
{
373-
$current = $current instanceof MongoDate ? $this->asDateTime($current) : $current;
374-
$original = $original instanceof MongoDate ? $this->asDateTime($original) : $original;
375-
return $current == $original;
376-
}
377-
378-
return parent::originalIsNumericallyEquivalent($key);
379-
}
380-
381357
/**
382358
* Remove one or more fields.
383359
*

0 commit comments

Comments
 (0)