-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from WalterWoshid/master
Fixed increment/decrement bad syntax
- Loading branch information
Showing
2 changed files
with
42 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,30 +19,37 @@ public function __construct(DatabaseManager $db) | |
} | ||
|
||
/** | ||
* Update multiple rows. | ||
* <h2>Update multiple rows.</h2> | ||
* | ||
* @param Model $table | ||
* @param array $values | ||
* @param string $index | ||
* @param bool $raw | ||
* @return bool|int | ||
* @updatedBy Ibrahim Sakr <[email protected]> | ||
* @desc | ||
* Example | ||
* $table = 'users'; | ||
* Example:<br> | ||
* ``` | ||
* $userInstance = new \App\Models\User; | ||
* $value = [ | ||
* [ | ||
* 'id' => 1, | ||
* 'status' => 'active', | ||
* 'nickname' => 'Mohammad' | ||
* ] , | ||
* ], | ||
* [ | ||
* 'id' => 5, | ||
* 'status' => 'deactive', | ||
* 'nickname' => 'Ghanbari' | ||
* ] , | ||
* ], | ||
* [ | ||
* 'id' => 7, | ||
* 'balance' => ['+', 500] | ||
* ] | ||
* ]; | ||
* $index = 'id'; | ||
* Batch::update($userInstance, $value, $index); | ||
* ``` | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $table | ||
* @param array $values | ||
* @param string $index | ||
* @param bool $raw | ||
* @return bool|int | ||
* @updatedBy Ibrahim Sakr <[email protected]> | ||
*/ | ||
public function update(Model $table, array $values, string $index = null, bool $raw = false) | ||
{ | ||
|
@@ -73,12 +80,28 @@ public function update(Model $table, array $values, string $index = null, bool $ | |
|
||
foreach (array_keys($val) as $field) { | ||
if ($field !== $index) { | ||
if (gettype($val[$field]) == 'string' && !empty($val[$field]) && str_replace(['+', '-', '*', '/', '%'], '', $val[$field][0]) !== $val[$field][0]) { | ||
$value = '`' . $field . '`' . $val[$field]; | ||
// If increment / decrement | ||
if (gettype($val[$field]) == 'array') { | ||
// If array has two values | ||
if (!array_key_exists(0, $val[$field]) || !array_key_exists(1, $val[$field])) { | ||
throw new \ArgumentCountError('Increment/Decrement array needs to have 2 values, a math operator (+, -, *, /, %) and a number'); | ||
} | ||
// Check first value | ||
if (gettype($val[$field][0]) != 'string' || !in_array($val[$field][0], ['+', '-', '*', '/', '%'])) { | ||
throw new \TypeError('First value in Increment/Decrement array needs to be a string and a math operator (+, -, *, /, %)'); | ||
} | ||
// Check second value | ||
if (!is_numeric($val[$field][1])) { | ||
throw new \TypeError('Second value in Increment/Decrement array needs to be numeric'); | ||
} | ||
// Increment / decrement | ||
$value = '`' . $field . '`' . $val[$field][0] . $val[$field][1]; | ||
} else { | ||
// Only update | ||
$finalField = $raw ? Common::mysql_escape($val[$field]) : "'" . Common::mysql_escape($val[$field]) . "'"; | ||
$value = (is_null($val[$field]) ? 'NULL' : $finalField); | ||
} | ||
|
||
if ($driver == 'pgsql') | ||
$final[$field][] = 'WHEN ' . $index . ' = \'' . $val[$index] . '\' THEN ' . $value . ' '; | ||
else | ||
|