Skip to content

Feature request: MongoDB persistable #185

@miklcct

Description

@miklcct

I would like a trait which provides methods to implement MongoDB\BSON\Persistable such that I can store enums directly into a MongoDB database and load it as is. Specifically, I hope to have an implementation of BsonTrait included in the library:

#!/usr/bin/php
<?php
declare(strict_types=1);

use MongoDB\BSON\Persistable;
use MongoDB\Client;

require_once __DIR__ . '/vendor/autoload.php';

enum Test : string implements Persistable {
    use BsonTrait;

    case FOO = 'F';
    case BAR = 'B';
}

$client = new Client(driverOptions: ['typeMap' => ['array' => 'array']]);
$database = $client->selectDatabase('test');
$collection = $database->selectCollection('test');
$collection->insertMany([Test::FOO, Test::BAR]);
var_dump($collection->find()->toArray());

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions