Commit d2c1da3a authored by Jens Segers's avatar Jens Segers

Merge branch 'master' into embedded-models

parents e1750a63 7534e6f9
...@@ -133,6 +133,16 @@ class Connection extends \Illuminate\Database\Connection { ...@@ -133,6 +133,16 @@ class Connection extends \Illuminate\Database\Connection {
return new MongoClient($dsn, $options); return new MongoClient($dsn, $options);
} }
/**
* Disconnect from the underlying MongoClient connection.
*
* @return void
*/
public function disconnect()
{
$this->connection->close();
}
/** /**
* Create a DSN string from a configuration. * Create a DSN string from a configuration.
* *
......
...@@ -39,17 +39,26 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -39,17 +39,26 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
/** /**
* Custom accessor for the model's id. * Custom accessor for the model's id.
* *
* @return string * @param mixed $value
*
* @return mixed
*/ */
public function getIdAttribute($value) public function getIdAttribute($value)
{ {
if ($value) return (string) $value; // If we don't have a value for 'id', we will use the Mongo '_id' value.
// This allows us to work with models in a more sql-like way.
if ( ! $value and array_key_exists('_id', $this->attributes))
{
$value = $this->attributes['_id'];
}
// Return _id as string // Convert MongoId's to string.
if (array_key_exists('_id', $this->attributes)) if ($value instanceof MongoId)
{ {
return (string) $this->attributes['_id']; return (string) $value;
} }
return $value;
} }
/** /**
......
...@@ -15,7 +15,8 @@ class ConnectionTest extends TestCase { ...@@ -15,7 +15,8 @@ class ConnectionTest extends TestCase {
$this->assertEquals(spl_object_hash($c1), spl_object_hash($c2)); $this->assertEquals(spl_object_hash($c1), spl_object_hash($c2));
$c1 = DB::connection('mongodb'); $c1 = DB::connection('mongodb');
$c2 = DB::reconnect('mongodb'); DB::purge('mongodb');
$c2 = DB::connection('mongodb');
$this->assertNotEquals(spl_object_hash($c1), spl_object_hash($c2)); $this->assertNotEquals(spl_object_hash($c1), spl_object_hash($c2));
} }
......
...@@ -79,7 +79,7 @@ class ModelTest extends TestCase { ...@@ -79,7 +79,7 @@ class ModelTest extends TestCase {
$this->assertEquals(20, $check->age); $this->assertEquals(20, $check->age);
} }
public function testManualId() public function testManualStringId()
{ {
$user = new User; $user = new User;
$user->_id = '4af9f23d8ead0e1d32000000'; $user->_id = '4af9f23d8ead0e1d32000000';
...@@ -93,6 +93,35 @@ class ModelTest extends TestCase { ...@@ -93,6 +93,35 @@ class ModelTest extends TestCase {
$raw = $user->getAttributes(); $raw = $user->getAttributes();
$this->assertInstanceOf('MongoId', $raw['_id']); $this->assertInstanceOf('MongoId', $raw['_id']);
$user = new User;
$user->_id = 'customId';
$user->name = 'John Doe';
$user->title = 'admin';
$user->age = 35;
$user->save();
$this->assertEquals(true, $user->exists);
$this->assertEquals('customId', $user->_id);
$raw = $user->getAttributes();
$this->assertInternalType('string', $raw['_id']);
}
public function testManualIntId()
{
$user = new User;
$user->_id = 1;
$user->name = 'John Doe';
$user->title = 'admin';
$user->age = 35;
$user->save();
$this->assertEquals(true, $user->exists);
$this->assertEquals(1, $user->_id);
$raw = $user->getAttributes();
$this->assertInternalType('integer', $raw['_id']);
} }
public function testDelete() public function testDelete()
......
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