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

save() public method

Save a model instance.
public save ( ) : mixed
return mixed
    public function save()
    {
        $this->exitIfModelNotSet();
        if (!$this->model->exists) {
            throw new \Exception('Model not persisted yet');
        }
        $document = $this->model->getDocumentData();
        $params = ['id' => $this->model->getKey(), 'type' => $this->model->getDocumentType(), 'index' => $this->model->getDocumentIndex(), 'body' => $document];
        return $this->connection->indexStatement($params);
    }

Usage Example

 /**
  * @test
  */
 public function it_throw_an_exception_if_trying_to_save_a_model_with_exits_false()
 {
     $connection = \Mockery::mock(Connection::class);
     $model = new PersistenceModelTest();
     $model->exists = false;
     $this->setExpectedException('Exception');
     $persistence = new EloquentPersistence($connection);
     $persistence->model($model);
     $persistence->save();
 }