Domanda

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.

  • What is happening here?
  • Can I somehow ensure that the type is loaded earlier, or provide it in a configuration file? As far as I can tell there is no way, at present, using the MongoDB configuration to specify custom types in a .yml file as described for the ORM here.
È stato utile?

Soluzione

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.

Altri suggerimenti

To answer the second of my two questions above, I seem to have found a work-around for this, but I don't like it very much. It feels more like a hack than a proper solution.

In app/autoload.php after I register the annotation registry and driver, I call:

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

... and this appears to ensure that the type is registered when calling the console commands.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top