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)
========================
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
......
<?php namespace Jenssegers\Mongodb;
/*
|--------------------------------------------------------------------------
| DEPRECATED
|--------------------------------------------------------------------------
*/
use Illuminate\Database\ConnectionResolverInterface;
use Jenssegers\Mongodb\Connection;
use Jenssegers\Mongodb\Builder as QueryBuilder;
......
<?php namespace Jenssegers\Mongodb\Facades;
/*
|--------------------------------------------------------------------------
| DEPRECATED
|--------------------------------------------------------------------------
*/
use Illuminate\Support\Facades\Facade;
class DB extends Facade {
......
......@@ -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']);
}
......@@ -24,13 +24,17 @@ class MongodbServiceProvider extends ServiceProvider {
*/
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
<?php
require_once('tests/app.php');
use Jenssegers\Mongodb\Facades\DB;
use Illuminate\Support\Facades\DB;
class CacheTest extends PHPUnit_Framework_TestCase {
......
<?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 {
......
<?php
require_once('tests/app.php');
use Jenssegers\Mongodb\Facades\DB;
use Illuminate\Support\Facades\DB;
class QueryTest extends PHPUnit_Framework_TestCase {
......
......@@ -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;
......@@ -20,15 +26,23 @@ $app['events'] = new Dispatcher;
$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
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