This package is totally based on Firestore REST API
- Visit Google Cloud Firestore API
- Select your desired project.
- Select
Credentials
from left menu and selectAPI Key
from Server key orCreate your own credentials
You can install the package via composer:
composer require ahsankhatri/firestore-php
or install it by adding it to composer.json
then run composer update
"require": {
"ahsankhatri/firestore-php": "^2.0",
}
The bindings require the following extensions in order to work properly:
If you use Composer, these dependencies should be handled automatically. If you install manually, you'll want to make sure that these extensions are available.
$firestoreClient = new FirestoreClient('project-id', 'AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', [
'database' => '(default)',
]);
$firestoreClient->addDocument($collection, [
'booleanTrue' => true,
'booleanFalse' => false,
'null' => null,
'string' => 'abc123',
'integer' => 123456,
'arrayRaw' => [
'string' => 'abc123',
],
'bytes' => new FirestoreBytes('bytesdata'),
'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),
]);
NOTE: Pass third argument if you want your custom document id to set else auto-id will generate it for you.
Or
$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->setBytes('bytes', new FirestoreBytes('bytesdata'));
$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');
And..
$document->fillValues([
'string' => 'abc123',
'boolean' => true,
]);
- Update (Merge) or Insert document
Following will merge document (if exist) else insert the data.
$firestoreClient->updateDocument($documentRoot, [
'newFieldToAdd' => new FirestoreTimestamp(new DateTime('2018-04-20 15:00:00')),
'existingFieldToRemove' => new FirestoreDeleteAttribute
]);
NOTE: Passing 3rd argument as a boolean true will force check that document must exist and vice-versa in order to perform update operation.
For example: If you want to update document only if exist else MrShan0\PHPFirestore\Exceptions\Client\NotFound
(Exception) will be thrown.
$firestoreClient->updateDocument($documentPath, [
'newFieldToAdd' => new FirestoreTimestamp(new DateTime('2018-04-20 15:00:00')),
'existingFieldToRemove' => new FirestoreDeleteAttribute
], true);
- Overwirte or Insert document
$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);
$collections = $firestoreClient->listDocuments('users', [
'pageSize' => 1,
'pageToken' => 'nextpagetoken'
]);
Note: You can pass custom parameters as supported by firestore list document
$document->get('bytes')->parseValue(); // will return bytes decoded value.
// Catch field that doesn't exist in document
try {
$document->get('allowed_notification');
} catch (\MrShan0\PHPFirestore\Exceptions\Client\FieldNotFound $e) {
// Set default value
}
$firestoreClient
->authenticator()
->signInEmailPassword('[email protected]', 'abc123');
$firestoreClient
->authenticator()
->signInAnonymously();
$authToken = $firestoreClient->authenticator()->getAuthToken();
- Added delete attribute support.
- Add Support for Object, Boolean, Null, String, Integer, Array, Timestamp, GeoPoint, Bytes
- Add Exception Handling.
- List all documents.
- List all collections.
- Filters and pagination support.
- Structured Query support.
- Transaction support.
- Indexes support.
- Entire collection delete support.
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.