Question

i'm doing a registration system for my site and want to prevent duplicate registrations with the same email address.

the declaration of the user class looks like this:

/**
 * @Document
 */
class User extends BaseEntity
{
    private 
    /**
     * @Id
     */
    $id,

    /**
     * @String @Index(unique=true)
     */    
    $email
;
}

but whenever i save a user with the same email, no exception is raised and i get a duplicate.

i found somewhere that i need to do $documentManager->flush(array('safe'=>true)); but that doesn't help.

How can I achieve what i need? Thanks

Was it helpful?

Solution

I had a similar problem. The index is not being created by Doctrine as you can see by typing the following in the mongo console:

db.system.indexes.find()

I had to create my index directly in mongo per these instructions. After that duplicates won't be created.

However Symfony2/Doctrine doesn't seem to throw any exceptions, the insert just fails silently. Mongodb DOES alert you of the failed insert if you do it directly in the console.

--edit: An exception is thrown when array('safe'=>true) is used as a parameter to flush() as per the original post.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top