Firebase PHP数据提交-arrayValue,mapValue

时间:2019-02-21 13:02:09

标签: php arrays google-cloud-firestore mapping

我正在尝试使用PHP和Google API将数据推送到Firestore数据库。

在我在网上看到的文档和示例中,我可以在发送数据时使用mapValuearrayValue

我使用的示例如下:-

[
    "orderName" => [
        "stringValue" => "Gbeila Aliu Wahab"
    ],
    "orderLocationName" => [
        "stringValue" => "Accra Mall Limited"
    ],
    "orderTotalAmount" => [
        "doubleValue" => 150.5
    ],
    "orderDescription" => [
        "stringValue" => "Lorem Ipsum is simply dummy text of the printing and typesetting industry"
    ],
    "orderLocationGeoPoints" => [
        "geoPointValue" => (object) [
            "latitude" => 5.5557, 
            "longitude" => -0.1963
        ]
    ],
    "orderStatus" => [
        "stringValue" => "NotAssigned"
    ],
]

这工作得很好,但是当我尝试发送对象或数组时,出现以下错误返回给我:-

"message": "Invalid JSON payload received. Unknown name \"map_value\" at 'document.fields[0].value': Proto field is not repeating, cannot start list.",

当尝试使用以下代码映射值时:-

"orderName" => [
    "mapValue" => ["Gbeila Aliu Wahab", 123]
]

// or

"orderName" => [
    "arrayValue" => [
        "first" => [
            "stringValue" => "test"
        ], 
        "second" => [
            "stringValue" => "test123"
        ]
    ]
]

我已经尝试了多种变体来尝试使它生效。

我应该如何使用mapValuearrayValue,我可以看到很多有关value选项的提及,但看不到任何有关如何使用它们的示例。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:4)

根据the documentation.,您要生成的数组或地图的有效载荷不正确,您需要将实际数据(要存储)包装在values键下,最终数组应为:

["orderName" => ["arrayValue" => ["values" => [["stringValue" => "test"], ["stringValue" => "test123"]]]]]

类似地,您的mapValue应该是

["orderName" => ["mapValue" => ["fields" => ["field1" => ["stringValue" => "Gbeila Aliu Wahab"]]]]]

此外,您可以通过this package.与其他数据映射器一起玩