Skip to content

Commit 21a4bdc

Browse files
committed
Work in progress
1 parent dc29f9a commit 21a4bdc

16 files changed

+162
-123
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"require-dev": {
2222
"phpunit/phpunit": "^4.0|^5.0",
23-
"orchestra/testbench": "^3.1",
23+
"orchestra/testbench": "^3.2",
2424
"mockery/mockery": "^0.9",
2525
"satooshi/php-coveralls": "^0.6"
2626
},

src/Jenssegers/Mongodb/Collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function __call($method, $parameters)
6262
}
6363
}
6464

65-
$queryString = $this->collection->getName() . '.' . $method . '(' . implode(',', $query) . ')';
65+
$queryString = $this->collection->getCollectionName() . '.' . $method . '(' . implode(',', $query) . ')';
6666

6767
$this->connection->logQuery($queryString, [], $time);
6868
}

src/Jenssegers/Mongodb/Connection.php

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
<?php
2-
namespace Jenssegers\Mongodb;
3-
use MongoDB;
1+
<?php namespace Jenssegers\Mongodb;
2+
3+
use MongoDB\Client;
4+
45
class Connection extends \Illuminate\Database\Connection {
56

67
/**
@@ -11,9 +12,9 @@ class Connection extends \Illuminate\Database\Connection {
1112
protected $db;
1213

1314
/**
14-
* The MongoClient connection handler.
15+
* The MongoDB connection handler.
1516
*
16-
* @var MongoClient
17+
* @var MongoDB
1718
*/
1819
protected $connection;
1920

@@ -29,7 +30,7 @@ public function __construct(array $config)
2930
// Build the connection string
3031
$dsn = $this->getDsn($config);
3132

32-
// You can pass options directly to the MongoClient constructor
33+
// You can pass options directly to the MongoDB constructor
3334
$options = array_get($config, 'options', []);
3435

3536
// Create the connection
@@ -109,49 +110,38 @@ public function getMongoDB()
109110
}
110111

111112
/**
112-
* return MongoClient object.
113+
* return MongoDB object.
113114
*
114-
* @return MongoClient
115+
* @return MongoDB
115116
*/
116117
public function getMongoClient()
117118
{
118119
return $this->connection;
119120
}
120121

121122
/**
122-
* Create a new MongoClient connection.
123+
* Create a new MongoDB connection.
123124
*
124125
* @param string $dsn
125126
* @param array $config
126127
* @param array $options
127-
* @return MongoClient
128+
* @return MongoDB
128129
*/
129130
protected function createConnection($dsn, array $config, array $options)
130131
{
131-
// Add credentials as options, this makes sure the connection will not fail if
132-
// the username or password contains strange characters.
133-
if ( ! empty($config['username']))
134-
{
135-
$options['username'] = $config['username'];
136-
}
137-
138-
if ( ! empty($config['password']))
139-
{
140-
$options['password'] = $config['password'];
141-
}
142-
143132
// By default driver options is an empty array.
144133
$driverOptions = [];
145134

146135
if (isset($config['driver_options']) && is_array($config['driver_options']))
147136
{
148137
$driverOptions = $config['driver_options'];
149138
}
150-
return new MongoDB\Client($dsn, $options, $driverOptions);
139+
140+
return new Client($dsn, $options, $driverOptions);
151141
}
152142

