PHP code example of bahaaodeh / firestore-php

1. Go to this page and download the library: Download bahaaodeh/firestore-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bahaaodeh / firestore-php example snippets


$firestoreClient = new FireStoreApiClient('project-id', 'AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', [
    'database' => '(default)',
]);

$firestoreClient->addDocument($collection, [
    'booleanTrue' => true,
    'booleanFalse' => false,
    'null' => null,
    'string' => 'abc123',
    'integer' => 123456,
    'arrayRaw' => [
        'string' => 'abc123',
    ],
    'array' => new FireStoreArray([
        'string' => 'abc123',
    ]),
    'reference' => new FireStoreReference('/users/23'),
    'object' => new FireStoreObject(['nested1' => new FireStoreObject(['nested2' => new FireStoreObject(['nested3' => 'test'])])]),
    'timestamp' => new FireStoreTimestamp,
    'geopoint' => new FireStoreGeoPoint(1,1),
]);

$document = new FireStoreDocument;
$document->setObject('sdf', new FireStoreObject(['nested1' => new FireStoreObject(['nested2' => new FireStoreObject(['nested3' => 'test'])])]));
$document->setBoolean('booleanTrue', true);
$document->setBoolean('booleanFalse', false);
$document->setNull('null', null);
$document->setString('string', 'abc123');
$document->setInteger('integer', 123456);
$document->setArray('arrayRaw', ['string'=>'abc123']);
$document->setArray('arrayObject', new FireStoreArray(['string' => 'abc123']));
$document->setTimestamp('timestamp', new FireStoreTimestamp);
$document->setGeoPoint('geopoint', new FireStoreGeoPoint(1.11,1.11));
$firestoreClient->addDocument($collection, $document, 'customDocumentId');

$document->fillValues([
    'string' => 'abc123',
    'boolean' => true,
]);

$firestoreClient->updateDocument($collection, $documentId, [
    'newFieldToAdd' => new FireStoreTimestamp(new DateTime('2018-04-20 15:00:00')),
    'existingFieldToRemove' => new FireStoreDeleteAttribute
], true);

$firestoreClient->setDocument($collection, $documentId, [
    'newFieldToAdd' => new FireStoreTimestamp(new DateTime('2018-04-20 15:00:00')),
    'existingFieldToRemove' => new FireStoreDeleteAttribute
], [
    'exists' => true, // Indicate document must exist
]);

$collection = 'collection/document/innerCollection';
$firestoreClient->deleteDocument($collection, $documentId);
bash
composer