Skip to content

Commit

Permalink
PHPORM-243 Alias _id to id in Schema::getColumns
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Sep 20, 2024
1 parent b51aeff commit 844da1b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
20 changes: 17 additions & 3 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
use MongoDB\Model\IndexInfo;

use function array_fill_keys;
use function array_filter;
use function array_keys;
use function assert;
use function count;
use function current;
use function implode;
use function in_array;
use function iterator_to_array;
use function sort;
use function sprintf;
Expand All @@ -40,6 +42,13 @@ public function hasColumn($table, $column): bool
*/
public function hasColumns($table, array $columns): bool
{
// The field "id" (alias of "_id") is required for all MongoDB documents
$columns = array_filter($columns, fn (string $column): bool => ! in_array($column, ['_id', 'id'], true));

if ($columns === []) {
return true;
}

$collection = $this->connection->table($table);

return $collection
Expand Down Expand Up @@ -187,16 +196,21 @@ public function getColumns($table)
foreach ($stats as $stat) {
sort($stat->types);
$type = implode(', ', $stat->types);
$name = $stat->_id;
if ($name === '_id') {
$name = 'id';
}

$columns[] = [
'name' => $stat->_id,
'name' => $name,
'type_name' => $type,
'type' => $type,
'collation' => null,
'nullable' => $stat->_id !== '_id',
'nullable' => $name !== 'id',
'default' => null,
'auto_increment' => false,
'comment' => sprintf('%d occurrences', $stat->total),
'generation' => $stat->_id === '_id' ? ['type' => 'objectId', 'expression' => null] : null,
'generation' => $name === 'id' ? ['type' => 'objectId', 'expression' => null] : null,
];
}

Expand Down
11 changes: 9 additions & 2 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ public function testRenameColumn(): void

public function testHasColumn(): void
{
$this->assertTrue(Schema::hasColumn('newcollection', '_id'));
$this->assertTrue(Schema::hasColumn('newcollection', 'id'));

DB::connection()->table('newcollection')->insert(['column1' => 'value']);

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

public function testHasColumns(): void
{
$this->assertTrue(Schema::hasColumns('newcollection', ['_id']));
$this->assertTrue(Schema::hasColumns('newcollection', ['id']));

// Insert documents with both column1 and column2
DB::connection()->table('newcollection')->insert([
['column1' => 'value1', 'column2' => 'value2'],
Expand Down Expand Up @@ -451,8 +457,9 @@ public function testGetColumns()
$this->assertIsString($column['comment']);
});

$this->assertEquals('objectId', $columns->get('_id')['type']);
$this->assertEquals('objectId', $columns->get('_id')['generation']['type']);
$this->assertNull($columns->get('_id'), '_id is renamed to id');
$this->assertEquals('objectId', $columns->get('id')['type']);
$this->assertEquals('objectId', $columns->get('id')['generation']['type']);
$this->assertNull($columns->get('text')['generation']);
$this->assertEquals('string', $columns->get('text')['type']);
$this->assertEquals('date', $columns->get('date')['type']);
Expand Down

0 comments on commit 844da1b

Please sign in to comment.