153143
/**
154-
* Disconnect from the underlying MongoClient connection.
144+
* Disconnect from the underlying MongoDB connection.
155145
*/
156146
public function disconnect()
157147
{
@@ -168,7 +158,7 @@ protected function getDsn(array $config)
168158
{
169159
// First we will create the basic DSN setup as well as the port if it is in
170160
// in the configuration options. This will give us the basic DSN we will
171-
// need to establish the MongoClient and return them back for use.
161+
// need to establish the MongoDB and return them back for use.
172162
extract($config);
173163

174164
// Check if the user passed a complete dsn to the configuration.
@@ -192,12 +182,10 @@ protected function getDsn(array $config)
192182
// The database name needs to be in the connection string, otherwise it will
193183
// authenticate to the admin database, which may result in permission errors.
194184
$auth = '';
195-
if(isset($username) && $username)
196-
$auth .= $username;
197-
if(isset($password) && $password)
198-
$auth .= ':'.$password;
199-
if($auth)
200-
$auth .= '@';
185+
if (! empty($username)) $auth .= $username;
186+
if (! empty($password)) $auth .= ':'.$password;
187+
if ($auth) $auth .= '@';
188+
201189
return "mongodb://" . $auth . implode(',', $hosts) . "/{$database}";
202190
}
203191

src/Jenssegers/Mongodb/Eloquent/Builder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
44
use Illuminate\Database\Eloquent\Relations\Relation;
5-
use MongoCursor;
5+
use MongoDB\Driver\Cursor;
66

77
class Builder extends EloquentBuilder {
88

@@ -218,7 +218,7 @@ public function raw($expression = null)
218218
$results = $this->query->raw($expression);
219219

220220
// Convert MongoCursor results to a collection of models.
221-
if ($results instanceof MongoCursor)
221+
if ($results instanceof Cursor)
222222
{
223223
$results = iterator_to_array($results, false);
224224

@@ -228,7 +228,7 @@ public function raw($expression = null)
228228
// The result is a single object.
229229
elseif (is_array($results) and array_key_exists('_id', $results))
230230
{
231-
$model = $this->model->newFromBuilder($results);
231+
$model = $this->model->newFromBuilder((array) $results);
232232

233233
$model->setConnection($this->model->getConnection());
234234

src/Jenssegers/Mongodb/Model.php

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
use Jenssegers\Mongodb\Relations\EmbedsMany;
1111
use Jenssegers\Mongodb\Relations\EmbedsOne;
1212
use Jenssegers\Mongodb\Relations\EmbedsOneOrMany;
13+
use MongoDB\BSON\ObjectID;
14+
use MongoDB\BSON\UTCDateTime;
1315
use ReflectionMethod;
14-
use MongoDB;
16+
1517
abstract class Model extends BaseModel {
1618

1719
use HybridRelations;
@@ -52,8 +54,8 @@ public function getIdAttribute($value)
5254
$value = $this->attributes['_id'];
5355
}
5456

55-
// Convert MongoDB\BSON\ObjectID's to string.
56-
if ($value instanceof MongoDB\BSON\ObjectID)
57+
// Convert ObjectID to string.
58+
if ($value instanceof ObjectID)
5759
{
5860
return (string) $value;
5961
}
@@ -148,15 +150,15 @@ protected function embedsOne($related, $localKey = null, $foreignKey = null, $re
148150
}
149151

150152
/**
151-
* Convert a DateTime to a storable MongoDB\BSON\UTCDateTime object.
153+
* Convert a DateTime to a storable UTCDateTime object.
152154
*
153155
* @param DateTime|int $value
154-
* @return MongoDB\BSON\UTCDateTime
156+
* @return UTCDateTime
155157
*/
156158
public function fromDateTime($value)
157159
{
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)
160+
// If the value is already a UTCDateTime instance, we don't need to parse it.
161+
if ($value instanceof UTCDateTime)
160162
{
161163
return $value;
162164
}
@@ -167,8 +169,7 @@ public function fromDateTime($value)
167169
$value = parent::asDateTime($value);
168170
}
169171

170-
171-
return new MongoDB\BSON\UTCDateTime($value->getTimestamp());
172+
return new UTCDateTime($value->getTimestamp() * 1000);
172173
}
173174

174175
/**
@@ -179,10 +180,10 @@ public function fromDateTime($value)
179180
*/
180181
protected function asDateTime($value)
181182
{
182-
// Convert MongoDB\BSON\UTCDateTime instances.
183-
if ($value instanceof MongoDB\BSON\UTCDateTime)
183+
// Convert UTCDateTime instances.
184+
if ($value instanceof UTCDateTime)
184185
{
185-
return Carbon::createFromTimestamp($value->sec);
186+
return Carbon::createFromTimestamp($value->toDateTime()->getTimestamp());
186187
}
187188

188189
return parent::asDateTime($value);
@@ -201,11 +202,11 @@ protected function getDateFormat()
201202
/**
202203
* Get a fresh timestamp for the model.
203204
*
204-
* @return MongoDB\BSON\UTCDateTime
205+
* @return UTCDateTime
205206
*/
206207
public function freshTimestamp()
207-
{
208-
return round(microtime(true) * 1000);
208+
{
209+
return new UTCDateTime(round(microtime(true) * 1000));
209210
}
210211

211212
/**
@@ -297,7 +298,7 @@ protected function getAttributeFromArray($key)
297298
*/
298299
public function setAttribute($key, $value)
299300
{
300-
// Convert _id to MongoDB\BSON\ObjectID.
301+
// Convert _id to ObjectID.
301302
if ($key == '_id' and is_string($value))
302303
{
303304
$builder = $this->newBaseQueryBuilder();
@@ -336,7 +337,7 @@ public function attributesToArray()
336337
// nicely when your models are converted to JSON.
337338
foreach ($attributes as $key => &$value)
338339
{
339-
if ($value instanceof MongoDB\BSON\ObjectID)
340+
if ($value instanceof ObjectID)
340341
{
341342
$value = (string) $value;
342343
}
@@ -354,6 +355,39 @@ public function attributesToArray()
354355
return $attributes;
355356
}
356357

358+
/**
359+
* Get the casts array.
360+
*
361+
* @return array
362+
*/
363+
protected function getCasts()
364+
{
365+
return $this->casts;
366+
}
367+
368+
/**
369+
* Determine if the new and old values for a given key are numerically equivalent.
370+
*
371+
* @param string $key
372+
* @return bool
373+
*/
374+
protected function originalIsNumericallyEquivalent($key)
375+
{
376+
$current = $this->attributes[$key];
377+
$original = $this->original[$key];
378+
379+
// Date comparison.
380+
if (in_array($key, $this->getDates()))
381+
{
382+
$current = $current instanceof UTCDateTime ? $this->asDateTime($current) : $current;
383+
$original = $original instanceof UTCDateTime ? $this->asDateTime($original) : $original;
384+
385+
return $current == $original;
386+
}
387+
388+
return parent::originalIsNumericallyEquivalent($key);
389+
}
390+
357391
/**
358392
* Remove one or more fields.
359393
*

0 commit comments

Comments
 (0)