Skip to content

Merge back master #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 41 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
771ee7a
DSN configuration example added to readme
pavemaksim Aug 31, 2017
809c3a7
Add L5.5 support
MCMatters Sep 13, 2017
64a2766
Update travis.yml
MCMatters Sep 13, 2017
a411bfa
Return event dispatcher to tests and add 'retrieved' handling
MCMatters Sep 14, 2017
5ecbf0b
Merge pull request #1300 from MCMatters/master
jenssegers Sep 18, 2017
b8ce7a5
Merge pull request #1283 from pavemaksim/pavemaksim-patch-config-docs
jenssegers Sep 18, 2017
9dd2717
Add test for chunkById.
DFurnes Oct 2, 2017
6261109
Fix issue using chunkById with ObjectIDs.
DFurnes Oct 2, 2017
4d750ef
fix typo in README.md
cjmling Oct 4, 2017
b9d99c6
Merge pull request #1319 from cjmling/patch-1
jenssegers Oct 4, 2017
d887e4f
Added support for a whereAll query
lindelius Oct 5, 2017
e107901
fix for returning incorrect builder in hybrid relations
pimlie Oct 11, 2017
8c17827
Merge pull request #1320 from lindelius/master
jenssegers Oct 11, 2017
86a61ab
Fix for issue #1276
lindelius Oct 11, 2017
1f2c88f
#1318 Implement dropping all tables with mongo driver
NoelDeMartin Oct 12, 2017
7fbc32c
#1318 Conform with PSR-2 styles
NoelDeMartin Oct 12, 2017
8172bfb
#1318 Conform with PSR-2 styles
NoelDeMartin Oct 13, 2017
91222dd
Fix Unable to set a custom collection for models
Oct 23, 2017
fbbf387
fix queue
jgcl Oct 30, 2017
cf15156
Merge pull request #1343 from robbytaylor/master
jenssegers Nov 3, 2017
5454ac9
Merge pull request #1317 from DoSomething/chunkById-5.5
jenssegers Nov 3, 2017
23e8423
fix ci, remove use from current namespace
pimlie Nov 7, 2017
27ebbdf
Update README.md
dzhiryakov Nov 13, 2017
a1c69ee
Merge pull request #1364 from dzhiryakov/patch-1
jenssegers Nov 24, 2017
13ba9a3
Refactoring tests
carusogabriel Dec 8, 2017
adfbc5b
fix: find() now uses getKeyName() for primary key
danjohnson95 Dec 11, 2017
8895014
Merge pull request #1385 from carusogabriel/refactoring-tests
jenssegers Dec 22, 2017
df3ebde
Merge pull request #1350 from jgcl/master
jenssegers Dec 22, 2017
5d55933
Merge pull request #1392 from DMNSteve/1354
jenssegers Dec 22, 2017
f12f766
Merge pull request #1330 from NoelDeMartin/fix-1318
jenssegers Dec 22, 2017
c3b622a
fix: fixes dsn connection strings by url encoding
Jan 5, 2018
b1ed166
refactor: support dsn strings with and without mongodb prefix
Jan 5, 2018
da9f4a1
Merge pull request #1397 from DMNSteve/1396
jenssegers Jan 8, 2018
99f402b
Merge pull request #1328 from lindelius/fix-1276
jenssegers Jan 8, 2018
9c750f0
Apply fixes from StyleCI
jenssegers Jan 8, 2018
321885c
Merge pull request #1398 from jenssegers/analysis-XN0yQy
jenssegers Jan 8, 2018
5a87639
Make testing easier with docker
jenssegers Jan 8, 2018
60c5329
Merge pull request #1326 from pimlie/master
jenssegers Jan 8, 2018
db98372
Fix some test issues
jenssegers Jan 8, 2018
6180fe1
refactor: remove unrequired aliasing
Jan 11, 2018
1bb259a
Merge pull request #1404 from StevePorter92/refactor-alias
jenssegers Jan 22, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.6
- 7
- 7.1

Expand Down
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ composer require jenssegers/mongodb
5.2.x | 2.3.x or 3.0.x
5.3.x | 3.1.x or 3.2.x
5.4.x | 3.2.x
5.5.x | 3.3.x

And add the service provider in `config/app.php`:

Expand Down Expand Up @@ -102,6 +103,15 @@ Embedded relations now return an `Illuminate\Database\Eloquent\Collection` rathe
$books = $user->books()->sortBy('title');
```

Testing
-------

To run the test for this package, run:

```
docker-compose up
```

Configuration
-------------

Expand Down Expand Up @@ -143,6 +153,18 @@ You can connect to multiple servers or replica sets with the following configura
],
```

Alternatively, you can use MongoDB connection string:

