Skip to content

Commit 716d8e1

Browse files
authored
PHPORM-243 Alias _id to id in Schema::getColumns() (#3160)
* PHPORM-243 Alias _id to id in Schema::getColumns * Support hasColumn for nested id
1 parent 38dc1e3 commit 716d8e1

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44
## [5.1.0] - next
55

66
* Convert `_id` and `UTCDateTime` in results of `Model::raw()` before hydratation by @GromNaN in [#3152](https://github.com/mongodb/laravel-mongodb/pull/3152)
7+
* Alias `_id` to `id` in `Schema::getColumns()` by @GromNaN in [#3160](https://github.com/mongodb/laravel-mongodb/pull/3160)
78

89
## [5.0.2] - 2024-09-17
910

src/Schema/Builder.php

+23-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@
99
use MongoDB\Model\IndexInfo;
1010

1111
use function array_fill_keys;
12+
use function array_filter;
1213
use function array_keys;
14+
use function array_map;
1315
use function assert;
1416
use function count;
1517
use function current;
1618
use function implode;
19+
use function in_array;
1720
use function iterator_to_array;
1821
use function sort;
1922
use function sprintf;
23+
use function str_ends_with;
24+
use function substr;
2025
use function usort;
2126

2227
class Builder extends \Illuminate\Database\Schema\Builder
@@ -40,6 +45,16 @@ public function hasColumn($table, $column): bool
4045
*/
4146
public function hasColumns($table, array $columns): bool
4247
{
48+
// The field "id" (alias of "_id") always exists in MongoDB documents
49+
$columns = array_filter($columns, fn (string $column): bool => ! in_array($column, ['_id', 'id'], true));
50+
51+
// Any subfield named "*.id" is an alias of "*._id"
52+
$columns = array_map(fn (string $column): string => str_ends_with($column, '.id') ? substr($column, 0, -3) . '._id' : $column, $columns);
53+
54+
if ($columns === []) {
55+
return true;
56+
}
57+
4358
$collection = $this->connection->table($table);
4459

4560
return $collection
@@ -187,16 +202,21 @@ public function getColumns($table)
187202
foreach ($stats as $stat) {
188203
sort($stat->types);
189204
$type = implode(', ', $stat->types);
205+
$name = $stat->_id;
206+
if ($name === '_id') {
207+
$name = 'id';
208+
}
209+
190210
$columns[] = [
191-
'name' => $stat->_id,
211+
'name' => $name,
192212
'type_name' => $type,
193213
'type' => $type,
194214
'collation' => null,
195-
'nullable' => $stat->_id !== '_id',
215+
'nullable' => $name !== 'id',
196216
'default' => null,
197217
'auto_increment' => false,
198218
'comment' => sprintf('%d occurrences', $stat->total),
199-
'generation' => $stat->_id === '_id' ? ['type' => 'objectId', 'expression' => null] : null,
219+
'generation' => $name === 'id' ? ['type' => 'objectId', 'expression' => null] : null,
200220
];
201221
}
202222

tests/SchemaTest.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -374,14 +374,22 @@ public function testRenameColumn(): void
374374

375375
public function testHasColumn(): void
376376
{
377-
DB::connection()->table('newcollection')->insert(['column1' => 'value']);
377+
$this->assertTrue(Schema::hasColumn('newcollection', '_id'));
378+
$this->assertTrue(Schema::hasColumn('newcollection', 'id'));
379+
380+
DB::connection()->table('newcollection')->insert(['column1' => 'value', 'embed' => ['_id' => 1]]);
378381

379382
$this->assertTrue(Schema::hasColumn('newcollection', 'column1'));
380383
$this->assertFalse(Schema::hasColumn('newcollection', 'column2'));
384+
$this->assertTrue(Schema::hasColumn('newcollection', 'embed._id'));
385+
$this->assertTrue(Schema::hasColumn('newcollection', 'embed.id'));
381386
}
382387

383388
public function testHasColumns(): void
384389
{
390+
$this->assertTrue(Schema::hasColumns('newcollection', ['_id']));
391+
$this->assertTrue(Schema::hasColumns('newcollection', ['id']));
392+
385393
// Insert documents with both column1 and column2
386394
DB::connection()->table('newcollection')->insert([
387395
['column1' => 'value1', 'column2' => 'value2'],
@@ -451,8 +459,9 @@ public function testGetColumns()
451459
$this->assertIsString($column['comment']);
452460
});
453461

454-
$this->assertEquals('objectId', $columns->get('_id')['type']);
455-
$this->assertEquals('objectId', $columns->get('_id')['generation']['type']);
462+
$this->assertNull($columns->get('_id'), '_id is renamed to id');
463+
$this->assertEquals('objectId', $columns->get('id')['type']);
464+
$this->assertEquals('objectId', $columns->get('id')['generation']['type']);
456465
$this->assertNull($columns->get('text')['generation']);
457466
$this->assertEquals('string', $columns->get('text')['type']);
458467
$this->assertEquals('date', $columns->get('date')['type']);

0 commit comments

Comments
 (0)