Skip to content

Commit

Permalink
Order by in delete
Browse files Browse the repository at this point in the history
  • Loading branch information
schtr4jh committed Jan 31, 2020
1 parent 2a1ada2 commit 38fa43d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/Pckg/Database/Query/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,19 @@ class Delete extends Query
function buildSQL()
{
$sql = "DELETE FROM `" . $this->table . "` " .
($this->where ? $this->buildWhere() : '') .
($this->limit ? 'LIMIT ' . $this->limit : '');
($this->where ? $this->buildWhere() : '');

return $sql;
$parts = [$sql];
if ($this->orderBy) {
$parts[] = 'ORDER BY ' . ($this->orderBy == 'id' ? '`' . $this->table . "`.`" . $this->orderBy .
'` ASC' : $this->orderBy);
}

if ($this->limit) {
$parts[] = 'LIMIT ' . $this->limit;
}

return implode(' ', $parts);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Pckg/Database/Query/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ public function transformToDelete()
foreach ($this->join as $join) {
$delete->join($join);
}
$order = $this->getOrderBy();
if ($order) {
$delete->orderBy($order);
}

return $delete;
}
Expand Down

0 comments on commit 38fa43d

Please sign in to comment.