-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOCSP-35951: write operations delete docs #2829
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
Changes from 41 commits
8fbbcc4
a5a75ca
a678179
eea96e5
88024a5
24fdd13
77bdbec
831f06e
d813f23
a194e60
2582bd5
86ff663
4dc106a
678490e
8ff6872
44eb655
60a6cfb
a9d072c
79037d2
78bc0e7
1d77153
39eb362
dfe2019
ebf1cc5
2c339bd
f2b0412
cd7f08a
9ba28be
07530e6
e6c162f
4aa7e5c
bb3439b
25a3912
868d3a6
aee0efe
2512f78
78df112
a69a0e7
022622f
1018c36
af90cb7
8830948
d07cb68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.. note:: | ||
|
||
The ``orderBy()`` call sorts the results by the ``_id`` field to | ||
guarantee a consistent sort order. To learn more about sorting in MongoDB, | ||
see the :manual:`Natural order </reference/glossary/#std-term-natural-order>` | ||
glossary entry in the {+server-docs-name+}. |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -329,4 +329,197 @@ public function testModelPositional(): void | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$this->assertContains('contemporary', $result->genres); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$this->assertFalse(in_array('dance-pop', $result->genres)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public function testModelDeleteById(): void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
require_once __DIR__ . '/Concert.php'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::truncate(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$data = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'_id' => 'CH-0401242000', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Mitsuko Uchida', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Carnegie Hall', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'genres' => ['classical'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'ticketsSold' => 2121, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performanceDate' => new UTCDateTime(Carbon::create(2024, 4, 1, 20, 0, 0, 'EST')), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'_id' => 'MSG-0212252000', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Brad Mehldau', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Philharmonie de Paris', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'genres' => [ 'jazz', 'post-bop' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'ticketsSold' => 5745, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performanceDate' => new UTCDateTime(Carbon::create(2025, 2, 12, 20, 0, 0, 'CET')), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'_id' => 'MSG-021222000', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Billy Joel', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Madison Square Garden', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'genres' => [ 'rock', 'soft rock', 'pop rock' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'ticketsSold' => 12852, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performanceDate' => new UTCDateTime(Carbon::create(2025, 2, 12, 20, 0, 0, 'CET')), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'_id' => 'SF-06302000', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'The Rolling Stones', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Soldier Field', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'genres' => [ 'rock', 'pop', 'blues' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'ticketsSold' => 59527, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performanceDate' => new UTCDateTime(Carbon::create(2024, 6, 30, 20, 0, 0, 'CDT')), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::insert($data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$this->assertEquals(4, Concert::count()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$id = Concert::first()->id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// begin model delete by id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$id = 'MSG-0212252000'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::destroy($id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// end model delete by id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$this->assertEquals(3, Concert::count()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$this->assertNull(Concert::find($id)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public function testModelDeleteModel(): void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
require_once __DIR__ . '/Concert.php'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::truncate(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$data = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Mitsuko Uchida', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Carnegie Hall', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::insert($data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// begin delete one model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$concert = Concert::first(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$concert->delete(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// end delete one model | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$this->assertEquals(0, Concert::count()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public function testModelDeleteFirst(): void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
require_once __DIR__ . '/Concert.php'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::truncate(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$data = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Mitsuko Uchida', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Carnegie Hall', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Brad Mehldau', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Philharmonie de Paris', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Billy Joel', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Madison Square Garden', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'The Rolling Stones', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Soldier Field', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::insert($data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// begin model delete one fluent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::where('venue', 'Carnegie Hall') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
->orderBy('_id') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't get the point of ordering for deleting. In fact, it's ignored: laravel-mongodb/src/Query/Builder.php Lines 787 to 814 in 95c5b4f
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, so if the where() query filter matched multiple documents, wouldn't it be important to specify the sort order such that it's known which one will surface when calling limit(1), since that's the one that will be deleted? Does that mean that you can't use indexes with sort orders for the query filter part of the delete operation? If not possible, I should probably include that information. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's currently not possible. That could be a new feature PHPORM-169 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Note: from the server's perspective, there is no way to specify an order with a delete operation. Indexes can certainly be used (and hinted). In the case of a sparse index that might actually have an impact on which document(s) get selected for the deletion. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
->limit(1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
->delete(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// end model delete one fluent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$this->assertEquals(3, Concert::count()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public function testModelDeleteMultipleById(): void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
require_once __DIR__ . '/Concert.php'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::truncate(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$data = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'_id' => 3, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Mitsuko Uchida', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Carnegie Hall', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'_id' => 5, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Brad Mehldau', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Philharmonie de Paris', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'_id' => 7, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Billy Joel', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Madison Square Garden', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'_id' => 9, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'The Rolling Stones', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Soldier Field', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::insert($data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$concerts = Concert::all(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$ids = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$concerts = Concert::all()->pluck('_id')->all(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// begin model delete multiple by id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$ids = [3, 5, 7, 9]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::destroy($ids); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// end model delete multiple by id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$this->assertEquals(0, Concert::count()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
public function testModelDeleteMultiple(): void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
require_once __DIR__ . '/Concert.php'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::truncate(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$data = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Mitsuko Uchida', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Carnegie Hall', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'genres' => ['classical'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'ticketsSold' => 2121, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Brad Mehldau', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Philharmonie de Paris', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'genres' => [ 'jazz', 'post-bop' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'ticketsSold' => 5745, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'Billy Joel', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Madison Square Garden', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'genres' => [ 'rock', 'soft rock', 'pop rock' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'ticketsSold' => 12852, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'performer' => 'The Rolling Stones', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'venue' => 'Soldier Field', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'genres' => [ 'rock', 'pop', 'blues' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'ticketsSold' => 59527, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::insert($data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// begin model delete multiple fluent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Concert::where('ticketsSold', '>', 7500) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
->delete(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// end model delete multiple fluent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$this->assertEquals(2, Concert::count()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"permanently" is wrong when the
SoftDeletes
trait is used. But you mention it just below, so maybe that's okay.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for providing that perspective, I'll update the text to make that more clear!