Commit fa4a5351 authored by wppd's avatar wppd Committed by Jens Segers

Fix query builder delete (#1130)

* fix query builder delete

* tweak a bit

* tweak the test
parent 163f7260
...@@ -611,6 +611,13 @@ class Builder extends BaseBuilder ...@@ -611,6 +611,13 @@ class Builder extends BaseBuilder
*/ */
public function delete($id = null) public function delete($id = null)
{ {
// If an ID is passed to the method, we will set the where clause to check
// the ID to allow developers to simply and quickly remove a single row
// from their database without manually specifying the where clauses.
if (! is_null($id)) {
$this->where('_id', '=', $id);
}
$wheres = $this->compileWheres(); $wheres = $this->compileWheres();
$result = $this->collection->DeleteMany($wheres); $result = $this->collection->DeleteMany($wheres);
if (1 == (int) $result->isAcknowledged()) { if (1 == (int) $result->isAcknowledged()) {
......
...@@ -11,6 +11,40 @@ class QueryBuilderTest extends TestCase ...@@ -11,6 +11,40 @@ class QueryBuilderTest extends TestCase
DB::collection('items')->truncate(); DB::collection('items')->truncate();
} }
public function testDeleteWithId()
{
$user = DB::collection('users')->insertGetId([
['name' => 'Jane Doe', 'age' => 20],
]);
$user_id = (string) $user;
DB::collection('items')->insert([
['name' => 'one thing', 'user_id' => $user_id],
['name' => 'last thing', 'user_id' => $user_id],
['name' => 'another thing', 'user_id' => $user_id],
['name' => 'one more thing', 'user_id' => $user_id],
]);
$product = DB::collection('items')->first();
$pid = (string) ($product['_id']);
DB::collection('items')->where('user_id', $user_id)->delete($pid);
$this->assertEquals(3, DB::collection('items')->count());
$product = DB::collection('items')->first();
$pid = $product['_id'];
DB::collection('items')->where('user_id', $user_id)->delete($pid);
DB::collection('items')->where('user_id', $user_id)->delete(str_random(32));
$this->assertEquals(2, DB::collection('items')->count());
}
public function testCollection() public function testCollection()
{ {
$this->assertInstanceOf('Jenssegers\Mongodb\Query\Builder', DB::collection('users')); $this->assertInstanceOf('Jenssegers\Mongodb\Query\Builder', DB::collection('users'));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment