Skip to content

Merge 5.2 into 5.x #3338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use MongoDB\Model\BSONDocument;

use function array_key_exists;
use function array_merge;
use function array_replace;
use function collect;
use function is_array;
use function is_object;
Expand Down Expand Up @@ -270,7 +270,7 @@ public function firstOrCreate(array $attributes = [], array $values = [])

// createOrFirst is not supported in transaction.
if ($this->getConnection()->getSession()?->isInTransaction()) {
return $this->create(array_merge($attributes, $values));
return $this->create(array_replace($attributes, $values));
}

return $this->createOrFirst($attributes, $values);
Expand All @@ -284,7 +284,7 @@ public function createOrFirst(array $attributes = [], array $values = [])
}

try {
return $this->create(array_merge($attributes, $values));
return $this->create(array_replace($attributes, $values));
} catch (BulkWriteException $e) {
if ($e->getCode() === self::DUPLICATE_KEY_ERROR) {
return $this->where($attributes)->first() ?? throw $e;
Expand All @@ -309,7 +309,7 @@ protected function addUpdatedAtColumn(array $values)
}

$column = $this->model->getUpdatedAtColumn();
$values = array_merge(
$values = array_replace(
[$column => $this->model->freshTimestampString()],
$values,
);
Expand Down
4 changes: 2 additions & 2 deletions src/Eloquent/DocumentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

use function array_key_exists;
use function array_keys;
use function array_merge;
use function array_replace;
use function array_unique;
use function array_values;
use function class_basename;
Expand Down Expand Up @@ -192,7 +192,7 @@ protected function transformModelValue($key, $value)
// to a Carbon or CarbonImmutable instance.
// @see Model::setAttribute()
if ($this->hasCast($key) && $value instanceof CarbonInterface) {
$value->settings(array_merge($value->getSettings(), ['toStringFormat' => $this->getDateFormat()]));
$value->settings(array_replace($value->getSettings(), ['toStringFormat' => $this->getDateFormat()]));

// "date" cast resets the time to 00:00:00.
$castType = $this->getCasts()[$key];
Expand Down
7 changes: 4 additions & 3 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use function array_key_exists;
use function array_map;
use function array_merge;
use function array_replace;
use function array_values;
use function assert;
use function blank;
Expand Down Expand Up @@ -426,7 +427,7 @@ public function toMql(): array

// Add custom query options
if (count($this->options)) {
$options = array_merge($options, $this->options);
$options = array_replace($options, $this->options);
}

$options = $this->inheritConnectionOptions($options);
Expand All @@ -450,7 +451,7 @@ public function toMql(): array

// Add custom projections.
if ($this->projections) {
$projection = array_merge($projection, $this->projections);
$projection = array_replace($projection, $this->projections);
}

$options = [];
Expand Down Expand Up @@ -484,7 +485,7 @@ public function toMql(): array

// Add custom query options
if (count($this->options)) {
$options = array_merge($options, $this->options);
$options = array_replace($options, $this->options);
}

$options = $this->inheritConnectionOptions($options);
Expand Down
4 changes: 2 additions & 2 deletions src/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use function array_diff;
use function array_keys;
use function array_map;
use function array_merge;
use function array_replace;
use function array_values;
use function assert;
use function count;
Expand Down Expand Up @@ -164,7 +164,7 @@ public function sync($ids, $detaching = true)
// Now we are finally ready to attach the new records. Note that we'll disable
// touching until after the entire operation is complete so we don't fire a
// ton of touch operations until we are totally done syncing the records.
$changes = array_merge(
$changes = array_replace(
$changes,
$this->attachNew($records, $current, false),
);
Expand Down
4 changes: 2 additions & 2 deletions src/Relations/MorphToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use function array_key_exists;
use function array_keys;
use function array_map;
use function array_merge;
use function array_reduce;
use function array_replace;
use function array_values;
use function collect;
use function count;
Expand Down Expand Up @@ -190,7 +190,7 @@ public function sync($ids, $detaching = true)
// Now we are finally ready to attach the new records. Note that we'll disable
// touching until after the entire operation is complete so we don't fire a
// ton of touch operations until we are totally done syncing the records.
$changes = array_merge(
$changes = array_replace(
$changes,
$this->attachNew($records, $current, false),
);
Expand Down
4 changes: 2 additions & 2 deletions src/Scout/ScoutEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
use function array_column;
use function array_flip;
use function array_map;
use function array_merge;
use function array_replace;
use function assert;
use function call_user_func;
use function class_uses_recursive;
Expand Down Expand Up @@ -117,7 +117,7 @@ public function update($models)

unset($searchableData['_id']);

$searchableData = array_merge($searchableData, $model->scoutMetadata());
$searchableData = array_replace($searchableData, $model->scoutMetadata());

/** Convert the __soft_deleted set by {@see Searchable::pushSoftDeleteMetadata()}
* into a boolean for efficient storage and indexing. */
Expand Down
33 changes: 33 additions & 0 deletions tests/Ticket/GH3335Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace MongoDB\Laravel\Tests\Ticket;

use MongoDB\Laravel\Tests\Models\Location;
use MongoDB\Laravel\Tests\TestCase;

/** @see https://github.com/mongodb/laravel-mongodb/discussions/3335 */
class GH3335Test extends TestCase
{
public function tearDown(): void
{
Location::truncate();

parent::tearDown();
}

public function testNumericalFieldName()
{
$model = new Location();
$model->id = 'foo';
$model->save();

$model = Location::find('foo');
$model->{'38'} = 'PHP';
$model->save();

$model = Location::find('foo');
self::assertSame('PHP', $model->{'38'});
}
}
Loading