Skip to content

Commit 5c6a971

Browse files
committed
Extend laravel database manager instead of registering a new manager, probably fixes #18
1 parent caba134 commit 5c6a971

File tree

8 files changed

+59
-22
lines changed

8 files changed

+59
-22
lines changed

README.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
Laravel Eloquent MongoDB [![Build Status](https://travis-ci.org/jenssegers/Laravel-MongoDB.png?branch=master)](https://travis-ci.org/jenssegers/Laravel-MongoDB)
22
========================
33

4-
An Eloquent model that supports MongoDB, inspired by LMongo, but using the original Eloquent methods.
4+
An Eloquent model that supports MongoDB, inspired by LMongo, but using the original Eloquent methods. For more information about Eloquent, check http://laravel.com/docs/eloquent.
55

66
*This model extends the original Eloquent model, so it uses exactly the same methods.*
77

8-
For more information about Eloquent, check http://laravel.com/docs/eloquent.
8+
**ATTENTION WHEN UPGRADING!**
9+
The way the internal connection resolving works has been changed to extend original Laravel objects instead of registering new objects. Check the configuration section for more information about the new configuration. Remove the old `MDB` facade as it is now deprecated.
910

1011
Installation
1112
------------
@@ -22,16 +23,19 @@ Add the service provider in `app/config/app.php`:
2223

2324
'Jenssegers\Mongodb\MongodbServiceProvider',
2425

25-
Add an alias for the database manager, you can change this alias to your own preference:
26-
27-
'MDB' => 'Jenssegers\Mongodb\Facades\DB',
26+
The service provider will register a mongodb extension with the original database manager, so that everything else keeps working just fine.
2827

2928
Configuration
3029
-------------
3130

32-
This package will automatically check the database configuration in `app/config/database.php` for a 'mongodb' item.
31+
Change your default database connection name in `app/config/database.php`:
32+
33+
'default' => 'mongodb',
34+
35+
And add a new mongodb connection:
3336

3437
'mongodb' => array(
38+
'driver' => 'mongodb',
3539
'host' => 'localhost',
3640
'port' => 27017,
3741
'username' => 'username',
@@ -41,6 +45,8 @@ This package will automatically check the database configuration in `app/config/
4145

4246
You can also specify the connection name in the model if you have multiple connections:
4347

48+
use Jenssegers\Mongodb\Model as Eloquent;
49+
4450
class MyModel extends Eloquent {
4551

4652
protected $connection = 'mongodb2';
@@ -50,6 +56,7 @@ You can also specify the connection name in the model if you have multiple conne
5056
You can connect to multiple servers or replica sets with the following configuration:
5157

5258
'mongodb' => array(
59+
'driver' => 'mongodb',
5360
'host' => array('server1', 'server2),
5461
'port' => 27017,
5562
'username' => 'username',
@@ -78,8 +85,8 @@ Query Builder
7885

7986
The MongoDB query builder allows you to execute queries, just like the original query builder (note that we are using the previously created alias here):
8087

81-
$users = MDB::collection('users')->get();
82-
$user = MDB::collection('users')->where('name', 'John')->first();
88+
$users = DB::collection('users')->get();
89+
$user = DB::collection('users')->where('name', 'John')->first();
8390

8491
Read more about the query builder on http://laravel.com/docs/queries
8592

src/Jenssegers/Mongodb/DatabaseManager.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<?php namespace Jenssegers\Mongodb;
22

3+
/*
4+
|--------------------------------------------------------------------------
5+
| DEPRECATED
6+
|--------------------------------------------------------------------------
7+
*/
8+
39
use Illuminate\Database\ConnectionResolverInterface;
410
use Jenssegers\Mongodb\Connection;
511
use Jenssegers\Mongodb\Builder as QueryBuilder;

src/Jenssegers/Mongodb/Facades/DB.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<?php namespace Jenssegers\Mongodb\Facades;
22

3+
/*
4+
|--------------------------------------------------------------------------
5+
| DEPRECATED
6+
|--------------------------------------------------------------------------
7+
*/
8+
39
use Illuminate\Support\Facades\Facade;
410

511
class DB extends Facade {

src/Jenssegers/Mongodb/MongodbServiceProvider.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class MongodbServiceProvider extends ServiceProvider {
1313
*/
1414
public function boot()
1515
{
16-
Model::setConnectionResolver($this->app['mongodb']);
16+
Model::setConnectionResolver($this->app['db']);
1717
Model::setEventDispatcher($this->app['events']);
1818
}
1919

@@ -24,13 +24,17 @@ public function boot()
2424
*/
2525
public function register()
2626
{
27-
// The database manager is used to resolve various connections, since multiple
28-
// connections might be managed. It also implements the connection resolver
29-
// interface which may be used by other components requiring connections.
27+
// DEPRECATED
3028
$this->app['mongodb'] = $this->app->share(function($app)
3129
{
3230
return new DatabaseManager($app);
3331
});
32+
33+
// Add a mongodb extension to the original database manager
34+
$this->app['db']->extend('mongodb', function($config)
35+
{
36+
return new Connection($config);
37+
});
3438
}
3539

3640
}

tests/CacheTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
require_once('tests/app.php');
33

4-
use Jenssegers\Mongodb\Facades\DB;
4+
use Illuminate\Support\Facades\DB;
55

66
class CacheTest extends PHPUnit_Framework_TestCase {
77

tests/ConnectionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
require_once('tests/app.php');
33

4-
use Jenssegers\Mongodb\Facades\DB;
4+
use Illuminate\Support\Facades\DB;
55
use Jenssegers\Mongodb\Connection;
66

77
class ConnectionTest extends PHPUnit_Framework_TestCase {

tests/QueryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
require_once('tests/app.php');
33

4-
use Jenssegers\Mongodb\Facades\DB;
4+
use Illuminate\Support\Facades\DB;
55

66
class QueryTest extends PHPUnit_Framework_TestCase {
77

tests/app.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22
$loader = require 'vendor/autoload.php';
33
$loader->add('', 'tests/models');
44

5-
use Jenssegers\Mongodb\Connection;
65
use Jenssegers\Mongodb\Model;
7-
use Jenssegers\Mongodb\DatabaseManager;
8-
use Jenssegers\Mongodb\Facades\DB;
6+
use Illuminate\Support\Facades\DB;
7+
use Illuminate\Container\Container;
8+
use Illuminate\Database\DatabaseManager;
9+
use Illuminate\Database\Connectors\ConnectionFactory;
910
use Illuminate\Events\Dispatcher;
1011
use Illuminate\Cache\ArrayStore;
1112
use Illuminate\Cache\Repository;
1213

14+
# Fake app class
15+
class App extends ArrayObject {
16+
function bound() {}
17+
}
18+
1319
# Fake app
14-
$app = array();
20+
$app = new App;
1521

1622
# Event dispatcher
1723
$app['events'] = new Dispatcher;
@@ -20,15 +26,23 @@
2026
$app['cache'] = new Repository(new ArrayStore);
2127

2228
# Database configuration
29+
$app['config']['database.fetch'] = null;
30+
$app['config']['database.default'] = 'mongodb';
2331
$app['config']['database.connections']['mongodb'] = array(
2432
'name' => 'mongodb',
33+
'driver' => 'mongodb',
2534
'host' => 'localhost',
2635
'database' => 'unittest'
2736
);
2837

29-
# Register service
30-
$app['mongodb'] = new DatabaseManager($app);
38+
# Initialize database manager
39+
$app['db.factory'] = new ConnectionFactory(new Container);
40+
$app['db'] = new DatabaseManager($app, $app['db.factory']);
41+
42+
# Extend database manager with reflection hack
43+
$reflection = new ReflectionClass('Jenssegers\Mongodb\Connection');
44+
$app['db']->extend('mongodb', array($reflection, 'newInstance'));
3145

3246
# Static setup
33-
Model::setConnectionResolver($app['mongodb']);
47+
Model::setConnectionResolver($app['db']);
3448
DB::setFacadeApplication($app);

0 commit comments

Comments
 (0)