Sokil\Mongo\GridFS::deleteFileById PHP Method

deleteFileById() public method

Delete file by id
public deleteFileById ( string | MongoId $id ) : GridFS
$id string | MongoId id of file's document
return GridFS
    public function deleteFileById($id)
    {
        if ($id instanceof \MongoId) {
            $result = $this->getMongoCollection()->delete($id);
        } else {
            try {
                $result = $this->getMongoCollection()->delete(new \MongoId($id));
            } catch (\MongoException $e) {
                $result = $this->getMongoCollection()->delete($id);
            }
        }
        if ($result['ok'] !== (double) 1) {
            throw new Exception('Error deleting file: ' . $result['err'] . ': ' . $result['errmsg']);
        }
        return $this;
    }

Usage Example

Example #1
0
 public function testDeleteById_MongoIdString()
 {
     $id = $this->gridFs->storeBytes('somebinarydata', array('meta1' => 1, 'meta2' => 2));
     $this->gridFs->deleteFileById((string) $id);
     $this->assertEquals(null, $this->gridFs->getFileById($id));
 }
All Usage Examples Of Sokil\Mongo\GridFS::deleteFileById