Skip to content

Commit 844da1b

Browse files
committed
PHPORM-243 Alias _id to id in Schema::getColumns
1 parent b51aeff commit 844da1b

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/Schema/Builder.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
use MongoDB\Model\IndexInfo;
1010

1111
use function array_fill_keys;
12+
use function array_filter;
1213
use function array_keys;
1314
use function assert;
1415
use function count;
1516
use function current;
1617
use function implode;
18+
use function in_array;
1719
use function iterator_to_array;
1820
use function sort;
1921
use function sprintf;
@@ -40,6 +42,13 @@ public function hasColumn($table, $column): bool
4042
*/
4143
public function hasColumns($table, array $columns): bool
4244
{
45+
// The field "id" (alias of "_id") is required for all MongoDB documents
46+
$columns = array_filter($columns, fn (string $column): bool => ! in_array($column, ['_id', 'id'], true));
47+
48+
if ($columns === []) {
49+
return true;
50+
}
51+
4352
$collection = $this->connection->table($table);
4453

4554
return $collection
@@ -187,16 +196,21 @@ public function getColumns($table)
187196
foreach ($stats as $stat) {
188197
sort($stat->types);
189198
$type = implode(', ', $stat->types);
199+
$name = $stat->_id;
200+
if ($name === '_id') {
201+
$name = 'id';
202+
}
203+
190204
$columns[] = [
191-
'name' => $stat->_id,
205+
'name' => $name,
192206
'type_name' => $type,
193207
'type' => $type,
194208
'collation' => null,
195-
'nullable' => $stat->_id !== '_id',
209+
'nullable' => $name !== 'id',
196210
'default' => null,
197211
'auto_increment' => false,
198212
'comment' => sprintf('%d occurrences', $stat->total),
199-
'generation' => $stat->_id === '_id' ? ['type' => 'objectId', 'expression' => null] : null,
213+
'generation' => $name === 'id' ? ['type' => 'objectId', 'expression' => null] : null,
200214
];
201215
}
202216

tests/SchemaTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ public function testRenameColumn(): void
374374

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

379382
$this->assertTrue(Schema::hasColumn('newcollection', 'column1'));
@@ -382,6 +385,9 @@ public function testHasColumn(): void
382385

383386
public function testHasColumns(): void
384387
{
388+
$this->assertTrue(Schema::hasColumns('newcollection', ['_id']));
389+
$this->assertTrue(Schema::hasColumns('newcollection', ['id']));
390+
385391
// Insert documents with both column1 and column2
386392
DB::connection()->table('newcollection')->insert([
387393
['column1' => 'value1', 'column2' => 'value2'],
@@ -451,8 +457,9 @@ public function testGetColumns()
451457
$this->assertIsString($column['comment']);
452458
});
453459

454-
$this->assertEquals('objectId', $columns->get('_id')['type']);
455-
$this->assertEquals('objectId', $columns->get('_id')['generation']['type']);
460+
$this->assertNull($columns->get('_id'), '_id is renamed to id');
461+
$this->assertEquals('objectId', $columns->get('id')['type']);
462+
$this->assertEquals('objectId', $columns->get('id')['generation']['type']);
456463
$this->assertNull($columns->get('text')['generation']);
457464
$this->assertEquals('string', $columns->get('text')['type']);
458465
$this->assertEquals('date', $columns->get('date')['type']);

0 commit comments

Comments
 (0)