Sleimanx2\Plastic\Persistence\EloquentPersistence::delete PHP Method

delete() public method

Delete a model document.
public delete ( ) : mixed
return mixed
    public function delete()
    {
        $this->exitIfModelNotSet();
        $params = ['id' => $this->model->getKey(), 'type' => $this->model->getDocumentType(), 'index' => $this->model->getDocumentIndex()];
        // check if the document exists before deleting
        if ($this->connection->existsStatement($params)) {
            return $this->connection->deleteStatement($params);
        }
        return true;
    }

Usage Example

 /**
  * @test
  */
 public function it_dosent_execute_a_delete_statement_if_model_document_not_indexed()
 {
     $connection = \Mockery::mock(Connection::class);
     $model = new PersistenceModelTest();
     $model->exists = true;
     $connection->shouldReceive('existsStatement')->once()->with(['id' => null, 'type' => 'foo', 'index' => 'bar'])->andReturn(false);
     $connection->shouldNotReceive('deleteStatement');
     $persistence = new EloquentPersistence($connection);
     $persistence->model($model);
     $persistence->delete();
 }