bestelmar / firestore-php
Firestore PHP 客户端
v3.0.0
2020-10-16 13:06 UTC
Requires
- php: >=5.6.6
- ext-curl: *
- ext-json: *
Requires (Dev)
- phpunit/phpunit: 5.7
This package is not auto-updated.
Last update: 2024-09-29 07:01:19 UTC
README
本包完全基于 Firestore REST API
身份验证 / 生成 API 密钥
- 访问 Google Cloud Firestore API
- 选择您的项目。
- 从左侧菜单选择
凭据
,然后从服务器密钥中选择API 密钥
或创建自己的凭据
安装
您可以通过 composer 安装此包
composer require ahsankhatri/firestore-php
或者将其添加到 composer.json
中,然后运行 composer update
"require": { "ahsankhatri/firestore-php": "^2.0", }
依赖项
绑定需要以下扩展才能正常工作
如果您使用 Composer,这些依赖项应自动处理。如果您手动安装,请确保这些扩展可用。
使用方法
初始化
$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), ]);
注意: 如果您想设置自定义 文档 ID,则传递第三个参数;否则,将自动生成 ID。
或者
$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');
并且...
$document->fillValues([ 'string' => 'abc123', 'boolean' => true, ]);
插入/更新文档
- 更新(合并)或插入文档
以下操作将合并文档(如果存在),否则插入数据。
$firestoreClient->updateDocument($documentRoot, [ 'newFieldToAdd' => new FirestoreTimestamp(new DateTime('2018-04-20 15:00:00')), 'existingFieldToRemove' => new FirestoreDeleteAttribute ]);
注意: 将第三个参数传递为布尔值 true 将强制检查文档必须存在,反之亦然,以执行更新操作。
例如:如果您只想在存在时更新文档,否则将抛出 MrShan0\PHPFirestore\Exceptions\Client\NotFound
(异常)。
$firestoreClient->updateDocument($documentPath, [ '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);
带分页(或自定义参数)的文档列表
$collections = $firestoreClient->listDocuments('users', [ 'pageSize' => 1, 'pageToken' => 'nextpagetoken' ]);
注意: 您可以传递自定义参数,这些参数由 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 }
Firebase 身份验证
使用电子邮件和密码登录。
$firestoreClient ->authenticator() ->signInEmailPassword('testuser@example.com', 'abc123');
匿名登录。
$firestoreClient ->authenticator() ->signInAnonymously();
检索身份验证令牌
$authToken = $firestoreClient->authenticator()->getAuthToken();
待办事项
- 添加了删除属性支持。
- 添加对对象、布尔值、空值、字符串、整数、数组、时间戳、地理位置、字节数的支持
- 添加异常处理。
- 列出所有文档。
- 列出所有集合。
- 支持过滤和分页。
- 支持结构化查询。
- 支持事务。
- 支持索引。
- 支持整个集合的删除。
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全性
如果您发现任何安全相关的问题,请通过电子邮件 ahsankhatri1992@gmail.com 而不是使用问题跟踪器。
鸣谢
许可证
MIT 许可证(MIT)。请参阅 许可证文件 了解更多信息。