public function persist($collection, array $document)
    {
        

        if ($collection instanceof Collection) {
            $collectionName = $collection->getName();
        } else {
            $collectionName = $collection;
            $collection = static::factory($collection);
        }
        
        $data = $this->marshall($document);
        $result = false;

        
        if (!isset($document['$id'])) {
            $id = $this->insert($collectionName, $data);
            if ($id) {
                $data['$id'] = $id;
                $result = $data;
            }
        } else {
            $data['id'] = $document['$id'];
            unset($data['$id']);
            $result = $this->update($collectionName, $data);

            if ($result) {
                $result = $data;
            }
        }

        return $this->unmarshall($result);
    }
public function persist($collection, array $document)
    {
        

        if ($collection instanceof Collection) {
            $collectionName = $collection->getName();
        } else {
            $collectionName = $collection;
            $collection = static::factory($collection);
        }
        
        $data = $this->marshall($document);
        $result = false;

        
        if (!isset($document['$id'])) {
            $id = $this->insert($collectionName, $data);
            if ($id) {
                $data['$id'] = $id;
                $result = $data;
            }
        } else {
            $data['id'] = $document['$id'];
            unset($data['$id']);
            $result = $this->update($collectionName, $data);

            if ($result) {
                $result = $data;
            }
        }

        return $this->unmarshall($result);
    }
public function persist ( $ collection , array $ document ) { if ( $ collection instanceof Collection ) { $ collectionName = $ collection -> getName ( ) ; } else { $ collectionName = $ collection ; $ collection = static :: factory ( $ collection ) ; } $ data = $ this -> marshall ( $ document ) ; $ result = false ; if ( ! isset ( $ document [ '$id' ] ) ) { $ id = $ this -> insert ( $ collectionName , $ data ) ; if ( $ id ) { $ data [ '$id' ] = $ id ; $ result = $ data ; } } else { $ data [ 'id' ] = $ document [ '$id' ] ; unset ( $ data [ '$id' ] ) ; $ result = $ this -> update ( $ collectionName , $ data ) ; if ( $ result ) { $ result = $ data ; } } return $ this -> unmarshall ( $ result ) ; }
Sync data to database. If it's new data, we insert it as new document, otherwise, if the document exists, we just update it.

@param Collection $collection
@param Model $model

@return bool
Sync data to database . If it s new data we insert it as new document otherwise if the document exists we just update it .