Skip to content

Commit dc0a97c

Browse files
committed
Support multiple calls to where() in finder
1 parent 825b276 commit dc0a97c

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

src/Finder.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class Finder
3333
private $type;
3434

3535
/**
36-
* @var string|null
36+
* @var string[]
3737
*/
38-
private $conditions;
38+
private $where = [];
3939

4040
/**
4141
* @var string
@@ -103,11 +103,28 @@ public function &where($pattern, ...$arguments)
103103
$conditions_to_prepare = array_merge($conditions_to_prepare, $arguments);
104104
}
105105

106-
$this->conditions = $this->connection->prepareConditions($conditions_to_prepare);
106+
$this->where[] = $this->connection->prepareConditions($conditions_to_prepare);
107107

108108
return $this;
109109
}
110110

111+
/**
112+
* Return where part of the query
113+
*/
114+
public function getWhere()
115+
{
116+
switch (count($this->where)) {
117+
case 0:
118+
return '';
119+
case 1:
120+
return $this->where[0];
121+
default:
122+
return implode(' AND ', array_map(function($condition) {
123+
return "($condition)";
124+
}, $this->where));
125+
}
126+
}
127+
111128
/**
112129
* @param string $order_by
113130
* @return $this
@@ -190,8 +207,8 @@ public function count()
190207
$sql .= " $this->join";
191208
}
192209

193-
if ($this->conditions) {
194-
$sql .= " WHERE $this->conditions";
210+
if ($where = $this->getWhere()) {
211+
$sql .= " WHERE $where";
195212
}
196213

197214
return $this->connection->executeFirstCell($sql);
@@ -234,7 +251,9 @@ public function first()
234251
*/
235252
public function ids()
236253
{
237-
return $this->connection->executeFirstColumn($this->getSelectIdsSql());
254+
$ids = $this->connection->executeFirstColumn($this->getSelectIdsSql());
255+
256+
return empty($ids) ? [] : $ids;
238257
}
239258

240259
/**
@@ -314,8 +333,8 @@ private function getSelectFieldsSql($escaped_field_names)
314333
$result .= " $this->join";
315334
}
316335

317-
if ($this->conditions) {
318-
$result .= " WHERE $this->conditions";
336+
if ($where = $this->getWhere()) {
337+
$result .= " WHERE $where";
319338
}
320339

321340
if ($this->order_by) {

test/src/FindTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ public function testFindAllIds()
8888
$this->assertCount(3, $ids);
8989
}
9090

91+
/**
92+
* Test if ids() returns an empty array on empty result set
93+
*/
94+
public function testFindIdsAlwaysReturnsArray()
95+
{
96+
$ids = $this->pool->find(Writer::class)->where('id = ?', -1)->ids();
97+
98+
$this->assertInternalType('array', $ids);
99+
$this->assertCount(0, $ids);
100+
}
101+
91102
/**
92103
* Test count using finder object
93104
*/
@@ -109,6 +120,32 @@ public function testFindByConditions()
109120
$this->assertEquals('Leo Tolstoy', $should_be_leo->getName());
110121
}
111122

123+
/**
124+
* Test find using multiple calls to where() method
125+
*/
126+
public function testFindByMultipleConditions()
127+
{
128+
$finder_1 = $this->pool->find(Writer::class)->where('`birthday` > ?', '1800-01-01');
129+
$this->assertEquals("`birthday` > '1800-01-01'", $finder_1->getWhere());
130+
131+
/** @var Writer[] $should_be_fyodor */
132+
$should_be_fyodor_and_leo = $finder_1->all();
133+
134+
$this->assertCount(2, $should_be_fyodor_and_leo);
135+
136+
$finder_2 = $this->pool->find(Writer::class)->where('`birthday` > ?', '1800-01-01')->where('birthday < ?', '1825-01-01');
137+
$this->assertEquals("(`birthday` > '1800-01-01') AND (birthday < '1825-01-01')", $finder_2->getWhere());
138+
139+
/** @var Writer[] $should_be_fyodor */
140+
$should_be_fyodor = $finder_2->all();
141+
142+
$this->assertCount(1, $should_be_fyodor);
143+
144+
$this->assertInstanceOf(Writer::class, $should_be_fyodor[0]);
145+
$this->assertTrue($should_be_fyodor[0]->isLoaded());
146+
$this->assertEquals('Fyodor Dostoyevsky', $should_be_fyodor[0]->getName());
147+
}
148+
112149
/**
113150
* @expectedException \InvalidArgumentException
114151
*/

0 commit comments

Comments
 (0)