Typo3 通过 CategoryCollection 的 parentID 获取记录

Getting records through parentID from CategoryCollection in Typo3

我正在使用 CategoryCollection 获取特定类别 ID 的记录,但问题是它只加载确切的类别,例如我有 parent > child 并且我已将子类别 ID 附加到a记录和我select子类,那么它显示我的记录就好了,但是如果我select parentID,那么它不显示子类记录。

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

如果我 select 来自 CategoryCollection 的父 ID,是否有任何内置方法来获取所有子类别的记录,或者我是否必须为此编写一些自定义内容?

遗憾的是,没有针对此类复杂选择的内置解决方案。您确实需要编写自己的逻辑,它可以像这样工作:

  1. 查找 parent 属于您的类别的类别
  2. 对找到的每个类别递归重复此操作,直到您不再为每个类别找到任何子项为止
  3. 使用类别 UID 列表进行自定义 IN() 查询

如果您有很深的类别树,类别 UID 列表可以放在 custom cache. You can use the root category UID or a hash thereof as key. These cache entries should be taggedsys_category 中。或者,您可以为列表中的每个类别 UID 添加一个 sys_category_<uid> 标签。这样可以确保只要其中一个类别发生变化,缓存条目就会被删除,您可以重建列表。