Skip to content

Commit 2272305

Browse files
committed
Adding order by to update and delete
1 parent 19830b1 commit 2272305

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

src/PHPFUI/ORM/Condition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public function getInput() : array
151151
if ($value instanceof \PHPFUI\ORM\Table)
152152
{
153153
$input = [];
154-
$sql = $value->getSQL($input);
154+
$sql = $value->getSelectSQL($input);
155155
$retVal = \array_merge($retVal, \PHPFUI\ORM::getValueArray($sql, $input));
156156
}
157157
elseif (\is_array($value))

src/PHPFUI/ORM/Operator/In.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public function __construct()
1111

1212
public function correctlyTyped(mixed $variable) : bool
1313
{
14-
return (\is_array($variable) && count($variable)) || $variable instanceof \PHPFUI\ORM\Table;
14+
return (\is_array($variable) && \count($variable)) || $variable instanceof \PHPFUI\ORM\Table;
1515
}
1616
}

src/PHPFUI/ORM/Operator/NotIn.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public function __construct()
1111

1212
public function correctlyTyped(mixed $variable) : bool
1313
{
14-
return (\is_array($variable) && count($variable)) || $variable instanceof \PHPFUI\ORM\Table;
14+
return (\is_array($variable) && \count($variable)) || $variable instanceof \PHPFUI\ORM\Table;
1515
}
1616
}

src/PHPFUI/ORM/Table.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,9 @@ public function delete(bool $allowDeleteAll = false) : static
301301
$this->lastInput = [];
302302
$where = $this->getWhere($this->lastInput);
303303
$limit = $this->getLimitClause();
304-
$this->lastSql = "DELETE FROM `{$table}`{$where}{$limit}";
304+
$orderBy = $this->getOrderBy();
305+
306+
$this->lastSql = "DELETE FROM `{$table}`{$where}{$orderBy}{$limit}";
305307
\PHPFUI\ORM::execute($this->lastSql, $this->lastInput);
306308

