doctrine / mongodb-odm

The Official PHP MongoDB ORM/ODM
https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/
MIT License
1.09k stars 502 forks source link

Invalid typemap provided. Type "array" is required for "root" #1873

Open bazo opened 5 years ago

bazo commented 5 years ago

Bug Report

Q A
BC Break don't know
Version dev-master#da5074b8b54f0e4c0d342d15c56dd054d4a136a7

Summary

ODM throws exception Invalid typemap provided. Type "array" is required for "root" but root is BSONDocument

MongoDB\Client #01
    manager private => MongoDB\Driver\Manager #02
    readConcern private => MongoDB\Driver\ReadConcern #03 
    readPreference private => MongoDB\Driver\ReadPreference #04
    mode => "primary"
    uri private => "redacted"
    typeMap private => array (3)
        array => "MongoDB\Model\BSONArray"
        document => "MongoDB\Model\BSONDocument"
        root => "MongoDB\Model\BSONDocument"

Current behavior

Exception is thrown

Expected behavior

No exception

malarzm commented 5 years ago

@bazo are you creating connection on your own? If yes then ODM requires certain settings of typeMap or will throw an exception as it couldn't work otherwise (as per #1871)

bazo commented 5 years ago

connection no, but client yes like this

$client = new Client($config['uri'], $config['mongoOptions']);
$dm = DocumentManager::create($client, $configuration, $evm);

it's actually this file https://github.com/bazo/nette-document-manager-extension/blob/master/src/Bazo/MongoDb/DI/DocumentManagerExtension.php#L134

malarzm commented 5 years ago

Yup, meant Client :) Leaving a note here so it won't get lost. @jmikola said:

I was going to suggest withOptions() to override the type map, but I realize now that's only available on the Database and Collection objects. I'm curious if ODM could also just work around this and pass a default type map when it creates those two objects from a Client, and then use the same default type map for any Client methods it needs to access directly (those are the only other things that would inherit the Client-level type map)

alcaeus commented 5 years ago

Quick update: this is currently waiting on https://github.com/mongodb/mongo-php-library/pull/599, which is most likely going to land in version 1.5.0 of mongodb/mongodb. There is DocumentManager::CLIENT_TYPEMAP which has to be used when manually creating a DocumentManager. Once we're able to require mongodb/mongodb 1.5.0 or newer (which will be in a future 2.x release), the exception will be removed, with the DocumentManager creating a copy of the client with the correct typeMap. Until then, I've created https://github.com/bazo/nette-document-manager-extension/pull/3 to work around this.

tyteen4a03 commented 3 years ago

Any updates on this?

alcaeus commented 3 years ago

Yes: we've decided to not implement a Client::withOptions helper in the MongoDB Driver, due to its implications on the underlying extensions. If you are creating your collections manually, you currently will have to pass the correct type map. We'll be working around this in a different way by passing a typeMap option to all find calls manually, which means that we can reuse existing connections that may have an incompatible typeMap set.