From 58a0e1b7e2bb6df3923883c4fc8cf13b1bce7322 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 7 Oct 2020 11:21:56 -0500 Subject: [PATCH] formatting --- src/Illuminate/Database/Eloquent/Builder.php | 89 +++++++++++--------- src/Illuminate/Database/Query/Builder.php | 4 +- 2 files changed, 48 insertions(+), 45 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index 6d4c054baf23..707386f77f15 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -822,11 +822,11 @@ public function upsert(array $values, $uniqueBy, $update = null) $update = array_keys(reset($values)); } - $values = $this->addTimestampsToValues($values); - - $update = $this->addUpdatedAtToColumns($update); - - return $this->toBase()->upsert($values, $uniqueBy, $update); + return $this->toBase()->upsert( + $this->addTimestampsToUpsertValues($values), + $uniqueBy, + $this->addUpdatedAtToUpsertColumns($update) + ); } /** @@ -859,13 +859,44 @@ public function decrement($column, $amount = 1, array $extra = []) ); } + /** + * Add the "updated at" column to an array of values. + * + * @param array $values + * @return array + */ + protected function addUpdatedAtColumn(array $values) + { + if (! $this->model->usesTimestamps() || + is_null($this->model->getUpdatedAtColumn())) { + return $values; + } + + $column = $this->model->getUpdatedAtColumn(); + + $values = array_merge( + [$column => $this->model->freshTimestampString()], + $values + ); + + $segments = preg_split('/\s+as\s+/i', $this->query->from); + + $qualifiedColumn = end($segments).'.'.$column; + + $values[$qualifiedColumn] = $values[$column]; + + unset($values[$column]); + + return $values; + } + /** * Add timestamps to the inserted values. * - * @param array $values + * @param array $values * @return array */ - protected function addTimestampsToValues(array $values) + protected function addTimestampsToUpsertValues(array $values) { if (! $this->model->usesTimestamps()) { return $values; @@ -873,7 +904,10 @@ protected function addTimestampsToValues(array $values) $timestamp = $this->model->freshTimestampString(); - $columns = array_filter([$this->model->getCreatedAtColumn(), $this->model->getUpdatedAtColumn()]); + $columns = array_filter([ + $this->model->getCreatedAtColumn(), + $this->model->getUpdatedAtColumn() + ]); foreach ($columns as $column) { foreach ($values as &$row) { @@ -887,10 +921,10 @@ protected function addTimestampsToValues(array $values) /** * Add the "updated at" column to the updated columns. * - * @param array $update + * @param array $update * @return array */ - protected function addUpdatedAtToColumns(array $update) + protected function addUpdatedAtToUpsertColumns(array $update) { if (! $this->model->usesTimestamps()) { return $update; @@ -898,44 +932,15 @@ protected function addUpdatedAtToColumns(array $update) $column = $this->model->getUpdatedAtColumn(); - if (! is_null($column) && ! array_key_exists($column, $update) && ! in_array($column, $update)) { + if (! is_null($column) && + ! array_key_exists($column, $update) && + ! in_array($column, $update)) { $update[] = $column; } return $update; } - /** - * Add the "updated at" column to an array of values. - * - * @param array $values - * @return array - */ - protected function addUpdatedAtColumn(array $values) - { - if (! $this->model->usesTimestamps() || - is_null($this->model->getUpdatedAtColumn())) { - return $values; - } - - $column = $this->model->getUpdatedAtColumn(); - - $values = array_merge( - [$column => $this->model->freshTimestampString()], - $values - ); - - $segments = preg_split('/\s+as\s+/i', $this->query->from); - - $qualifiedColumn = end($segments).'.'.$column; - - $values[$qualifiedColumn] = $values[$column]; - - unset($values[$column]); - - return $values; - } - /** * Delete records from the database. * diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 27d2d8e902ee..eb59daaef963 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -2935,9 +2935,7 @@ public function upsert(array $values, $uniqueBy, $update = null) { if (empty($values)) { return 0; - } - - if ($update === []) { + } elseif ($update === []) { return (int) $this->insert($values); }