```php
'mongodb' => [
'driver' => 'mongodb',
'dsn' => env('DB_DSN'),
'database' => env('DB_DATABASE'),
],
```

Please refer to MongoDB official docs for its URI format: https://docs.mongodb.com/manual/reference/connection-string/

Eloquent
--------

Expand Down Expand Up @@ -942,7 +964,7 @@ $cursor = DB::collection('users')->raw(function($collection)
Optional: if you don't pass a closure to the raw method, the internal MongoCollection object will be accessible:

```php
$model = User::raw()->findOne(['age' => array('$lt' => 18]));
$model = User::raw()->findOne(['age' => array('$lt' => 18)]);
```

The internal MongoClient and MongoDB objects can be accessed like this:
Expand Down
15 changes: 8 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
],
"license" : "MIT",
"require": {
"illuminate/support": "^5.1",
"illuminate/container": "^5.1",
"illuminate/database": "^5.1",
"illuminate/events": "^5.1",
"illuminate/support": "^5.5",
"illuminate/container": "^5.5",
"illuminate/database": "^5.5",
"illuminate/events": "^5.5",
"mongodb/mongodb": "^1.0.0"
},
"require-dev": {
"phpunit/phpunit": "^5.0|^6.0",
"phpunit/phpunit": "^6.0",
"orchestra/testbench": "^3.1",
"mockery/mockery": "^0.9",
"satooshi/php-coveralls": "^1.0"
"mockery/mockery": "^1.0",
"satooshi/php-coveralls": "^2.0",
"doctrine/dbal": "^2.5"
},
"autoload": {
"psr-0": {
Expand Down
29 changes: 29 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: '3'

services:

php:
build:
context: .
dockerfile: docker/Dockerfile
volumes:
- .:/code
working_dir: /code
command: docker/entrypoint.sh
depends_on:
- mysql
- mongodb

mysql:
image: mysql
environment:
MYSQL_ROOT_PASSWORD:
MYSQL_DATABASE: unittest
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
logging:
driver: none

mongodb:
image: mongo
logging:
driver: none
6 changes: 6 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM php:7.1-cli

RUN apt-get update && \
apt-get install -y autoconf pkg-config libssl-dev && \
pecl install mongodb && docker-php-ext-enable mongodb && \
docker-php-ext-install -j$(nproc) pdo pdo_mysql
3 changes: 3 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

sleep 3 && php ./vendor/bin/phpunit
2 changes: 1 addition & 1 deletion src/Jenssegers/Mongodb/Auth/DatabaseTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected function tokenExpired($token)
$date = $token['created_at']->toDateTime();
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
$token['created_at'] = $date->format('Y-m-d H:i:s');
} elseif (is_array($token['created_at']) and isset($token['created_at']['date'])) {
} elseif (is_array($token['created_at']) && isset($token['created_at']['date'])) {
$date = new DateTime($token['created_at']['date'], new DateTimeZone(isset($token['created_at']['timezone']) ? $token['created_at']['timezone'] : 'UTC'));
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
$token['created_at'] = $date->format('Y-m-d H:i:s');
Expand Down
2 changes: 1 addition & 1 deletion src/Jenssegers/Mongodb/Auth/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Jenssegers\Mongodb\Eloquent\Model as Model;
use Jenssegers\Mongodb\Eloquent\Model;

class User extends Model implements
AuthenticatableContract,
Expand Down
54 changes: 47 additions & 7 deletions src/Jenssegers/Mongodb/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Jenssegers\Mongodb;

use Illuminate\Database\Connection as BaseConnection;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use MongoDB\Client;

class Connection extends BaseConnection
Expand Down Expand Up @@ -34,7 +36,7 @@ public function __construct(array $config)
$dsn = $this->getDsn($config);

// You can pass options directly to the MongoDB constructor
$options = array_get($config, 'options', []);
$options = Arr::get($config, 'options', []);

// Create the connection
$this->connection = $this->createConnection($dsn, $config, $options);
Expand Down Expand Up @@ -149,18 +151,43 @@ public function disconnect()
}

/**
* Create a DSN string from a configuration.
* Determine if the given configuration array has a UNIX socket value.
*
* @param array $config
* @param array $config
* @return bool
*/
protected function hasDsnString(array $config)
{
return isset($config['dsn']) && ! empty($config['dsn']);
}

/**
* Get the DSN string for a socket configuration.
*
* @param array $config
* @return string
*/
protected function getDsn(array $config)
protected function getDsnString(array $config)
{
// Check if the user passed a complete dsn to the configuration.
if (!empty($config['dsn'])) {
return $config['dsn'];
$dsn_string = $config['dsn'];

if (Str::contains($dsn_string, 'mongodb://')) {
$dsn_string = Str::replaceFirst('mongodb://', '', $dsn_string);
}

$dsn_string = rawurlencode($dsn_string);

return "mongodb://{$dsn_string}";
}

/**
* Get the DSN string for a host / port configuration.
*
* @param array $config
* @return string
*/
protected function getHostDsn(array $config)
{
// Treat host option as array of hosts
$hosts = is_array($config['host']) ? $config['host'] : [$config['host']];

Expand All @@ -177,6 +204,19 @@ protected function getDsn(array $config)
return 'mongodb://' . implode(',', $hosts) . ($auth_database ? '/' . $auth_database : '');
}

/**
* Create a DSN string from a configuration.
*
* @param array $config
* @return string
*/
protected function getDsn(array $config)
{
return $this->hasDsnString($config)
? $this->getDsnString($config)
: $this->getHostDsn($config);
}

/**
* @inheritdoc
*/
Expand Down
18 changes: 17 additions & 1 deletion src/Jenssegers/Mongodb/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ public function decrement($column, $amount = 1, array $extra = [])
return parent::decrement($column, $amount, $extra);
}

/**
* @inheritdoc
*/
public function chunkById($count, callable $callback, $column = '_id', $alias = null)
{
return parent::chunkById($count, $callback, $column, $alias);
}

/**
* @inheritdoc
*/
Expand All @@ -162,10 +170,18 @@ public function raw($expression = null)

return $this->model->newFromBuilder((array) $results);
} // The result is a single object.
elseif (is_array($results) and array_key_exists('_id', $results)) {
elseif (is_array($results) && array_key_exists('_id', $results)) {
return $this->model->newFromBuilder((array) $results);
}

return $results;
}

