Search code examples
typo3typo3-7.6.xtypo3-8.xtypo3-extensions

Getting records through parentID from CategoryCollection in Typo3


I am using CategoryCollection to get the records of a specific category ID, but the problem is it only loads the exact category for e.g I have parent > child and I have attached child category ID to a record and I select child category, then it shows me the record fine, but if I select parentID, then it does not show the child category record.

$collection = \TYPO3\CMS\Frontend\Category\Collection\CategoryCollection::load(
        $categoryID,
        true,
        'tx_myextension_table_name',
        'categories'
    );

Is there any built-in way to get the records of all child category if I select parent ID from CategoryCollectionor do I have to write something custom for that?


Solution

  • Unfortunately there is no built-in solution for complex selections like this. You will indeed need to write your own logic which could work like this:

    1. Find categories whose parent is your category
    2. Repeat this recursively for every category found until you don't find any children for each category anymore
    3. Do a custom IN() query with the list of category UIDs

    If you have deep category trees, the list of category UIDs could be put in a custom cache. You can use the root category UID or a hash thereof as key. These cache entries should be tagged with sys_category. Alternatively you can add a sys_category_<uid> tag for every category UID in your list. This ensures that whenever something changes about one of the categories, the cache entries are dropped and you can rebuild the list.