Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to count records? #25

Open
gfh31fgh2 opened this issue Aug 7, 2023 · 3 comments
Open

How to count records? #25

gfh31fgh2 opened this issue Aug 7, 2023 · 3 comments

Comments

@gfh31fgh2
Copy link

How to count records with this api?

@rafaelwendel
Copy link
Owner

Hello @gfh31fgh2

There is no specific function in the library to do this, but it is possible to obtain it through the count function

$db = $service->initializeDatabase('products', 'id');

try{
    $listProducts = $db->fetchAll()->getResult(); //fetch all products
    echo "Total: " . count($listProducts) . " rows on product table";
}
catch(Exception $e){
    echo $e->getMessage();
}

@gfh31fgh2
Copy link
Author

gfh31fgh2 commented Aug 7, 2023

Hello @gfh31fgh2

There is no specific function in the library to do this, but it is possible to obtain it through the count function

$db = $service->initializeDatabase('products', 'id');

try{
    $listProducts = $db->fetchAll()->getResult(); //fetch all products
    echo "Total: " . count($listProducts) . " rows on product table";
}
catch(Exception $e){
    echo $e->getMessage();
}

Thanks for answer! But for count > 100k, it isnt good function for this.
Trying to use javascript on same instance for counting special requests like

const { data, count } = supabase
  .from('countries')
  .select('*', { count: 'exact', head: true })

@reidsneo
Copy link

Hello @gfh31fgh2
There is no specific function in the library to do this, but it is possible to obtain it through the count function

$db = $service->initializeDatabase('products', 'id');

try{
    $listProducts = $db->fetchAll()->getResult(); //fetch all products
    echo "Total: " . count($listProducts) . " rows on product table";
}
catch(Exception $e){
    echo $e->getMessage();
}

Thanks for answer! But for count > 100k, it isnt good function for this. Trying to use javascript on same instance for counting special requests like

const { data, count } = supabase
  .from('countries')
  .select('*', { count: 'exact', head: true })

Hi, I struggle same issue, but after read the discussion here, I found out that the count parameter and response was calculated in header of request & response.

What I do is :

  1. Modify the request section to able handle count parameter
  2. In executeQuery under QueryBuilder.php must be able to handle the HEAD request
  3. In executeHttpRequest under Service.php you must be able to map header response of "Content-Range" to body section
  4. Viola now count result can be accessible

I don't recommend to use GET request to do this because it will render the body section, but use HEAD instead more lighter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants