Skip to content

Commit

Permalink
Merge branch 'where-in-raw' of https://github.com/staudenmeir/framework
Browse files Browse the repository at this point in the history
… into staudenmeir-where-in-raw
  • Loading branch information
taylorotwell committed Nov 11, 2018
2 parents abd5962 + d10b0a5 commit cfd1693
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ protected function whereInMethod(Model $model, $key)
{
return $model->getKeyName() === last(explode('.', $key))
&& in_array($model->getKeyType(), ['int', 'integer'])
? 'whereInRaw'
? 'whereInRawInt'
: 'whereIn';
}

Expand Down
12 changes: 7 additions & 5 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -949,23 +949,25 @@ protected function whereInExistingQuery($column, $query, $boolean, $not)
}

/**
* Add a "where in raw" clause to the query.
* Add a "where in raw" clause for integer values to the query.
*
* @param string $column
* @param array $values
* @param \Illuminate\Contracts\Support\Arrayable|array $values
* @param string $boolean
* @param bool $not
* @param bool $not
* @return $this
*/
public function whereInRaw($column, array $values, $boolean = 'and', $not = false)
public function whereInRawInt($column, $values, $boolean = 'and', $not = false)
{
$type = $not ? 'NotInRaw' : 'InRaw';

if ($values instanceof Arrayable) {
$values = $values->toArray();
}

$values = array_map('intval', $values);
foreach ($values as &$value) {
$value = (int) $value;
}

$this->wheres[] = compact('type', 'column', 'values', 'boolean');

Expand Down
8 changes: 4 additions & 4 deletions tests/Database/DatabaseEloquentBelongsToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function testEagerConstraintsAreProperlyAdded()
$relation = $this->getRelation();
$relation->getRelated()->shouldReceive('getKeyName')->andReturn('id');
$relation->getRelated()->shouldReceive('getKeyType')->andReturn('int');
$relation->getQuery()->shouldReceive('whereInRaw')->once()->with('relation.id', ['foreign.value', 'foreign.value.two']);
$relation->getQuery()->shouldReceive('whereInRawInt')->once()->with('relation.id', ['foreign.value', 'foreign.value.two']);
$models = [new EloquentBelongsToModelStub, new EloquentBelongsToModelStub, new AnotherEloquentBelongsToModelStub];
$relation->addEagerConstraints($models);
}
Expand All @@ -91,7 +91,7 @@ public function testIdsInEagerConstraintsCanBeZero()
$relation = $this->getRelation();
$relation->getRelated()->shouldReceive('getKeyName')->andReturn('id');
$relation->getRelated()->shouldReceive('getKeyType')->andReturn('int');
$relation->getQuery()->shouldReceive('whereInRaw')->once()->with('relation.id', ['foreign.value', 0]);
$relation->getQuery()->shouldReceive('whereInRawInt')->once()->with('relation.id', ['foreign.value', 0]);
$models = [new EloquentBelongsToModelStub, new EloquentBelongsToModelStubWithZeroId];
$relation->addEagerConstraints($models);
}
Expand Down Expand Up @@ -160,7 +160,7 @@ public function testDefaultEagerConstraintsWhenIncrementing()
$relation = $this->getRelation();
$relation->getRelated()->shouldReceive('getKeyName')->andReturn('id');
$relation->getRelated()->shouldReceive('getKeyType')->andReturn('int');
$relation->getQuery()->shouldReceive('whereInRaw')->once()->with('relation.id', m::mustBe([null]));
$relation->getQuery()->shouldReceive('whereInRawInt')->once()->with('relation.id', m::mustBe([null]));
$models = [new MissingEloquentBelongsToModelStub, new MissingEloquentBelongsToModelStub];
$relation->addEagerConstraints($models);
}
Expand All @@ -178,7 +178,7 @@ public function testDefaultEagerConstraintsWhenNotIncrementing()
$relation = $this->getRelation(null, false);
$relation->getRelated()->shouldReceive('getKeyName')->andReturn('id');
$relation->getRelated()->shouldReceive('getKeyType')->andReturn('int');
$relation->getQuery()->shouldReceive('whereInRaw')->once()->with('relation.id', m::mustBe([null]));
$relation->getQuery()->shouldReceive('whereInRawInt')->once()->with('relation.id', m::mustBe([null]));
$models = [new MissingEloquentBelongsToModelStub, new MissingEloquentBelongsToModelStub];
$relation->addEagerConstraints($models);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/DatabaseEloquentHasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function testEagerConstraintsAreProperlyAdded()
$relation = $this->getRelation();
$relation->getParent()->shouldReceive('getKeyName')->once()->andReturn('id');
$relation->getParent()->shouldReceive('getKeyType')->once()->andReturn('int');
$relation->getQuery()->shouldReceive('whereInRaw')->once()->with('table.foreign_key', [1, 2]);
$relation->getQuery()->shouldReceive('whereInRawInt')->once()->with('table.foreign_key', [1, 2]);
$model1 = new EloquentHasManyModelStub;
$model1->id = 1;
$model2 = new EloquentHasManyModelStub;
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/DatabaseEloquentHasOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public function testEagerConstraintsAreProperlyAdded()
$relation = $this->getRelation();
$relation->getParent()->shouldReceive('getKeyName')->once()->andReturn('id');
$relation->getParent()->shouldReceive('getKeyType')->once()->andReturn('int');
$relation->getQuery()->shouldReceive('whereInRaw')->once()->with('table.foreign_key', [1, 2]);
$relation->getQuery()->shouldReceive('whereInRawInt')->once()->with('table.foreign_key', [1, 2]);
$model1 = new EloquentHasOneModelStub;
$model1->id = 1;
$model2 = new EloquentHasOneModelStub;
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/DatabaseEloquentMorphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function testMorphManyEagerConstraintsAreProperlyAdded()
$relation = $this->getManyRelation();
$relation->getParent()->shouldReceive('getKeyName')->once()->andReturn('id');
$relation->getParent()->shouldReceive('getKeyType')->once()->andReturn('int');
$relation->getQuery()->shouldReceive('whereInRaw')->once()->with('table.morph_id', [1, 2]);
$relation->getQuery()->shouldReceive('whereInRawInt')->once()->with('table.morph_id', [1, 2]);
$relation->getQuery()->shouldReceive('where')->once()->with('table.morph_type', get_class($relation->getParent()));

$model1 = new EloquentMorphResetModelStub;
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/DatabaseEloquentMorphToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testEagerConstraintsAreProperlyAdded()
$relation = $this->getRelation();
$relation->getParent()->shouldReceive('getKeyName')->andReturn('id');
$relation->getParent()->shouldReceive('getKeyType')->andReturn('int');
$relation->getQuery()->shouldReceive('whereInRaw')->once()->with('taggables.taggable_id', [1, 2]);
$relation->getQuery()->shouldReceive('whereInRawInt')->once()->with('taggables.taggable_id', [1, 2]);
$relation->getQuery()->shouldReceive('where')->once()->with('taggables.taggable_type', get_class($relation->getParent()));
$model1 = new EloquentMorphToManyModelStub;
$model1->id = 1;
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,10 +693,10 @@ public function testEmptyWhereNotIns()
$this->assertEquals([0 => 1], $builder->getBindings());
}

public function testWhereInRaw()
public function testWhereInRawInt()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereInRaw('id', ['1a', 2]);
$builder->select('*')->from('users')->whereInRawInt('id', ['1a', 2]);
$this->assertEquals('select * from "users" where "id" in (1, 2)', $builder->toSql());
$this->assertEquals([], $builder->getBindings());
}
Expand Down

0 comments on commit cfd1693

Please sign in to comment.