Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 493c826

Browse files
edruidahsankhatri
authored andcommitted
Fix notice when listing non-existant documents (#13)
* Fix notice when listing non-existant documents When listing documents with the parameter showMissing=true, non-existing documents (that has sub-objects) will show up but will not have createTime, updateTime of fields columns. This triggers an "Undefined index" notice which some frameworks (laravel for instance) escalates to an ErrorException. * Handle empty collections when listing documents
1 parent 0cdba84 commit 493c826

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/FirestoreDatabaseResource.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ public function listDocuments($collection, array $parameters = [], array $option
6060

6161
$response = $this->client->request('GET', 'documents/' . FirestoreHelper::normalizeCollection($collection), $options, $parameters);
6262

63-
$documents = array_map(function($doc) {
64-
return new FirestoreDocument($doc);
65-
}, $response['documents']);
63+
if (isset($response['documents'])) {
64+
$documents = array_map(function($doc) {
65+
return new FirestoreDocument($doc);
66+
}, $response['documents']);
67+
} else {
68+
$documents = [];
69+
}
6670

6771
return array_merge($response, [
6872
'documents' => $documents,

src/FirestoreDocument.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ public function __construct($object = null, $databaseResource = null)
3636
{
3737
if (null !== $object) {
3838
$this->name = $object['name'];
39-
$this->createTime = $object['createTime'];
40-
$this->updateTime = $object['updateTime'];
39+
$this->createTime = isset($object['createTime']) ? $object['createTime'] : null;
40+
$this->updateTime = isset($object['updateTime']) ? $object['updateTime'] : null;
4141

42-
foreach ($object['fields'] as $fieldName => $value) {
43-
$this->fields[ $fieldName ] = $value;
42+
if (isset($object['fields'])) {
43+
foreach ($object['fields'] as $fieldName => $value) {
44+
$this->fields[ $fieldName ] = $value;
45+
}
4446
}
4547
}
4648

0 commit comments

Comments
 (0)