Commit c22670a5 authored by Kévin Bargoin's avatar Kévin Bargoin

Allows an _id attribute of another type than string

parent 96036ab1
......@@ -54,7 +54,11 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
// Return _id as string
if (array_key_exists('_id', $this->attributes))
{
return (string) $this->attributes['_id'];
if ($this->attributes['_id'] instanceof MongoId) {
return (string) $this->attributes['_id'];
}
return $this->attributes['_id'];
}
}
......
......@@ -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