Commit 28b1068f authored by Jens Segers's avatar Jens Segers

Added schema builder support

parent db622f7a
...@@ -43,16 +43,6 @@ And add a new mongodb connection: ...@@ -43,16 +43,6 @@ And add a new mongodb connection:
'database' => 'database' 'database' => 'database'
), ),
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';
}
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(
...@@ -78,12 +68,14 @@ Tell your model to use the MongoDB model and set the collection (alias for table ...@@ -78,12 +68,14 @@ Tell your model to use the MongoDB model and set the collection (alias for table
} }
*You can also specify the connection name in the model by changing the `connection` attribute.*
Everything else works just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent Everything else works just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent
Query Builder Query Builder
------------- -------------
Once you have selected a mongodb connection, you can execute queries just like the original query builder. The main difference is that we are using `collection` instead of `table` (but table will work as well), and some additional operations like `push` and `pull`. The database driver plugs right into the original query builder. When using mongodb connections you will be able to build fluent queries to perform database operations. For your convenience, there is a `collection` alias for `table` as well as some additional mongodb specific operations like `push` and `pull`.
// With custom connection // With custom connection
$user = DB::connection('mongodb')->collection('users')->get(); $user = DB::connection('mongodb')->collection('users')->get();
...@@ -94,10 +86,31 @@ Once you have selected a mongodb connection, you can execute queries just like t ...@@ -94,10 +86,31 @@ Once you have selected a mongodb connection, you can execute queries just like t
Read more about the query builder on http://laravel.com/docs/queries Read more about the query builder on http://laravel.com/docs/queries
Schema
------
The database driver also has (limited) schema builder support. You can easily manipulate collections and set indexes:
Schema::create('users', function($collection)
{
$collection->index('name');
$collection->unique('email');
});
Supported operations are:
- create and drop
- collection
- hasCollection
- index and dropIndex
- unique and dropUnique
Read more about the schema builder on http://laravel.com/docs/schema
Sessions Sessions
-------- --------
If you want a MongoDB session driver, check out https://github.com/jenssegers/Laravel-MongoDB-Session The MongoDB session driver is available in a separate package, check out https://github.com/jenssegers/Laravel-MongoDB-Session
Documentation Documentation
------------- -------------
......
...@@ -77,6 +77,16 @@ class Connection extends \Illuminate\Database\Connection { ...@@ -77,6 +77,16 @@ class Connection extends \Illuminate\Database\Connection {
return $this->db->{$name}; return $this->db->{$name};
} }
/**
* Get a schema builder instance for the connection.
*
* @return Schema\Builder
*/
public function getSchemaBuilder()
{
return new Schema\Builder($this);
}
/** /**
* Get the MongoDB database object. * Get the MongoDB database object.
* *
......
<?php namespace Jenssegers\Mongodb\Schema;
use Closure;
use Illuminate\Database\Connection;
class Blueprint {
/**
* The MongoCollection object for this blueprint.
*
* @var MongoCollection
*/
protected $collection;
/**
* Create a new schema blueprint.
*
* @param string $table
* @param Closure $callback
* @return void
*/
public function __construct(Connection $connection, $collection)
{
$this->collection = $connection->getCollection($collection);
}
/**
* Specify an index for the collection.
*
* @param string|array $columns
* @param array $otions
* @return bool
*/
public function index($columns, $options = array())
{
$result = $this->collection->ensureIndex($columns, $options);
return (1 == (int) $result['ok']);
}
/**
* Indicate that the given index should be dropped.
*
* @param string|array $columns
* @return bool
*/
public function dropIndex($columns)
{
$result = $this->collection->deleteIndex($columns);
return (1 == (int) $result['ok']);
}
/**
* Specify a unique index for the collection.
*
* @param string|array $columns
* @param string $name
* @return bool
*/
public function unique($columns)
{
return $this->index($columns, array("unique" => true));
}
/**
* Indicate that the given unique key should be dropped.
*
* @param string|array $index
* @return bool
*/
public function dropUnique($columns)
{
return $this->dropIndex($columns);
}
/**
* Indicate that the collection should be dropped.
*
* @return bool
*/
public function drop()
{
$result = $this->collection->drop();
return (1 == (int) $result['ok']);
}
}
<?php namespace Jenssegers\Mongodb\Schema;
use Closure;
use Jenssegers\Mongodb\Connection;
use Jenssegers\Mongodb\Schema\Blueprint;
class Builder extends \Illuminate\Database\Schema\Builder {
/**
* Create a new database Schema manager.
*
* @param Connection $connection
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
/**
* Determine if the given collection exists.
*
* @param string $collection
* @return bool
*/
public function hasCollection($collection)
{
$db = $this->connection->getMongoDB();
return in_array($collection, $db->getCollectionNames());
}
/**
* Determine if the given collection exists.
*
* @param string $collection
* @return bool
*/
public function hasTable($collection)
{
return $this->hasCollection($collection);
}
/**
* Modify a collection on the schema.
*
* @param string $collection
* @param Closure $callback
* @return bool
*/
public function collection($collection, Closure $callback)
{
$blueprint = $this->createBlueprint($collection);
return $callback($blueprint);
}
/**
* Modify a collection on the schema.
*
* @param string $collection
* @param Closure $callback
* @return bool
*/
public function table($collection, Closure $callback)
{
return $this->collection($collection, $callback);
}
/**
* Create a new collection on the schema.
*
* @param string $collection
* @param Closure $callback
* @return bool
*/
public function create($collection, Closure $callback = null)
{
$db = $this->connection->getMongoDB();
$db->createCollection($collection);
if ($callback)
{
return $this->collection($collection, $callback);
}
}
/**
* Drop a collection from the schema.
*
* @param string $collection
* @return bool
*/
public function drop($collection)
{
$blueprint = $this->createBlueprint($collection);
return $blueprint->drop();
}
/**
* Create a new Blueprint.
*
* @param string $collection
* @return Schema\Blueprint
*/
protected function createBlueprint($collection, Closure $callback = null)
{
return new Blueprint($this->connection, $collection);
}
}
\ No newline at end of file
...@@ -9,7 +9,6 @@ class CacheTest extends PHPUnit_Framework_TestCase { ...@@ -9,7 +9,6 @@ class CacheTest extends PHPUnit_Framework_TestCase {
public function setUp() public function setUp()
{ {
# clear cache
global $app; global $app;
$this->cache = $app['cache']; $this->cache = $app['cache'];
...@@ -26,14 +25,12 @@ class CacheTest extends PHPUnit_Framework_TestCase { ...@@ -26,14 +25,12 @@ class CacheTest extends PHPUnit_Framework_TestCase {
public function testCache() public function testCache()
{ {
# auto generate cache key
$users = DB::collection('users')->where('age', '>', 10)->remember(10)->get(); $users = DB::collection('users')->where('age', '>', 10)->remember(10)->get();
$this->assertEquals(3, count($users)); $this->assertEquals(3, count($users));
$users = DB::collection('users')->where('age', '>', 10)->getCached(); $users = DB::collection('users')->where('age', '>', 10)->getCached();
$this->assertEquals(3, count($users)); $this->assertEquals(3, count($users));
# store under predefined cache key
$users = User::where('age', '>', 10)->remember(10, 'db.users')->get(); $users = User::where('age', '>', 10)->remember(10, 'db.users')->get();
$this->assertEquals(3, count($users)); $this->assertEquals(3, count($users));
......
...@@ -5,7 +5,6 @@ class QueryTest extends PHPUnit_Framework_TestCase { ...@@ -5,7 +5,6 @@ class QueryTest extends PHPUnit_Framework_TestCase {
public static function setUpBeforeClass() public static function setUpBeforeClass()
{ {
// test data
User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin')); User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin'));
User::create(array('name' => 'Jane Doe', 'age' => 33, 'title' => 'admin')); User::create(array('name' => 'Jane Doe', 'age' => 33, 'title' => 'admin'));
User::create(array('name' => 'Harry Hoe', 'age' => 13, 'title' => 'user')); User::create(array('name' => 'Harry Hoe', 'age' => 13, 'title' => 'user'));
......
<?php
require_once('tests/app.php');
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class SchemaTest extends PHPUnit_Framework_TestCase {
public function setUp() {}
public function tearDown()
{
Schema::drop('newcollection');
}
public function testCreate()
{
Schema::create('newcollection');
$this->assertTrue(Schema::hasCollection('newcollection'));
}
public function testDrop()
{
Schema::create('newcollection');
Schema::drop('newcollection');
$this->assertFalse(Schema::hasCollection('newcollection'));
}
public function testIndex()
{
Schema::collection('newcollection', function($collection)
{
$collection->index('mykey');
});
$index = $this->getIndex('newcollection', 'mykey');
$this->assertEquals(1, $index);
}
public function testUnique()
{
Schema::collection('newcollection', function($collection)
{
$collection->unique('uniquekey');
});
$index = $this->getIndex('newcollection', 'uniquekey');
$this->assertEquals('unique', $index);
}
protected function getIndex($collection, $name)
{
$collection = DB::getCollection($collection);
foreach ($collection->getIndexInfo() as $index)
{
if (isset($index['key'][$name]))
{
if (isset($index['unique'])) return 'unique';
return $index['key'][$name];
}
}
return false;
}
}
\ 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