Firebase query filter multiple keys by array contain in PHP?

痞子三分冷 提交于 2021-01-07 01:09:58

问题


I am using this firebase PHP library

https://github.com/kreait/firebase-php

I have to filter data by keys from user table.

      $serviceAccount = ServiceAccount::fromJsonFile(__DIR__ . self::SECRET_FILE);
        $firebase = (new Factory)
       ->withServiceAccount($serviceAccount)
       ->withDatabaseUri(self::DATABASE_URL)
       ->create();
         $database = $firebase->getDatabase();


$userIds = ['xpCjiz6cuGSx4oBrEUIsn8cqxPy2','xpdXbPu3HdfQ48eZhk2wjUTJtq43','XpdxeLmlvhVABkBYDFyfcVI52Xa2'];
    forreach($userIds as $userId){
        $values = $database->getReference('users/'.$userId)
                    ->getValue();
                    echo "<pre>";
                    print_r($values);
    }

I have userids list and fetch all the user records based on random key so how can I fetch all the data in one request? My above codes works fine to fetch all the user data ,but Problem is I have to send multiple request based on keys. For example If I have 15 userids so I have to requests 15 times into firebase database to fetch user data.

Here is $userId is random values which is created in each row in table in firebase.

HOW CAN I FETCH ALL USER DATA IN ONE REQUEST BY FILTER MULTIPLE KEYS ?


回答1:


The only way to get multiple (but not all) child nodes with a single request is with a query. But you can't pass multiple IDs in a query.

Simply put: Firebase Realtime Database does not support a WHERE $key IN [key1, key2, key3] type query that you want to do here. You will have to perform a separate request for each ID.



来源:https://stackoverflow.com/questions/65165908/firebase-query-filter-multiple-keys-by-array-contain-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!