Commit eed876e6 authored by Jens Segers's avatar Jens Segers

Merge pull request #276 from v-six/master

Allows non-string _id
parents 96036ab1 b4456474
......@@ -45,17 +45,22 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
/**
* Custom accessor for the model's id.
*
* @return string
* @param mixed $value
*
* @return mixed
*/
public function getIdAttribute($value)
{
if ($value) return (string) $value;
if (!$value && array_key_exists('_id', $this->attributes)) {
$value = $this->attributes['_id'];
}
// Return _id as string
if (array_key_exists('_id', $this->attributes))
{
return (string) $this->attributes['_id'];
if ($value instanceof MongoId) {
return (string) $value;
}
return $value;
}
/**
......
......@@ -79,7 +79,7 @@ class ModelTest extends TestCase {
$this->assertEquals(20, $check->age);
}
public function testManualId()
public function testManualStringId()
{
$user = new User;
$user->_id = '4af9f23d8ead0e1d32000000';
......@@ -93,6 +93,35 @@ class ModelTest extends TestCase {
$raw = $user->getAttributes();
$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()
......
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