Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Oct 7, 2020
1 parent 338ffa6 commit 58a0e1b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 45 deletions.
89 changes: 47 additions & 42 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

/**
Expand Down Expand Up @@ -859,21 +859,55 @@ 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;
}

$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) {
Expand All @@ -887,55 +921,26 @@ 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;
}

$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.
*
Expand Down
4 changes: 1 addition & 3 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 58a0e1b

Please sign in to comment.