Skip to content

Fix #175 : embedsMany duplicates are identified by key #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2014
Merged

Fix #175 : embedsMany duplicates are identified by key #180

merged 1 commit into from
Apr 8, 2014

Conversation

alexandre-butynski
Copy link
Contributor

The exists attribute is not enough reliable to detect duplicates in embedsMany documents. I just wrote a small contains() method that is used instead.

@jenssegers
Copy link
Contributor

Can you show me the code example when it would fail without the contains?

@alexandre-butynski
Copy link
Contributor Author

I wrote a failing test case before writing the code. The simple example below save two records with the same _id in the _addresses attribute :

$addressData = array('_id' => new MongoId);
$user->adresses()->create($addressData);
$user->adresses()->create($addressData);

This happens when data come from a form, for example when you update the existing addresses of a user.

@jenssegers
Copy link
Contributor

Why would you create objects like that?

@alexandre-butynski
Copy link
Contributor Author

I don't want to create objects, I want to save data in my database. These data come from a form filled by the user.

Something like that :

$user->adresses()->createMany(Input::get('addresses');

When the user want to update its addresses, a hidden field with the existing _id is sent with the text fields values in order to not erase and resave all the embedded items (of course, my use case is more complex than basic adresses).

@jenssegers
Copy link
Contributor

I'm going to merge this, it kind of makes sense to check primary keys.

jenssegers added a commit that referenced this pull request Apr 8, 2014
Fix #175 : embedsMany duplicates are identified by key
@jenssegers jenssegers merged commit b0a8e0d into mongodb:master Apr 8, 2014
@alexandre-butynski alexandre-butynski deleted the master-fix-duplicate-bug branch April 8, 2014 16:11
mnphpexpert added a commit to mnphpexpert/laravel-mongodb that referenced this pull request Sep 2, 2024
Fix mongodb#175 : embedsMany duplicates are identified by key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants