diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 0b6681cc6564..999a67496310 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -2377,6 +2377,16 @@ public function getIncrementing() return $this->incrementing; } + /** + * Get the value indicating the auto incrementing key type. + * + * @return string + */ + public function getKeyType() + { + return $this->keyType; + } + /** * Set whether IDs are incrementing. * diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php index e823ebda63d4..d41e61ea7aed 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php @@ -174,7 +174,7 @@ protected function getEagerModelKeys(array $models) // null or 0 in (depending on if incrementing keys are in use) so the query wont // fail plus returns zero results, which should be what the developer expects. if (count($keys) === 0) { - return [$this->related->getIncrementing() ? 0 : null]; + return [$this->related->getIncrementing() && $this->related->getKeyType() === 'int' ? 0 : null]; } return array_values(array_unique($keys)); diff --git a/tests/Database/DatabaseEloquentBelongsToTest.php b/tests/Database/DatabaseEloquentBelongsToTest.php index 9792b1fa4418..146c5adffbed 100755 --- a/tests/Database/DatabaseEloquentBelongsToTest.php +++ b/tests/Database/DatabaseEloquentBelongsToTest.php @@ -97,6 +97,14 @@ public function testDefaultEagerConstraintsWhenIncrementing() $relation->addEagerConstraints($models); } + public function testDefaultEagerConstraintsWhenIncrementingAndNonIntKeyType() + { + $relation = $this->getRelation(null, false, 'string'); + $relation->getQuery()->shouldReceive('whereIn')->once()->with('relation.id', m::mustBe([null])); + $models = [new MissingEloquentBelongsToModelStub, new MissingEloquentBelongsToModelStub]; + $relation->addEagerConstraints($models); + } + public function testDefaultEagerConstraintsWhenNotIncrementing() { $relation = $this->getRelation(null, false); @@ -105,12 +113,13 @@ public function testDefaultEagerConstraintsWhenNotIncrementing() $relation->addEagerConstraints($models); } - protected function getRelation($parent = null, $incrementing = true) + protected function getRelation($parent = null, $incrementing = true, $keyType = 'int') { $builder = m::mock('Illuminate\Database\Eloquent\Builder'); $builder->shouldReceive('where')->with('relation.id', '=', 'foreign.value'); $related = m::mock('Illuminate\Database\Eloquent\Model'); $related->incrementing = $incrementing; + $related->shouldReceive('getKeyType')->andReturn($keyType); $related->shouldReceive('getIncrementing')->andReturn($incrementing); $related->shouldReceive('getKeyName')->andReturn('id'); $related->shouldReceive('getTable')->andReturn('relation');