Skip to content

Commit c1b00d0

Browse files
authored
Merge pull request #792 from DirectoryTree/fix-select-v4
Bug 790 - select('cn', 'description') does not return 'description' attribute in v4
2 parents 710d7f7 + 8a2d53c commit c1b00d0

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

src/Query/Model/Builder.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,13 +569,20 @@ public function get(array|string $selects = ['*']): Collection
569569
/**
570570
* Select the given attributes to retrieve.
571571
*/
572-
public function select(array|string $selects): static
572+
public function select(array|string $selects = ['*']): static
573573
{
574+
$selects = is_array($selects) ? $selects : func_get_args();
575+
576+
// Default to all attributes if empty.
577+
if (empty($selects)) {
578+
$selects = ['*'];
579+
}
580+
574581
// If selects are being overridden, then we need to ensure
575582
// the GUID key is always selected so that it may be
576583
// returned in the results for model hydration.
577584
$selects = array_values(array_unique(
578-
array_merge([$this->model->getGuidKey()], (array) $selects)
585+
array_merge([$this->model->getGuidKey()], $selects)
579586
));
580587

581588
$this->query->select($selects);
@@ -588,6 +595,8 @@ public function select(array|string $selects): static
588595
*/
589596
public function addSelect(array|string $select): static
590597
{
598+
$select = is_array($select) ? $select : func_get_args();
599+
591600
$this->query->addSelect($select);
592601

593602
return $this;

tests/Unit/Query/Model/ActiveDirectoryBuilderTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,48 @@ public function test_built_where_disabled()
121121

122122
$this->assertEquals('(UserAccountControl:1.2.840.113556.1.4.803:=2)', $b->getQuery()->getQuery());
123123
}
124+
125+
public function test_select_with_variadic_arguments()
126+
{
127+
$b = $this->newBuilder();
128+
129+
$selects = $b->select('cn', 'description')->getSelects();
130+
131+
$this->assertContains('cn', $selects);
132+
$this->assertContains('description', $selects);
133+
$this->assertContains('objectguid', $selects); // GUID key always included
134+
}
135+
136+
public function test_select_with_array_argument()
137+
{
138+
$b = $this->newBuilder();
139+
140+
$selects = $b->select(['cn', 'description'])->getSelects();
141+
142+
$this->assertContains('cn', $selects);
143+
$this->assertContains('description', $selects);
144+
$this->assertContains('objectguid', $selects);
145+
}
146+
147+
public function test_select_with_empty_array_defaults_to_all()
148+
{
149+
$b = $this->newBuilder();
150+
151+
$selects = $b->select([])->getSelects();
152+
153+
$this->assertContains('*', $selects);
154+
$this->assertContains('objectguid', $selects);
155+
}
156+
157+
public function test_add_select_with_variadic_arguments()
158+
{
159+
$b = $this->newBuilder();
160+
161+
$selects = $b->select('cn')->addSelect('description', 'mail')->getSelects();
162+
163+
$this->assertContains('cn', $selects);
164+
$this->assertContains('description', $selects);
165+
$this->assertContains('mail', $selects);
166+
$this->assertContains('objectguid', $selects);
167+
}
124168
}

0 commit comments

Comments
 (0)