symfonydoctrine-odmcustom-typedoctrine-mongodb

Invalid Type Error Occasionally in Symfony 2 Console


I have a Symfony 2 environment in which I am using a custom data type with Doctrine's MongoDB ODM mappings. This all works, except occasionally; when I go to clear the cache or install the assets I sometimes receive the following error:

[InvalidArgumentException]
Invalid type specified "..."

This seems to always happen with the next command I issue to the console after I have cleared the cache, later operations all succeed. Doctrine seems to have issues intermittently finding it, and I suspect it has to do with where I'm registering the type and when that occurs with relationship to when Doctrine processes the mappings.

The type is being added as part of the boot() method in another bundle which may not always be included.


Solution

  • I found a good solution in this post.

    The short answer is to add

    \Doctrine\ODM\MongoDB\Mapping\Types\Type::registeredType('mytype', 'My\Type\Class');
    

    in MyBundleClass::__consruct(). This will get the type registered before any warmup happens with the cache.

    Using Type::registeredType() instead of Type::addType() will avoid checking to see if the type is already registered. In the case of Type::addType() will throw an exception if it has already been added.