diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index f446319d5d7a..52f1eb7aacdd 100755 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -70,6 +70,13 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab */ public $incrementing = true; + /** + * The type used by the incrementing ID. + * + * @var string + */ + public $idType = 'int'; + /** * Indicates if the model should be timestamped. * @@ -2763,7 +2770,7 @@ public function getCasts() { if ($this->getIncrementing()) { return array_merge([ - $this->getKeyName() => 'int', + $this->getKeyName() => $this->idType, ], $this->casts); } diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index d6a19125f4ef..7ae315335399 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -1319,6 +1319,28 @@ public function testIssetBehavesCorrectlyWithAttributesAndRelationships() $this->assertTrue(isset($model->some_relation)); } + public function testIntIdTypePreserved() + { + $model = $this->getMock('EloquentModelStub', ['newQueryWithoutScopes', 'updateTimestamps', 'refresh']); + $query = m::mock('Illuminate\Database\Eloquent\Builder'); + $query->shouldReceive('insertGetId')->once()->with([], 'id')->andReturn(1); + $model->expects($this->once())->method('newQueryWithoutScopes')->will($this->returnValue($query)); + + $this->assertTrue($model->save()); + $this->assertEquals(1, $model->id); + } + + public function testStringIdTypePreserved() + { + $model = $this->getMock('EloquentIdTypeModelStub', ['newQueryWithoutScopes', 'updateTimestamps', 'refresh']); + $query = m::mock('Illuminate\Database\Eloquent\Builder'); + $query->shouldReceive('insertGetId')->once()->with([], 'id')->andReturn('string id'); + $model->expects($this->once())->method('newQueryWithoutScopes')->will($this->returnValue($query)); + + $this->assertTrue($model->save()); + $this->assertEquals('string id', $model->id); + } + protected function addMockConnection($model) { $model->setConnectionResolver($resolver = m::mock('Illuminate\Database\ConnectionResolverInterface')); @@ -1431,6 +1453,11 @@ public function setIncrementing($value) } } +class EloquentIdTypeModelStub extends EloquentModelStub +{ + public $idType = 'string'; +} + class EloquentModelFindWithWritePdoStub extends Model { public function newQuery()