PhpEnums icon indicating copy to clipboard operation
PhpEnums copied to clipboard

Feature request: MongoDB persistable

Open miklcct opened this issue 2 years ago • 0 comments

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());

miklcct avatar Apr 22 '22 22:04 miklcct