Skip to content
Advertisement

firebase php sdk not supported multi filter data

I am stuck to multi filter data in firebase with php sdk

$serviceAccount = ServiceAccount::fromJsonFile(__DIR__ . self::SECRET_FILE);
    $firebase = (new Factory)
   ->withServiceAccount($serviceAccount)
   ->withDatabaseUri(self::DATABASE_URL)
   ->create();
    $database = $firebase->getDatabase();
$values = $database->getReference('users')
            ->orderByChild('email')->equalTo('test@test.com')
            ->orderByChild('is_active')->equalTo('1')
            
            ->getValue();
            echo "<pre>";
            print_r($values);die();

Here I used two times orderByChild so it throws the error KreaitFirebaseExceptionDatabaseUnsupportedQuery: This query is already ordered. in C:wamp64wwwfirebase-diemvendorkreaitfirebase-phpsrcFirebaseDatabaseQuery.php on line <i>288</i></th>

Could you any suggest me that firebase php sdk support multi filter ? If yes how can I solve above error ?

Advertisement

Answer

This is not a limitation of the PHP SDK (or any other Firebase SDK) – Realtime Database queries only support sorting by one dimension. The SDK just saves you from making a request and getting an error or unexpected results.

If you need to sort by multiple dimensions, you either have to do it in your (PHP) code, or create a compound key/field in the database that you can sort by.

You could do the latter for example by introducing a new field in which the value is active_<email> or inactive_<email>.

Querying by this field in ascending order would give you the active users sorted by email first, followed by the inactive users sorted by email.

Querying by this field in descending order would give you the inactive users sorted by email first, followed by the active users sorted by email.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement