Skip to content

Commit f6a3f19

Browse files
committed
wip
1 parent e096186 commit f6a3f19

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

packages/database/src/Builder/QueryBuilders/SelectQueryBuilder.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ public function chunk(Closure $closure, int $amountPerChunk = 200): void
123123
/** @return self<TModelClass> */
124124
public function where(string $where, mixed ...$bindings): self
125125
{
126+
if ($this->select->where->isNotEmpty()) {
127+
return $this->andWhere($where, ...$bindings);
128+
}
129+
126130
$this->select->where[] = new WhereStatement($where);
127131

128132
$this->bind(...$bindings);
@@ -132,7 +136,11 @@ public function where(string $where, mixed ...$bindings): self
132136

133137
public function andWhere(string $where, mixed ...$bindings): self
134138
{
135-
return $this->where("AND {$where}", ...$bindings);
139+
$this->select->where[] = new WhereStatement("AND {$where}");
140+
141+
$this->bind(...$bindings);
142+
143+
return $this;
136144
}
137145

138146
public function orWhere(string $where, mixed ...$bindings): self

tests/Integration/Database/Builder/SelectQueryBuilderTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,40 @@ public function test_select_from_model(): void
8080
$this->assertSameWithoutBackticks($expected, $sql);
8181
}
8282

83+
public function test_multiple_where(): void
84+
{
85+
$sql = query('books')->select()
86+
->where('title = ?', 'a')
87+
->where('author_id = ?', 1)
88+
->toSql();
89+
90+
$expected = <<<SQL
91+
SELECT *
92+
FROM `books`
93+
WHERE title = ?
94+
AND author_id = ?
95+
SQL;
96+
97+
$this->assertSameWithoutBackticks($expected, $sql);
98+
}
99+
100+
public function test_multiple_where_field(): void
101+
{
102+
$sql = query('books')->select()
103+
->whereField('title', 'a')
104+
->whereField('author_id', 1)
105+
->toSql();
106+
107+
$expected = <<<SQL
108+
SELECT *
109+
FROM `books`
110+
WHERE books.title = :title
111+
AND books.author_id = :author_id
112+
SQL;
113+
114+
$this->assertSameWithoutBackticks($expected, $sql);
115+
}
116+
83117
public function test_where_statement(): void
84118
{
85119
$this->migrate(

0 commit comments

Comments
 (0)