diff --git a/README.md b/README.md index e18c8ac85..fdf0cba21 100644 --- a/README.md +++ b/README.md @@ -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 ------------ @@ -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', @@ -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'; @@ -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', @@ -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 diff --git a/src/Jenssegers/Mongodb/DatabaseManager.php b/src/Jenssegers/Mongodb/DatabaseManager.php index bdde98030..64dc499ac 100644 --- a/src/Jenssegers/Mongodb/DatabaseManager.php +++ b/src/Jenssegers/Mongodb/DatabaseManager.php @@ -1,5 +1,11 @@ app['mongodb']); + Model::setConnectionResolver($this->app['db']); Model::setEventDispatcher($this->app['events']); } @@ -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); + }); } } \ No newline at end of file diff --git a/tests/CacheTest.php b/tests/CacheTest.php index a614b9090..d1e742ca2 100644 --- a/tests/CacheTest.php +++ b/tests/CacheTest.php @@ -1,7 +1,7 @@ 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; @@ -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); \ No newline at end of file