Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #47 from faryar76/1.0
Browse files Browse the repository at this point in the history
*new_feature: delete items with custom filter
  • Loading branch information
timothymarois committed Feb 21, 2019
2 parents 433190a + 5cd3f44 commit c694ff9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,5 +269,21 @@ public function toArray()


//--------------------------------------------------------------------


public function delete($input)
{
$items=$this->resultDocuments();
$condition=$input;
foreach($items as $item)
{
if(is_object($input))
{
$condition=$input($item);
}
if($condition)
{
$item->delete();
}
}
}
}
34 changes: 34 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1044,5 +1044,39 @@ public function testQueryFromCacheAfterSave()

$db->flush(true);
}
public function testDeleteWhereMatchItemsWithCustomFilter()
{
$db = new \Filebase\Database([
'dir' => 'path/to/users/users/test',
]);
$db->flush(true);

$a=0;
while($a < 15)
{
$user=$db->get($a."username");
$user->name = $a.'John';
$user->email = '[email protected]';
$user->tags = ['php','developer','html5'];

$user->save();
$a++;
}
$actual=$db->query()->results();
$this->assertCount(15,$actual);
$r=$db->query()->where('name','LIKE','john')->resultDocuments();
$this->assertInstanceOf(Document::class, $r[0]);

$db->query()->where('name','LIKE','john')->delete(function($item){

return $item->name=='0John';

});
$actual=$db->query()->where('name','LIKE','john')->resultDocuments();
$this->assertCount(14,$actual);
$db->flush(true);

}


}

0 comments on commit c694ff9

Please sign in to comment.