PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

ServerGrove Blog: Enforcing unique key constrains with Doctrine ODM for MongoDB & Symfony 2

Note: This article was originally published at PHPDeveloper on 22 October 2010.
PHPDeveloper

New on the ServerGrove blog today is a quick post talking about how you can enforce key constraints on a MongoDb with Doctrine in Symfony.

Of course you can define unique indexes to prevent duplicate values and there are a couple of different syntax options to do so, which are clearly defined in the official documentation. But defining and creating the indexes is not enough. You must specify when you want to enforce the constrain, this is due to the fact that the PHP driver needs to tell MongoDB to throw an error when a duplicate key is found.

The fix is as simple as adding a "safe" option to the "flush()" call on your database object. If you give it a value of "true", the ORM is smart enough to handle things on its own.