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 { ...@@ -45,17 +45,22 @@ 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 (!$value && array_key_exists('_id', $this->attributes)) {
$value = $this->attributes['_id'];
}
// Return _id as string // Return _id as string
if (array_key_exists('_id', $this->attributes)) if ($value instanceof MongoId) {
{ return (string) $value;
return (string) $this->attributes['_id'];
} }
return $value;
} }
/** /**
......
...@@ -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