Skip to content

Commit fb0a0ad

Browse files
committed
Fix issue 606
Related #606
1 parent 2a75f10 commit fb0a0ad

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/Query/Grammar.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,11 @@ protected function hasMultipleRawFilters(Builder $query): bool
128128
*/
129129
protected function shouldWrapEntireQueryInOr(Builder $query): bool
130130
{
131+
// If we have exactly one AND condition and one or more OR conditions, wrap the
132+
// entire query in OR to treat all conditions as alternatives. This handles
133+
// the common case where a single where() is followed by orWhere() calls.
131134
return count($query->filters['and'] ?? []) === 1
132-
&& count($query->filters['or'] ?? []) === 1
135+
&& ! empty($query->filters['or'])
133136
&& empty($query->filters['raw']);
134137
}
135138

tests/Unit/Query/BuilderTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,34 @@ public function test_built_where_and_or_wheres()
589589
$this->assertEquals('(|(field=value)(or=value))', $b->getUnescapedQuery());
590590
}
591591

592+
public function test_single_where_with_multiple_or_wheres_creates_single_or_filter()
593+
{
594+
// This test verifies the fix for GitHub issue #606
595+
// https://github.com/DirectoryTree/LdapRecord-Laravel/issues/606
596+
$b = $this->newBuilder()
597+
->where('memberof', '=', 'cn=Group1')
598+
->orWhere('memberof', '=', 'cn=Group2')
599+
->orWhere('memberof', '=', 'cn=Group3');
600+
601+
$this->assertEquals(
602+
'(|(memberof=cn=Group1)(memberof=cn=Group2)(memberof=cn=Group3))',
603+
$b->getUnescapedQuery()
604+
);
605+
}
606+
607+
public function test_single_where_with_single_or_where_creates_single_or_filter()
608+
{
609+
// This test verifies that the existing behavior for 1 where + 1 orWhere is preserved
610+
$b = $this->newBuilder()
611+
->where('field', '=', 'value1')
612+
->orWhere('field', '=', 'value2');
613+
614+
$this->assertEquals(
615+
'(|(field=value1)(field=value2))',
616+
$b->getUnescapedQuery()
617+
);
618+
}
619+
592620
public function test_built_where_has()
593621
{
594622
$b = $this->newBuilder();

0 commit comments

Comments
 (0)