Skip to content

Commit 58a6e47

Browse files
committed
✨ add increment() and decrement() methods
Signed-off-by: otengkwame <[email protected]>
1 parent aaf09e5 commit 58a6e47

File tree

1 file changed

+115
-12
lines changed

1 file changed

+115
-12
lines changed

Core/core/Models/EasyModel.php

Lines changed: 115 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,21 +1070,24 @@ public function outerJoin($table, $condition, $escape = null)
10701070
return $this;
10711071
}
10721072

1073+
//------------------ CodeIgniter Database Wrappers ------------------
10731074

1074-
//------------------ CodeIgniter Database Wrappers --------------------------------------------------
1075-
1076-
/**
1077-
* To allow for more expressive syntax, we provide wrapper functions
1078-
* for most of the query builder methods here.
1079-
*
1080-
* This allows for calls such as:
1081-
* $result = $this->model->select('...')
1082-
* ->where('...')
1083-
* ->having('...')
1084-
* ->find();
1085-
*
1075+
/*
1076+
| To allow for more expressive syntax, we provide
1077+
| wrapper functions for most of the query
1078+
| builder methods here and also some
1079+
| custom methods.
1080+
|
1081+
| This allows for calls such as:
1082+
| $result = $this->model->select('...')
1083+
| ->where('...')
1084+
| ->having('...')
1085+
| ->find();
1086+
|
10861087
*/
10871088

1089+
//--------------------------------------------------------------------
1090+
10881091
/**
10891092
* Pick function
10901093
* A sugared way of using select()
@@ -1099,6 +1102,105 @@ public function pick($select = '*', $escape = null)
10991102
return $this;
11001103
}
11011104

1105+
//--------------------------------------------------------------------
1106+
1107+
/**
1108+
* Increments the value of fields or multiple fields
1109+
* and their values by primary key of a specific table.
1110+
*
1111+
* @param mixed $id
1112+
* @param string|array $fields
1113+
* @param integer $value
1114+
* @param array $columns
1115+
* @return bool|CI_DB_query_builder
1116+
*/
1117+
public function increment(
1118+
mixed $id,
1119+
string|array $fields,
1120+
int|array $value = 1,
1121+
array $columns = []
1122+
) {
1123+
1124+
if (is_numeric($value)) {
1125+
$value = (int) abs($value);
1126+
}
1127+
1128+
$columns = is_array($value) ? $value : $columns;
1129+
1130+
$fieldsArray = is_array($fields);
1131+
1132+
$this->db->where($this->primaryKey, $id);
1133+
1134+
if ($fieldsArray) {
1135+
foreach ($fields as $field => $number) {
1136+
$this->db->set($field, "{$field}+{$number}", false);
1137+
}
1138+
}
1139+
1140+
if (!$fieldsArray && is_string($fields)) {
1141+
$this->db->set($fields, "{$fields}+{$value}", false);
1142+
}
1143+
1144+
if (!empty($columns) ) {
1145+
$this->db->set($columns, false);
1146+
}
1147+
1148+
$result = $this->db->update($this->table);
1149+
1150+
return ($fieldsArray) ? $result : $this;
1151+
1152+
}
1153+
1154+
//--------------------------------------------------------------------
1155+
1156+
/**
1157+
* Decrements the value of fields or multiple fields
1158+
* and their values by primary key of a specific table.
1159+
*
1160+
* @param mixed $id
1161+
* @param string|array $fields
1162+
* @param integer $value
1163+
* @param array $columns
1164+
* @return bool|CI_DB_query_builder
1165+
*/
1166+
public function decrement(
1167+
mixed $id,
1168+
string|array $fields,
1169+
int|array $value = 1,
1170+
array $columns = []
1171+
) {
1172+
if (is_numeric($value)) {
1173+
$value = (int) abs($value);
1174+
}
1175+
1176+
$columns = is_array($value) ? $value : $columns;
1177+
1178+
$fieldsArray = is_array($fields);
1179+
1180+
$this->db->where($this->primaryKey, $id);
1181+
1182+
if ($fieldsArray) {
1183+
foreach ($fields as $field => $number) {
1184+
$this->db->set($field, "{$field}-{$number}", false);
1185+
}
1186+
}
1187+
1188+
if (!$fieldsArray && is_string($fields)) {
1189+
$this->db->set($fields, "{$fields}-{$value}", false);
1190+
}
1191+
1192+
if (!empty($columns) ) {
1193+
$this->db->set($columns, false);
1194+
}
1195+
1196+
$result = $this->db->update($this->table);
1197+
1198+
return ($fieldsArray) ? $result : $this;
1199+
1200+
}
1201+
1202+
//--------------------------------------------------------------------
1203+
11021204
/**
11031205
* Select function
11041206
*
@@ -1454,5 +1556,6 @@ public function set($key, $value = '', $escape = true)
14541556
$this->db->set($key, $value, $escape);
14551557
return $this;
14561558
}
1559+
14571560
}
14581561
/* end of file Core/core/Models/EasyModel.php */

0 commit comments

Comments
 (0)