/**
* @return \Illuminate\Database\ConnectionInterface
*/
public function getConnection()
{
return $this->query->getConnection();
}
}
5 changes: 3 additions & 2 deletions src/Jenssegers/Mongodb/Eloquent/EmbedsRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jenssegers\Mongodb\Eloquent;

use Illuminate\Support\Str;
use Jenssegers\Mongodb\Relations\EmbedsMany;
use Jenssegers\Mongodb\Relations\EmbedsOne;

Expand Down Expand Up @@ -32,7 +33,7 @@ protected function embedsMany($related, $localKey = null, $foreignKey = null, $r
}

if (is_null($foreignKey)) {
$foreignKey = snake_case(class_basename($this));
$foreignKey = Str::snake(class_basename($this));
}

$query = $this->newQuery();
Expand Down Expand Up @@ -67,7 +68,7 @@ protected function embedsOne($related, $localKey = null, $foreignKey = null, $re
}

if (is_null($foreignKey)) {
$foreignKey = snake_case(class_basename($this));
$foreignKey = Str::snake(class_basename($this));
}

$query = $this->newQuery();
Expand Down
40 changes: 35 additions & 5 deletions src/Jenssegers/Mongodb/Eloquent/HybridRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,20 @@ public function morphTo($name = null, $type = null, $id = null)
* @param string $collection
* @param string $foreignKey
* @param string $otherKey
* @param string $parentKey
* @param string $relatedKey
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null, $relation = null)
{
public function belongsToMany(
$related,
$collection = null,
$foreignKey = null,
$otherKey = null,
$parentKey = null,
$relatedKey = null,
$relation = null
) {
// If no relationship name was passed, we will pull backtraces to get the
// name of the calling function. We will use that function name as the
// title of this relation since that is a great convention to apply.
Expand All @@ -228,7 +237,15 @@ public function belongsToMany($related, $collection = null, $foreignKey = null,

// Check if it is a relation with an original model.
if (!is_subclass_of($related, \Jenssegers\Mongodb\Eloquent\Model::class)) {
return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation);
return parent::belongsToMany(
$related,
$collection,
$foreignKey,
$otherKey,
$parentKey,
$relatedKey,
$relation
);
}

// First, we'll need to determine the foreign key and "other key" for the
Expand All @@ -252,7 +269,16 @@ public function belongsToMany($related, $collection = null, $foreignKey = null,
// appropriate query constraint and entirely manages the hydrations.
$query = $instance->newQuery();

return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $relation);
return new BelongsToMany(
$query,
$this,
$collection,
$foreignKey,
$otherKey,
$parentKey ?: $this->getKeyName(),
$relatedKey ?: $instance->getKeyName(),
$relation
);
}

/**
Expand All @@ -274,6 +300,10 @@ protected function guessBelongsToManyRelation()
*/
public function newEloquentBuilder($query)
{
return new EloquentBuilder($query);
if (is_subclass_of($this, \Jenssegers\Mongodb\Eloquent\Model::class)) {
return new Builder($query);
} else {
return new EloquentBuilder($query);
}
}
}
Loading