Skip to content
Merged
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
20 changes: 5 additions & 15 deletions src/Phaseolies/Database/Entity/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2529,38 +2529,28 @@ protected function bindValuesForInsertOrUpdate(PDOStatement $stmt, array $attrib
*/
public function distinct(string $column): Collection
{
// Validate the column exists
if (!in_array($column, $this->getTableColumns())) {
throw new \InvalidArgumentException("Column {$column} does not exist in table {$this->table}");
}

// Build the distinct query
$sql = "SELECT DISTINCT {$column} FROM {$this->table}";

// Add WHERE conditions if any
if (!empty($this->conditions)) {
$conditionStrings = [];
foreach ($this->conditions as $condition) {
$conditionStrings[] = "{$condition[1]} {$condition[2]} ?";
}
$sql .= ' WHERE ' . implode(' ', $this->formatConditions($conditionStrings));
[$whereSql, $bindings] = $this->buildWhereClause();
if ($whereSql) {
$sql .= ' WHERE ' . $whereSql;
}

// Add ORDER BY if any
if (!empty($this->orderBy)) {
$orderByStrings = array_map(fn($o) => "$o[0] $o[1]", $this->orderBy);
$sql .= ' ORDER BY ' . implode(', ', $orderByStrings);
}

try {
$stmt = $this->pdo->prepare($sql);
$this->bindValues($stmt);
$this->bindAllValues($stmt);
$stmt->execute();

// Fetch just the column values
$results = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);

return new Collection('array', $results);
return new Collection('array', $stmt->fetchAll(PDO::FETCH_COLUMN, 0));
} catch (PDOException $e) {
throw new PDOException("Database error: " . $e->getMessage());
}
Expand Down
10 changes: 10 additions & 0 deletions src/Phaseolies/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -959,4 +959,14 @@ public function sole(?callable $callback = null): mixed

return $filtered->first();
}

/**
* Get all items in the collection
*
* @return array
*/
public function get(): array
{
return $this->data;
}
}
Loading