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

Adding array parameter support for where. #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,18 @@ $builder->from('table')->where('column', 'IN', function ($query) {
SELECT * FROM `table` WHERE `column` IN (SELECT `column` FROM `table`)
```

`where` accepts array parameter, which works similarly to closure.
```php
$builder->from('table')->where([
['column1', 'value1'],
['column2', 'value2'],
]);
```

```sql
SELECT * FROM `table` WHERE (`column1` = 'value1' AND `column2` = 'value2')
```

Also you can pass internal representation of this statement and it will be used. I will no talk about this with deeper
explanation because its not preferable way to use this.

Expand Down
27 changes: 23 additions & 4 deletions src/Query/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1037,15 +1037,34 @@ public function orPreWhereNotBetween($column, array $values)
/**
* Add where statement.
*
* @param TwoElementsLogicExpression|string|Closure|self $column
* @param mixed $operator
* @param mixed $value
* @param string $concatOperator
* @param TwoElementsLogicExpression|string|array|Closure|self $column
* @param mixed $operator
* @param mixed $value
* @param string $concatOperator
*
* @return static
*/
public function where($column, $operator = null, $value = null, string $concatOperator = Operator:: AND)
{
if (is_array($column)) {
// only support `and` and `or` concat
if ($concatOperator == Operator:: AND) {
$this->where(function ($query) use ($column) {
foreach ($column as $item) { // unpacking argument
$query->where(...$item);
}
});
} elseif ($concatOperator == Operator:: OR) {
$this->orWhere(function ($query) use ($column) {
foreach ($column as $item) { // unpacking argument
$query->where(...$item);
}
});
}

return $this;
}

list($value, $operator) = $this->prepareValueAndOperator($value, $operator, func_num_args() == 2);

$this->wheres[] = $this->assembleTwoElementsLogicExpression(
Expand Down
11 changes: 11 additions & 0 deletions tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,9 @@ public function test_wheres_basic()
});
$this->assertEquals('SELECT `column` FROM `table` WHERE (`column1` = 2 AND `column2` > 3)', $builder->toSql());

$builder = $this->getBuilder()->select('column')->from('table')->where([['column1', 2], ['column2', '>', 3]]);
$this->assertEquals('SELECT `column` FROM `table` WHERE (`column1` = 2 AND `column2` > 3)', $builder->toSql());

$builder = $this->getBuilder()->select('column')->from('table')->whereRaw('column = 2');
$this->assertEquals('SELECT `column` FROM `table` WHERE column = 2', $builder->toSql());

Expand All @@ -577,6 +580,14 @@ public function test_wheres_basic()
$builder = $this->getBuilder()->from('table')->where('column', 1)->orWhere('column2', 3);
$this->assertEquals('SELECT * FROM `table` WHERE `column` = 1 OR `column2` = 3', $builder->toSql());

$builder = $this->getBuilder()->from('table')->where('column', 1)->orWhere([['column2', '>', 3], ['column3', '<', 4]]);
$this->assertEquals('SELECT * FROM `table` WHERE `column` = 1 OR (`column2` > 3 AND `column3` < 4)', $builder->toSql());

$builder = $this->getBuilder()->from('table')->where('column', 1)->orWhere(function ($query) {
$query->where([['column2', '>', 3], ['column3', '<', 4]]);
});
$this->assertEquals('SELECT * FROM `table` WHERE `column` = 1 OR ((`column2` > 3 AND `column3` < 4))', $builder->toSql());

$builder = $this->getBuilder();
$element = new TwoElementsLogicExpression($builder);
$element->firstElement('a');
Expand Down