307309
return $this;
@@ -378,7 +380,7 @@ public static function getAllTables(array $skipTables = []) : array
378380
public function getArrayCursor() : \PHPFUI\ORM\ArrayCursor
379381
{
380382
$this->lastInput = [];
381-
$this->lastSql = $this->getSQL($this->lastInput);
383+
$this->lastSql = $this->getSelectSQL($this->lastInput);
382384

383385
$totalInput = [];
384386

@@ -391,7 +393,7 @@ public function getArrayCursor() : \PHPFUI\ORM\ArrayCursor
391393
public function getDataObjectCursor() : \PHPFUI\ORM\DataObjectCursor
392394
{
393395
$this->lastInput = [];
394-
$this->lastSql = $this->getSQL($this->lastInput);
396+
$this->lastSql = $this->getSelectSQL($this->lastInput);
395397
$totalInput = [];
396398

397399
return \PHPFUI\ORM::getDataObjectCursor($this->lastSql, $this->lastInput)->setCountSQL($this->getCountSQL($totalInput))->setTotalCountSQL($this->getTotalSQL($totalInput));
@@ -405,7 +407,7 @@ public function getDataObjectCursor() : \PHPFUI\ORM\DataObjectCursor
405407
public function getExplainRows() : array
406408
{
407409
$this->lastInput = [];
408-
$this->lastSql = 'explain ' . $this->getSQL($this->lastInput);
410+
$this->lastSql = 'explain ' . $this->getSelectSQL($this->lastInput);
409411

410412
return \PHPFUI\ORM::getRows($this->lastSql, $this->lastInput);
411413
}
@@ -585,7 +587,7 @@ public function getRecord() : \PHPFUI\ORM\Record
585587
public function getRecordCursor() : \PHPFUI\ORM\RecordCursor
586588
{
587589
$this->lastInput = [];
588-
$this->lastSql = $this->getSQL($this->lastInput);
590+
$this->lastSql = $this->getSelectSQL($this->lastInput);
589591

590592
$totalInput = [];
591593

@@ -598,15 +600,15 @@ public function getRecordCursor() : \PHPFUI\ORM\RecordCursor
598600
public function getRows() : array
599601
{
600602
$this->lastInput = [];
601-
$this->lastSql = $this->getSQL($this->lastInput);
603+
$this->lastSql = $this->getSelectSQL($this->lastInput);
602604

603605
return \PHPFUI\ORM::getRows($this->lastSql, $this->lastInput);
604606
}
605607

606608
/**
607609
* @return string the current select string, '*' if nothing specified, or a comma delimited field list
608610
*/
609-
public function getSelect() : string
611+
public function getSelectFields() : string
610612
{
611613
$sql = '';
612614
$comma = '';
@@ -676,10 +678,10 @@ public function getSelect() : string
676678
*
677679
* Sets up lastSql and lastInput variable for use in returning cursors
678680
*/
679-
public function getSQL(array &$input, bool $limited = true) : string
681+
public function getSelectSQL(array &$input, bool $limited = true) : string
680682
{
681683
$table = $this->instance->getTableName();
682-
$select = $this->getSelect();
684+
$select = $this->getSelectFields();
683685
$joins = $this->getJoins($input);
684686
$where = $this->getWhere($input);
685687
$groupBy = $this->getGroupBy();
@@ -699,7 +701,7 @@ public function getSQL(array &$input, bool $limited = true) : string
699701
{
700702
$sql .= 'ANY ';
701703
}
702-
$sql .= ' ' . $table->getSQL($input, $limited) . ' ';
704+
$sql .= ' ' . $table->getSelectSQL($input, $limited) . ' ';
703705
}
704706
}
705707
$sql .= $orderBy;
@@ -830,7 +832,7 @@ public function setFullJoinSelects(bool $fullSelects = true) : static
830832
*
831833
* @param bool $rollup can be applied to any group by field, but affects the entire group by clause
832834
*/
833-
public function setGroupBy(string $field, bool $rollup = false) : self
835+
public function setGroupBy(string $field, bool $rollup = false) : static
834836
{
835837
$this->groupBys = [];
836838

@@ -862,7 +864,7 @@ public function setOffset(int $offset) : static
862864
return $this;
863865
}
864866

865-
public function setOrderBy(string $field, string $ascending = 'ASC') : self
867+
public function setOrderBy(string $field, string $ascending = 'ASC') : static
866868
{
867869
$this->orderBys = [];
868870

@@ -983,9 +985,10 @@ public function update(array $variables) : static
983985
}
984986

985987
$where = $this->getWhere($this->lastInput);
988+
$orderBy = $this->getOrderBy();
986989
$limit = $this->getLimitClause();
987990

988-
$this->lastSql .= $where . $limit;
991+
$this->lastSql .= $where . $orderBy . $limit;
989992
\PHPFUI\ORM::execute($this->lastSql, $this->lastInput);
990993

991994
return $this;
@@ -1011,6 +1014,7 @@ public function updateFromTable(array $request) : bool
10111014
$data = [];
10121015

10131016
$record = new static::$className($existingKey);
1017+
10141018
foreach ($fields as $field => $typeInfo)
10151019
{
10161020
if (isset($request[$field]))
@@ -1056,7 +1060,7 @@ private function doTranslation(string $text) : string
10561060
*/
10571061
private function getCountSQL(array &$input) : string
10581062
{
1059-
return 'SELECT COUNT(*) from (' . $this->getSql($input) . ') countAlias';
1063+
return 'SELECT COUNT(*) from (' . $this->getSelectSQL($input) . ') countAlias';
10601064
}
10611065

10621066
/**
@@ -1090,6 +1094,6 @@ private function getTotalSQL(array &$input) : string
10901094
{
10911095
$input = [];
10921096

1093-
return 'SELECT COUNT(*) from (' . $this->getSql($input, false) . ') countAlias';
1097+
return 'SELECT COUNT(*) from (' . $this->getSelectSQL($input, false) . ') countAlias';
10941098
}
10951099
}

0 commit comments

Comments
 (0)