Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

doctrine 2 ODM preventing duplicate record

Doctrine NOOB here, trying to figure out how to prevent a duplicate record in an embed many property. I have a EmbededDocment like this:

<?
/**
 * @EmbeddedDocument
 */
class Contact {
/**
 * @Id
 */
private $id;

/**
 * created timestamp
 * @Date
 */
private $created;

/**
 * modified timestamp
 * @Date
 */
private $modified;

/**
 * @String
 */
private $name;

 /**
 * @String
 */
private $name;

 /**
 * @String
 */
private $address;
 }

what I want to happen is when I add a new contact, two contacts can have the same name, two contacts can have the same address, but two contacts can not have the same name and address. When checking for duplicates, doctrine will need to ignore the $id, $created and $modified properties as these will almost always be distinct. It is the combination of all the other fields that must be unique. How can this be accomplished using doctrine? Does this logic belong in a service layer or can doctrine do it for me?

UPDATE: I do accept that Andrew's answer is the correct way to check for duplication using Mongo, I really want to know if doctrine can do this for me. Therefore, I'm starting a bounty.

like image 873
Fatmuemoo Avatar asked Jun 17 '11 01:06

Fatmuemoo


1 Answers

You could implement an event listener which will listen to an preUpdate and prePersist event. http://www.doctrine-project.org/docs/mongodb_odm/1.0/en/reference/events.html

In your event, you can do your own check.

like image 77
Reuven Avatar answered Oct 08 '22 09:10

Reuven