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

bulkDelete() public method

Bulk Delete a collection of Models.
public bulkDelete ( array | collection $collection = [] ) : mixed
$collection array | collection
return mixed
    public function bulkDelete($collection = [])
    {
        $params = [];
        $defaultIndex = $this->connection->getDefaultIndex();
        foreach ($collection as $item) {
            $modelIndex = $item->getDocumentIndex();
            $params['body'][] = ['delete' => ['_id' => $item->getKey(), '_type' => $item->getDocumentType(), '_index' => $modelIndex ? $modelIndex : $defaultIndex]];
        }
        return $this->connection->bulkStatement($params);
    }

Usage Example

 /**
  * @test
  */
 public function it_deletes_models_data_in_bulk()
 {
     $connection = \Mockery::mock(Connection::class);
     $connection->shouldReceive('getDefaultIndex')->once()->andReturn('plastic');
     $model1 = new PersistenceModelTest();
     $model2 = new PersistenceModelTest();
     $collection = [$model1, $model2];
     $connection->shouldReceive('bulkStatement')->once()->with(['body' => [['delete' => ['_id' => null, '_type' => 'foo', '_index' => 'bar']], ['delete' => ['_id' => null, '_type' => 'foo', '_index' => 'bar']]]]);
     $persistence = new EloquentPersistence($connection);
     $persistence->bulkDelete($collection);
 }