Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Adds idType to model in order to prevent assumptions about ids #13985

Merged
merged 7 commits into from
Jun 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -2763,7 +2770,7 @@ public function getCasts()
{
if ($this->getIncrementing()) {
return array_merge([
$this->getKeyName() => 'int',
$this->getKeyName() => $this->idType,
], $this->casts);
}

Expand Down
27 changes: 27 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down Expand Up @@ -1431,6 +1453,11 @@ public function setIncrementing($value)
}
}

class EloquentIdTypeModelStub extends EloquentModelStub
{
public $idType = 'string';
}

class EloquentModelFindWithWritePdoStub extends Model
{
public function newQuery()
Expand Down