6

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.

Fatmuemoo
  • 2,187
  • 3
  • 17
  • 34

2 Answers2

1

You should validate your document before save it.

For example if user adding Contact with name="Name" and address="Address" you shoud check in mongodb if such Contact exists. And in case if it exists you just showing validation message, otherwise you adding contact to embedded contacts array.

So, suppose you have collection of users that's contains embedded array of contacts. To verify that new contact exists/not exists you can send request like this:

db.users.find({ userId: "userId" , 
                contacts.name: "new contact name", 
                contacts.address: "new contact address"}).count();

If above query will return count >= 1 you no need add new contact, just show validation.

Andrew Orsich
  • 52,935
  • 16
  • 139
  • 134
  • 1
    YES! I know how to do it using mongo. But can Doctrine do this for me? I don't want to put such logic in the entity, so where does it go? But I am really hoping that doctrine can do it for me. – Fatmuemoo Jun 17 '11 at 12:37
  • 1
    @Fatmuemoo: You can create some service and put validation logic there. I guess that doctrine can't do this for you, but i am not expert in doctrine. – Andrew Orsich Jun 19 '11 at 08:29
1

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.

Reuven
  • 3,336
  • 2
  • 23
  • 29
  • I am leaning towards doing this on the parent document... I am also going to investigate doing this in a custom repo class... that would preferable to doing it in the entity itself imo – Fatmuemoo Jun 22 '11 at 20:14
  • 1
    You're right, don't do that in the entity. I was talking about the 13.5 section on implating s event listener, and not about the lifecycle callbacks. – Reuven Jun 22 '11 at 21:34
  • Yes! I think this is what I'm looking for. Its an external class that can handle an event. I like it. – Fatmuemoo Jun 22 '11 at 23:53