akeneo / api-php-client

PHP client of Akeneo PIM API

Home Page:https://packagist.org/packages/akeneo/api-php-client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue with "with_count" in conjunction with ListableResourceInterface

Toflar opened this issue · comments

Hey everybody,

I'm trying to get all categories and include their count. Unfortunately, that's not possible right now because the CategoryApi does not support passing the $withCount variable in all(). I understand that's an issue there because all() is part of the ListableResourceInterface so you cannot adjust that without breaking BC.

However, why is it not allowed to pass ['with_count' => true]? It should work pretty straight forward by doing the following:

    /**
     * {@inheritdoc}
     */
    public function all($pageSize = 10, array $queryParameters = [])
    {
+        $withCount = false;
+
+        if (array_key_exists('with_count', $queryParameters)) {
+            $withCount = (bool) $queryParameters['with_count'];
+            unset($queryParameters['with_count']);
+        }

+        $firstPage = $this->listPerPage($pageSize, $withCount, $queryParameters);
-        $firstPage = $this->listPerPage($pageSize, false, $queryParameters);

        return $this->cursorFactory->createCursor($pageSize, $firstPage);
    }

Any feedback on this? I'll happily work on a PR but I want to get some feedback on the concept :)

Hello and sorry for the late response.

  • First point:
    I don't understand how the "count" could be returned with the all method.
    It's a cursor returning the directly entities, not the page itself.

  • Second point:
    I strongly discourage you to do it this way for performance concern.
    The cursor is, under the hood, iterating by doing several requests. So, it would count each time for every requests. It can have an impact on the performance on pretty big catalogs.

I advise you to get the count with the listPerPage method, and then iterate over the categories with the all.

$categoryCount = $client->getProductApi()->listPerPage(1, true)->getCount();
$categories = $client->getCategoryApi()->all(100);

Thanks for the reply but I don't think you understood the issue exactly :-) As far as I understood from the API docs (https://api.akeneo.com/api-reference-21.html#get_categories), the with_count parameter adds the number of products per category to the result. It's not related to pagination or the cursor at all, is it?

What I need is the number of products per category so I can build something like this:

  • Category A (512)
    • Category C (18)
    • Category D (494)
  • Category B (32)

Hello,

No, this count is the total number of categories.

If you want to get the number of products in a category, you have to request the product endpoint with a filter IN CHILDREN:
https://api.akeneo.com/documentation/filter.html#on-categories

It's not possible to do it in 1 request for several categories though.

Ah, I see. Thank you for taking the time to reply then 👍