Skip to content

Commit

Permalink
Extend laravel database manager instead of registering a new manager,…
Browse files Browse the repository at this point in the history
… probably fixes mongodb#18
  • Loading branch information
mnphpexpert committed Aug 15, 2013
1 parent 9dd6aec commit 5965ea4
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 22 deletions.
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
Laravel Eloquent MongoDB [![Build Status](https://travis-ci.org/jenssegers/Laravel-MongoDB.png?branch=master)](https://travis-ci.org/jenssegers/Laravel-MongoDB)
========================

An Eloquent model that supports MongoDB, inspired by LMongo, but using the original Eloquent methods.
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.

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

For more information about Eloquent, check http://laravel.com/docs/eloquent.
**ATTENTION WHEN UPGRADING!**
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.

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

'Jenssegers\Mongodb\MongodbServiceProvider',

Add an alias for the database manager, you can change this alias to your own preference:

'MDB' => 'Jenssegers\Mongodb\Facades\DB',
The service provider will register a mongodb extension with the original database manager, so that everything else keeps working just fine.

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

This package will automatically check the database configuration in `app/config/database.php` for a 'mongodb' item.
Change your default database connection name in `app/config/database.php`:

'default' => 'mongodb',

And add a new mongodb connection:

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

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

use Jenssegers\Mongodb\Model as Eloquent;

class MyModel extends Eloquent {

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

'mongodb' => array(
'driver' => 'mongodb',
'host' => array('server1', 'server2),
'port' => 27017,
'username' => 'username',
Expand Down Expand Up @@ -78,8 +85,8 @@ Query Builder

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):

$users = MDB::collection('users')->get();
$user = MDB::collection('users')->where('name', 'John')->first();
$users = DB::collection('users')->get();
$user = DB::collection('users')->where('name', 'John')->first();

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

Expand Down
6 changes: 6 additions & 0 deletions src/Jenssegers/Mongodb/DatabaseManager.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?php namespace Jenssegers\Mongodb;

/*
|--------------------------------------------------------------------------
| DEPRECATED
|--------------------------------------------------------------------------
*/

use Illuminate\Database\ConnectionResolverInterface;
use Jenssegers\Mongodb\Connection;
use Jenssegers\Mongodb\Builder as QueryBuilder;
Expand Down
6 changes: 6 additions & 0 deletions src/Jenssegers/Mongodb/Facades/DB.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?php namespace Jenssegers\Mongodb\Facades;

/*
|--------------------------------------------------------------------------
| DEPRECATED
|--------------------------------------------------------------------------
*/

use Illuminate\Support\Facades\Facade;

class DB extends Facade {
Expand Down
12 changes: 8 additions & 4 deletions src/Jenssegers/Mongodb/MongodbServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MongodbServiceProvider extends ServiceProvider {
*/
public function boot()
{
Model::setConnectionResolver($this->app['mongodb']);
Model::setConnectionResolver($this->app['db']);
Model::setEventDispatcher($this->app['events']);
}

Expand All @@ -24,13 +24,17 @@ public function boot()
*/
public function register()
{
// The database manager is used to resolve various connections, since multiple
// connections might be managed. It also implements the connection resolver
// interface which may be used by other components requiring connections.
// DEPRECATED
$this->app['mongodb'] = $this->app->share(function($app)
{
return new DatabaseManager($app);
});

// Add a mongodb extension to the original database manager
$this->app['db']->extend('mongodb', function($config)
{
return new Connection($config);
});
}

}
2 changes: 1 addition & 1 deletion tests/CacheTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
require_once('tests/app.php');

use Jenssegers\Mongodb\Facades\DB;
use Illuminate\Support\Facades\DB;

class CacheTest extends PHPUnit_Framework_TestCase {

Expand Down
2 changes: 1 addition & 1 deletion tests/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
require_once('tests/app.php');

use Jenssegers\Mongodb\Facades\DB;
use Illuminate\Support\Facades\DB;
use Jenssegers\Mongodb\Connection;

class ConnectionTest extends PHPUnit_Framework_TestCase {
Expand Down
2 changes: 1 addition & 1 deletion tests/QueryTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
require_once('tests/app.php');

use Jenssegers\Mongodb\Facades\DB;
use Illuminate\Support\Facades\DB;

class QueryTest extends PHPUnit_Framework_TestCase {

Expand Down
28 changes: 21 additions & 7 deletions tests/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@
$loader = require 'vendor/autoload.php';
$loader->add('', 'tests/models');

use Jenssegers\Mongodb\Connection;
use Jenssegers\Mongodb\Model;
use Jenssegers\Mongodb\DatabaseManager;
use Jenssegers\Mongodb\Facades\DB;
use Illuminate\Support\Facades\DB;
use Illuminate\Container\Container;
use Illuminate\Database\DatabaseManager;
use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Events\Dispatcher;
use Illuminate\Cache\ArrayStore;
use Illuminate\Cache\Repository;

# Fake app class
class App extends ArrayObject {
function bound() {}
}

# Fake app
$app = array();
$app = new App;

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

# Database configuration
$app['config']['database.fetch'] = null;
$app['config']['database.default'] = 'mongodb';
$app['config']['database.connections']['mongodb'] = array(
'name' => 'mongodb',
'driver' => 'mongodb',
'host' => 'localhost',
'database' => 'unittest'
);

# Register service
$app['mongodb'] = new DatabaseManager($app);
# Initialize database manager
$app['db.factory'] = new ConnectionFactory(new Container);
$app['db'] = new DatabaseManager($app, $app['db.factory']);

# Extend database manager with reflection hack
$reflection = new ReflectionClass('Jenssegers\Mongodb\Connection');
$app['db']->extend('mongodb', array($reflection, 'newInstance'));

# Static setup
Model::setConnectionResolver($app['mongodb']);
Model::setConnectionResolver($app['db']);
DB::setFacadeApplication($app);

0 comments on commit 5965ea4

Please sign in to comment.