Commit 5c6a9717 authored by Jens Segers's avatar Jens Segers

Extend laravel database manager instead of registering a new manager, probably fixes #18

parent caba1340
Laravel Eloquent MongoDB [![Build Status](https://travis-ci.org/jenssegers/Laravel-MongoDB.png?branch=master)](https://travis-ci.org/jenssegers/Laravel-MongoDB) 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.* *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 Installation
------------ ------------
...@@ -22,16 +23,19 @@ Add the service provider in `app/config/app.php`: ...@@ -22,16 +23,19 @@ Add the service provider in `app/config/app.php`:
'Jenssegers\Mongodb\MongodbServiceProvider', 'Jenssegers\Mongodb\MongodbServiceProvider',
Add an alias for the database manager, you can change this alias to your own preference: The service provider will register a mongodb extension with the original database manager, so that everything else keeps working just fine.
'MDB' => 'Jenssegers\Mongodb\Facades\DB',
Configuration 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( 'mongodb' => array(
'driver' => 'mongodb',
'host' => 'localhost', 'host' => 'localhost',
'port' => 27017, 'port' => 27017,
'username' => 'username', 'username' => 'username',
...@@ -41,6 +45,8 @@ This package will automatically check the database configuration in `app/config/ ...@@ -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: 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 { class MyModel extends Eloquent {
protected $connection = 'mongodb2'; protected $connection = 'mongodb2';
...@@ -50,6 +56,7 @@ You can also specify the connection name in the model if you have multiple conne ...@@ -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: You can connect to multiple servers or replica sets with the following configuration:
'mongodb' => array( 'mongodb' => array(
'driver' => 'mongodb',
'host' => array('server1', 'server2), 'host' => array('server1', 'server2),
'port' => 27017, 'port' => 27017,
'username' => 'username', 'username' => 'username',
...@@ -78,8 +85,8 @@ Query Builder ...@@ -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): 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(); $users = DB::collection('users')->get();
$user = MDB::collection('users')->where('name', 'John')->first(); $user = DB::collection('users')->where('name', 'John')->first();
Read more about the query builder on http://laravel.com/docs/queries Read more about the query builder on http://laravel.com/docs/queries
......
<?php namespace Jenssegers\Mongodb; <?php namespace Jenssegers\Mongodb;
/*
|--------------------------------------------------------------------------
| DEPRECATED
|--------------------------------------------------------------------------
*/
use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Database\ConnectionResolverInterface;
use Jenssegers\Mongodb\Connection; use Jenssegers\Mongodb\Connection;
use Jenssegers\Mongodb\Builder as QueryBuilder; use Jenssegers\Mongodb\Builder as QueryBuilder;
......
<?php namespace Jenssegers\Mongodb\Facades; <?php namespace Jenssegers\Mongodb\Facades;
/*
|--------------------------------------------------------------------------
| DEPRECATED
|--------------------------------------------------------------------------
*/
use Illuminate\Support\Facades\Facade; use Illuminate\Support\Facades\Facade;
class DB extends Facade { class DB extends Facade {
......
...@@ -13,7 +13,7 @@ class MongodbServiceProvider extends ServiceProvider { ...@@ -13,7 +13,7 @@ class MongodbServiceProvider extends ServiceProvider {
*/ */
public function boot() public function boot()
{ {
Model::setConnectionResolver($this->app['mongodb']); Model::setConnectionResolver($this->app['db']);
Model::setEventDispatcher($this->app['events']); Model::setEventDispatcher($this->app['events']);
} }
...@@ -24,13 +24,17 @@ class MongodbServiceProvider extends ServiceProvider { ...@@ -24,13 +24,17 @@ class MongodbServiceProvider extends ServiceProvider {
*/ */
public function register() public function register()
{ {
// The database manager is used to resolve various connections, since multiple // DEPRECATED
// connections might be managed. It also implements the connection resolver
// interface which may be used by other components requiring connections.
$this->app['mongodb'] = $this->app->share(function($app) $this->app['mongodb'] = $this->app->share(function($app)
{ {
return new DatabaseManager($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
<?php <?php
require_once('tests/app.php'); require_once('tests/app.php');
use Jenssegers\Mongodb\Facades\DB; use Illuminate\Support\Facades\DB;
class CacheTest extends PHPUnit_Framework_TestCase { class CacheTest extends PHPUnit_Framework_TestCase {
......
<?php <?php
require_once('tests/app.php'); require_once('tests/app.php');
use Jenssegers\Mongodb\Facades\DB; use Illuminate\Support\Facades\DB;
use Jenssegers\Mongodb\Connection; use Jenssegers\Mongodb\Connection;
class ConnectionTest extends PHPUnit_Framework_TestCase { class ConnectionTest extends PHPUnit_Framework_TestCase {
......
<?php <?php
require_once('tests/app.php'); require_once('tests/app.php');
use Jenssegers\Mongodb\Facades\DB; use Illuminate\Support\Facades\DB;
class QueryTest extends PHPUnit_Framework_TestCase { class QueryTest extends PHPUnit_Framework_TestCase {
......
...@@ -2,16 +2,22 @@ ...@@ -2,16 +2,22 @@
$loader = require 'vendor/autoload.php'; $loader = require 'vendor/autoload.php';
$loader->add('', 'tests/models'); $loader->add('', 'tests/models');
use Jenssegers\Mongodb\Connection;
use Jenssegers\Mongodb\Model; use Jenssegers\Mongodb\Model;
use Jenssegers\Mongodb\DatabaseManager; use Illuminate\Support\Facades\DB;
use Jenssegers\Mongodb\Facades\DB; use Illuminate\Container\Container;
use Illuminate\Database\DatabaseManager;
use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Events\Dispatcher; use Illuminate\Events\Dispatcher;
use Illuminate\Cache\ArrayStore; use Illuminate\Cache\ArrayStore;
use Illuminate\Cache\Repository; use Illuminate\Cache\Repository;
# Fake app class
class App extends ArrayObject {
function bound() {}
}
# Fake app # Fake app
$app = array(); $app = new App;
# Event dispatcher # Event dispatcher
$app['events'] = new Dispatcher; $app['events'] = new Dispatcher;
...@@ -20,15 +26,23 @@ $app['events'] = new Dispatcher; ...@@ -20,15 +26,23 @@ $app['events'] = new Dispatcher;
$app['cache'] = new Repository(new ArrayStore); $app['cache'] = new Repository(new ArrayStore);
# Database configuration # Database configuration
$app['config']['database.fetch'] = null;
$app['config']['database.default'] = 'mongodb';
$app['config']['database.connections']['mongodb'] = array( $app['config']['database.connections']['mongodb'] = array(
'name' => 'mongodb', 'name' => 'mongodb',
'driver' => 'mongodb',
'host' => 'localhost', 'host' => 'localhost',
'database' => 'unittest' 'database' => 'unittest'
); );
# Register service # Initialize database manager
$app['mongodb'] = new DatabaseManager($app); $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 # Static setup
Model::setConnectionResolver($app['mongodb']); Model::setConnectionResolver($app['db']);
DB::setFacadeApplication($app); DB::setFacadeApplication($app);
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment