Firestore/src/WriteBatch.php
public function create($document, array $fields, array $options = [])
    {
        // Record whether the document is empty before any filtering.
        $emptyDocument = count($fields) === 0;

        list ($fields, $sentinels, $metadata) = $this->filterFields($fields);

        if ($metadata['hasDelete']) {
            throw new \InvalidArgumentException('Cannot delete fields when creating a document.');
        }

        // Cannot create a document that already exists!
        $precondition = ['exists' => false];

        // Enqueue an update operation if an empty document was provided,
        // or if there are still fields after filtering.
        $transformOptions = [];
        if (!empty($fields) || $emptyDocument) {
            $this->writes[] = $this->createDatabaseWrite(self::TYPE_UPDATE, $document, [
                'fields' => $this->valueMapper->encodeValues($fields),
                'precondition' => $precondition
            ] + $options);
        } else {
            // If no UPDATE mutation is enqueued, we need the precondition applied
            // to the transform mutation.
            $transformOptions = [
                'precondition' => $precondition
            ];
        }

        // document transform operations are enqueued as a separate mutation.
        $this->enqueueTransforms($document, $sentinels, $transformOptions);

        return $this;
    }
public function create($document, array $fields, array $options = [])
    {
        // Record whether the document is empty before any filtering.
        $emptyDocument = count($fields) === 0;

        list ($fields, $sentinels, $metadata) = $this->filterFields($fields);

        if ($metadata['hasDelete']) {
            throw new \InvalidArgumentException('Cannot delete fields when creating a document.');
        }

        // Cannot create a document that already exists!
        $precondition = ['exists' => false];

        // Enqueue an update operation if an empty document was provided,
        // or if there are still fields after filtering.
        $transformOptions = [];
        if (!empty($fields) || $emptyDocument) {
            $this->writes[] = $this->createDatabaseWrite(self::TYPE_UPDATE, $document, [
                'fields' => $this->valueMapper->encodeValues($fields),
                'precondition' => $precondition
            ] + $options);
        } else {
            // If no UPDATE mutation is enqueued, we need the precondition applied
            // to the transform mutation.
            $transformOptions = [
                'precondition' => $precondition
            ];
        }

        // document transform operations are enqueued as a separate mutation.
        $this->enqueueTransforms($document, $sentinels, $transformOptions);

        return $this;
    }
public function create ( $ document , array $ fields , array $ options = [ ] ) { // Record whether the document is empty before any filtering. $ emptyDocument = count ( $ fields ) === 0 ; list ( $ fields , $ sentinels , $ metadata ) = $ this -> filterFields ( $ fields ) ; if ( $ metadata [ 'hasDelete' ] ) { throw new \ InvalidArgumentException ( 'Cannot delete fields when creating a document.' ) ; } // Cannot create a document that already exists! $ precondition = [ 'exists' => false ] ; // Enqueue an update operation if an empty document was provided, // or if there are still fields after filtering. $ transformOptions = [ ] ; if ( ! empty ( $ fields ) || $ emptyDocument ) { $ this -> writes [ ] = $ this -> createDatabaseWrite ( self :: TYPE_UPDATE , $ document , [ 'fields' => $ this -> valueMapper -> encodeValues ( $ fields ) , 'precondition' => $ precondition ] + $ options ) ; } else { // If no UPDATE mutation is enqueued, we need the precondition applied // to the transform mutation. $ transformOptions = [ 'precondition' => $ precondition ] ; } // document transform operations are enqueued as a separate mutation. $ this -> enqueueTransforms ( $ document , $ sentinels , $ transformOptions ) ; return $ this ; }
Enqueue a document creation.

This operation will fail (when committed) if the document already exists.

Example:
```
$batch->create($documentName, [
'name' => 'John'
]);
```

@param DocumentReference|string $document The document to target, either
as a string document name, or DocumentReference object. Please
note that DocumentReferences will be used only for the document
name. Field data must be provided in the `$fields` argument.
@param array $fields An array containing fields, where keys are the field
names, and values are field values. Nested arrays are allowed.
Note that unlike {@see Google\Cloud\Firestore\DocumentReference::update()},
field paths are NOT supported by this method.
@param array $options Configuration options
@return WriteBatch
@throws \InvalidArgumentException If `FieldValue::deleteField()` is found in the fields list.
@throws \InvalidArgumentException If `FieldValue::serverTimestamp()` is found in an array value.
Enqueue a document creation .