Skip to content
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

Support delete one document with the query builder #2591

Merged
merged 2 commits into from
Sep 1, 2023

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Aug 31, 2023

Fix #2502

MongoDB PHP Library supports deleting one or all documents only. So we cannot accept other limits than null (unlimited) or 1.

@@ -703,7 +703,14 @@ public function delete($id = null)
$wheres = $this->compileWheres();
$options = $this->inheritConnectionOptions();

$result = $this->collection->deleteMany($wheres, $options);
if ($this->limit) {
if ($this->limit !== 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should limit === 0 be supported? Or would Eloquent expect that to be a NOP?

I suppose the notion of limit: 0 meaning "delete all" is an implementation detail of MongoDB's delete command and not a general concept, so I'm fine with not supporting that.

I do wonder if we should have a separate exception message if 0 is specified that advises users to specify null instead (or not call limit() at all), as this message might confuse a user with more experience with MongoDB than Eloquent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right. I've also banned 0.

@GromNaN GromNaN merged commit ad79fb1 into mongodb:master Sep 1, 2023
9 checks passed
@GromNaN GromNaN deleted the delete-one branch September 1, 2023 08:52
@GromNaN
Copy link
Member Author

GromNaN commented Sep 4, 2023

Related issue: #2597

GromNaN added a commit that referenced this pull request Sep 5, 2023
Custom implementation of MassPrunable is required to prevent using the limit. #2591 added an exception when limited is used because MongoDB Delete operation doesn't support it.

- MassPrunable::pruneAll() is called by the command model:prune.
- Using the parent trait is required because it's used to detect prunable models.
- Prunable feature was introducted in Laravel 8.x by laravel/framework#37889.

Users have to be aware that MassPrunable can break relationships as it doesn't call model methods to remove links.
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.

Deleting With Limit Doesn't Work
3 participants