Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3362948

Browse files
Merge 5.4 into 5.x (#3432)
2 parents fafc6f4 + 161cb8c commit 3362948

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

‎src/Query/Builder.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,10 @@ public function insert(array $values)
755755
$values = [$values];
756756
}
757757

758-
$values = $this->aliasIdForQuery($values);
758+
$values = array_map(
759+
$this->aliasIdForQuery(...),
760+
$values,
761+
);
759762

760763
$options = $this->inheritConnectionOptions();
761764

‎tests/ModelTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use MongoDB\Laravel\Tests\Models\IdIsString;
2626
use MongoDB\Laravel\Tests\Models\Item;
2727
use MongoDB\Laravel\Tests\Models\MemberStatus;
28+
use MongoDB\Laravel\Tests\Models\NonIncrementing;
2829
use MongoDB\Laravel\Tests\Models\Soft;
2930
use MongoDB\Laravel\Tests\Models\SqlUser;
3031
use MongoDB\Laravel\Tests\Models\User;
@@ -56,6 +57,7 @@ public function tearDown(): void
5657
Book::truncate();
5758
Item::truncate();
5859
Guarded::truncate();
60+
NonIncrementing::truncate();
5961

6062
parent::tearDown();
6163
}
@@ -106,6 +108,26 @@ public function testInsert(): void
106108
$this->assertEquals(35, $user->age);
107109
}
108110

111+
public function testInsertNonIncrementable(): void
112+
{
113+
$connection = DB::connection('mongodb');
114+
$connection->setRenameEmbeddedIdField(false);
115+
116+
$nonIncrementing = new NonIncrementing();
117+
$nonIncrementing->id = '12345';
118+
$nonIncrementing->name = 'John Doe';
119+
120+
$nonIncrementing->save();
121+
122+
$this->assertTrue($nonIncrementing->exists);
123+
$this->assertEquals(1, NonIncrementing::count());
124+
125+
$check = NonIncrementing::find($nonIncrementing->id);
126+
$this->assertInstanceOf(NonIncrementing::class, $check);
127+
$this->assertSame('12345', $check->id);
128+
$this->assertEquals('John Doe', $check->name);
129+
}
130+
109131
public function testUpdate(): void
110132
{
111133
$user = new User();

‎tests/Models/NonIncrementing.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Laravel\Tests\Models;
6+
7+
use Illuminate\Database\Eloquent\Model;
8+
use MongoDB\Laravel\Eloquent\DocumentModel;
9+
10+
/**
11+
* @property string $id
12+
* @property string $name
13+
*/
14+
class NonIncrementing extends Model
15+
{
16+
use DocumentModel;
17+
18+
protected $keyType = 'string';
19+
protected $connection = 'mongodb';
20+
21+
protected $fillable = [
22+
'name',
23+
];
24+
protected static $unguarded = true;
25+
public $incrementing = false;
26+
}

‎tests/QueryBuilderTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,22 @@ public function testInsert()
126126
$this->assertIsArray($user->tags);
127127
}
128128

129+
#[TestWith([true])]
130+
#[TestWith([false])]
131+
public function testInsertWithCustomId(bool $renameEmbeddedIdField)
132+
{
133+
$connection = DB::connection('mongodb');
134+
$connection->setRenameEmbeddedIdField($renameEmbeddedIdField);
135+
136+
$data = ['id' => 'abcdef', 'name' => 'John Doe'];
137+
138+
DB::table('users')->insert($data);
139+
140+
$user = User::find('abcdef');
141+
$this->assertInstanceOf(User::class, $user);
142+
$this->assertSame('abcdef', $user->id);
143+
}
144+
129145
public function testInsertGetId()
130146
{
131147
$id = DB::table('users')->insertGetId(['name' => 'John Doe']);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /