@@ -50,6 +50,7 @@ public function __construct(DatabaseManager $db)
5050 * @param string $index
5151 * @param bool $raw
5252 * @return bool|int
53+ * @createdBy Mohammad Ghanbari <mavin.developer@gmail.com>
5354 * @updatedBy Ibrahim Sakr <ebrahimes@gmail.com>
5455 */
5556 public function update (Model $ table , array $ values , string $ index = null , bool $ raw = false )
@@ -119,7 +120,7 @@ public function update(Model $table, array $values, string $index = null, bool $
119120 $ cases = '' ;
120121 foreach ($ final as $ k => $ v ) {
121122 $ cases .= '" ' . $ k . '" = (CASE ' . implode ("\n" , $ v ) . "\n"
122- . 'ELSE " ' . $ k . '" END), ' ;
123+ . 'ELSE " ' . $ k . '" END), ' ;
123124 }
124125
125126 $ query = "UPDATE \"" . $ this ->getFullTableName ($ table ) . '" SET ' . substr ($ cases , 0 , -2 ) . " WHERE \"$ index \" IN(' " . implode ("',' " , $ ids ) . "'); " ;
@@ -129,14 +130,13 @@ public function update(Model $table, array $values, string $index = null, bool $
129130 $ cases = '' ;
130131 foreach ($ final as $ k => $ v ) {
131132 $ cases .= '` ' . $ k . '` = (CASE ' . implode ("\n" , $ v ) . "\n"
132- . 'ELSE ` ' . $ k . '` END), ' ;
133+ . 'ELSE ` ' . $ k . '` END), ' ;
133134 }
134135
135136 $ query = "UPDATE ` " . $ this ->getFullTableName ($ table ) . "` SET " . substr ($ cases , 0 , -2 ) . " WHERE ` $ index` IN( " . '" ' . implode ('"," ' , $ ids ) . '" ' . "); " ;
136137
137138 }
138139
139-
140140 return $ this ->db ->connection ($ this ->getConnectionName ($ table ))->update ($ query );
141141 }
142142
@@ -148,6 +148,7 @@ public function update(Model $table, array $values, string $index = null, bool $
148148 * @param string|null $index2
149149 * @param bool $raw
150150 * @return bool|int
151+ * @createdBy Mohammad Ghanbari <mavin.developer@gmail.com>
151152 * @updatedBy Ibrahim Sakr <ebrahimes@gmail.com>
152153 *
153154 * @desc
@@ -205,7 +206,7 @@ public function updateWithTwoIndex(Model $table, array $values, string $index =
205206 $ cases = '' ;
206207 foreach ($ final as $ k => $ v ) {
207208 $ cases .= '" ' . $ k . '" = (CASE ' . implode ("\n" , $ v ) . "\n"
208- . 'ELSE " ' . $ k . '" END), ' ;
209+ . 'ELSE " ' . $ k . '" END), ' ;
209210 }
210211
211212 $ query = "UPDATE \"" . $ this ->getFullTableName ($ table ) . '" SET ' . substr ($ cases , 0 , -2 ) . " WHERE \"$ index \" IN(' " . implode ("',' " , $ ids ) . "') AND \"$ index2 \" IN(' " . implode ("',' " , $ ids2 ) . "'); " ;
@@ -214,14 +215,135 @@ public function updateWithTwoIndex(Model $table, array $values, string $index =
214215 $ cases = '' ;
215216 foreach ($ final as $ k => $ v ) {
216217 $ cases .= '` ' . $ k . '` = (CASE ' . implode ("\n" , $ v ) . "\n"
217- . 'ELSE ` ' . $ k . '` END), ' ;
218+ . 'ELSE ` ' . $ k . '` END), ' ;
218219 }
219220 $ query = "UPDATE ` " . $ this ->getFullTableName ($ table ) . "` SET " . substr ($ cases , 0 , -2 ) . " WHERE ` $ index` IN( " . '" ' . implode ('"," ' , $ ids ) . '") ' . " AND ` $ index2` IN( " . '" ' . implode ('"," ' , $ ids2 ) . '" ' . " ); " ;
220221 }
221222
222223 return $ this ->db ->connection ($ this ->getConnectionName ($ table ))->update ($ query );
223224 }
224225
226+ /**
227+ * Update multiple condition rows
228+ * @param Model $table
229+ * @param array $arrays
230+ * @param string $keyName
231+ * @param bool $raw
232+ * @return bool|int
233+ * @createdBy Mohammad Ghanbari <mavin.developer@gmail.com>
234+ *
235+ * @desc
236+ * Example
237+ * $table = new \App\Models\User;
238+ * $arrays = [
239+ * [
240+ * 'conditions' => ['id' => 1, 'status' => 'active'],
241+ * 'columns' => [
242+ * 'status' => 'invalid'
243+ * 'nickname' => 'mohammad'
244+ * ],
245+ * ],
246+ * [
247+ * 'conditions' => ['id' => 2],
248+ * 'columns' => [
249+ * 'nickname' => 'mavinoo',
250+ * 'name' => 'mohammad',
251+ * ],
252+ * ],
253+ * [
254+ * 'conditions' => ['id' => 3],
255+ * 'columns' => [
256+ * 'nickname' => 'ali'
257+ * ],
258+ * ],
259+ * ];
260+ * $keyName = 'id';
261+ *
262+ */
263+ public function updateMultipleCondition (Model $ table , array $ arrays , string $ keyName = null , bool $ raw = false )
264+ {
265+ $ driver = $ table ->getConnection ()->getDriverName ();
266+ $ connectionName = $ this ->getConnectionName ($ table );
267+ $ tableName = $ this ->getFullTableName ($ table );
268+ $ timestamp = $ table ->usesTimestamps ();
269+ $ backtick = Common::disableBacktick ($ driver ) ? '` ' : '' ;
270+
271+ if (!count ($ arrays )) {
272+ return false ;
273+ }
274+
275+ if (!isset ($ keyName ) || empty ($ keyName )) {
276+ $ keyName = $ table ->getKeyName ();
277+ }
278+
279+ $ columns = [];
280+ $ conditionMaster = [];
281+ foreach ($ arrays as $ array ) {
282+ foreach ($ array ['conditions ' ] as $ keyCondition => $ condition ) {
283+ if ($ keyName == $ keyCondition and !in_array ($ condition , $ conditionMaster )) {
284+ $ conditionMaster [] = str (Common::mysql_escape ($ condition ))->toString ();
285+ }
286+ }
287+ foreach ($ array as $ key => $ item ) {
288+ if ($ key == 'columns ' ) {
289+ foreach ($ item as $ k => $ value ) {
290+ if (!in_array ($ key , $ columns )) {
291+ $ columns [$ k ] = $ k ;
292+ }
293+ }
294+ }
295+ }
296+ }
297+
298+ $ arraysNew = [];
299+ $ keys = array_keys ($ columns );
300+ foreach ($ keys as $ key ) {
301+ $ arraysMixed = collect ($ arrays )->filter (function ($ rows ) use ($ key ) {
302+ return in_array ($ key , array_keys ($ rows ['columns ' ]));
303+ });
304+
305+ foreach ($ arraysMixed as $ item ) {
306+ $ value = $ raw ? Common::mysql_escape ($ item ['columns ' ][$ key ]) : "' " . Common::mysql_escape ($ item ['columns ' ][$ key ]) . "' " ;
307+ $ arraysNew [$ key ][] = [
308+ 'conditions ' => $ item ['conditions ' ],
309+ 'value ' => is_null ($ item ['columns ' ][$ key ]) ? "NULL " : $ value ,
310+ ];
311+
312+ if ($ timestamp ) {
313+ $ arraysNew ['updated_at ' ][] = [
314+ 'conditions ' => $ item ['conditions ' ],
315+ 'value ' => "' " .(now ())."' " ,
316+ ];
317+ }
318+ }
319+ }
320+
321+ $ cases = [];
322+ foreach ($ arraysNew as $ key => $ items ) {
323+ $ caseSql = "{$ backtick }{$ key }{$ backtick } = (CASE " ;
324+ foreach ($ items as $ item ) {
325+ $ conditions = $ item ['conditions ' ];
326+ $ value = $ item ['value ' ];
327+
328+ $ conditionContext = [];
329+ foreach ($ conditions as $ conditionKey => $ condition ) {
330+ $ conditionContext [] = " {$ backtick }{$ conditionKey }{$ backtick } = ' {$ condition }' " ;
331+ }
332+
333+ $ conditionContext = join (' and ' , $ conditionContext );
334+ $ caseSql .= " WHEN $ conditionContext THEN {$ value } " ;
335+ }
336+ $ caseSql .= " ELSE {$ backtick }{$ key }{$ backtick } END) " ;
337+ $ cases [] = $ caseSql ;
338+ }
339+ $ caseSql = join (', ' , $ cases );
340+ $ conditionMaster = join (', ' , $ conditionMaster );
341+
342+ $ query = "update {$ backtick }{$ tableName }{$ backtick } set {$ caseSql } where {$ backtick }{$ keyName }{$ backtick } in ( {$ conditionMaster }) " ;
343+
344+ return $ this ->db ->connection ($ connectionName )->update ($ query );
345+ }
346+
225347 /**
226348 * Insert Multi rows.
227349 *
@@ -232,6 +354,7 @@ public function updateWithTwoIndex(Model $table, array $values, string $index =
232354 * @param bool $insertIgnore
233355 * @return bool|mixed
234356 * @throws \Throwable
357+ * @createdBy Mohammad Ghanbari <mavin.developer@gmail.com>
235358 * @updatedBy Ibrahim Sakr <ebrahimes@gmail.com>
236359 * @desc
237360 * Example
@@ -360,9 +483,9 @@ public function insert(Model $table, array $columns, array $values, int $batchSi
360483 }
361484
362485 return [
363- 'totalRows ' => $ totalValues ,
364- 'totalBatch ' => $ totalChunk ,
365- 'totalQuery ' => $ totalQuery
486+ 'totalRows ' => $ totalValues ,
487+ 'totalBatch ' => $ totalChunk ,
488+ 'totalQuery ' => $ totalQuery
366489 ];
367490 });
368491 }
0 commit comments