Silverstripe-显示父类别和子类别中的产品

Silverstripe-显示父类别和子类别中的产品,silverstripe,silverstripe-4,Silverstripe,Silverstripe 4,当我尝试循环父类别及其子类别中的所有产品时,我遇到了问题 我创建了两种额外的页面类型: 产品类别 产品 在SiteTree(页面)中,我创建了结构: |-Pages |- Dental equipment (productCategory) |----- Sub category (productCategory) |----- Sub sub category (productCategory)

当我尝试循环父类别及其子类别中的所有产品时,我遇到了问题

我创建了两种额外的页面类型:

  • 产品类别
  • 产品
  • SiteTree
    (页面)中,我创建了结构:

     |-Pages
        |- Dental equipment (productCategory)
               |----- Sub category (productCategory)
                          |----- Sub sub category (productCategory)
                                        |----- Sub sub sub category (productCategory)
                                               |----- Product 1 (product type)
                                               |----- Product 2 (product type)
                                               |----- Product 3 (product type)
                                               |----- Product 4 (product type)
    
    现在在
    ProductCategory\u Controller
    中,我创建了一个方法来循环所有不工作的产品

     public function Products()
        {
    
            $products = Product::get()->filter([
                'ParentID' => $this->ID
            ]);
    
            return  $products;
        }
    

    我的问题是如何获取属于所有parenet和子类别的所有产品?

    如果您试图获取当前类别页面和所有父类别页面的产品,那么下面的示例代码可能会为您指明正确的方向

    如果我误解了你的问题,请评论,我会更新我的答案

    <?php
    
    namespace {
        class ProductCategoryPageController extends PageController
        {
            public function ProductsInCurrentPageTree()
            {
                $currentPage = $this->data();
                $parentPage = $currentPage->getParent();
                if (!$parentPage) {
                    // No parent page, return the current page products
                    return ProductPage::get()->filter('ParentID', $currentPage->ID);
                }
    
                $pageTreeIds = [
                    $currentPage->ID,
                    $parentPage->ID,
                ];
    
                while ($parentPage = $parentPage->getParent()) {
                    // Any conditions to break out of the loop, for instance, if your category/product pages does not start from the root level
                    if (!$parentPage instanceof ProductCategoryPage) {
                        break;
                    }
                    $pageTreeIds[] = $parentPage->ID;
                }
                
                return ProductPage::get()->filterAny(['ParentID' => $pageTreeIds]);
            }
        }
    }
    

    递归地为子类别页面调用相同的函数,但如果没有子类别页面,则将产品添加到数组列表中。未经测试的代码,但应足够清晰:

    class ProductCategoryPageController extends PageController {
    
        public function ProductsRecursive($iParentID,$productList) {
            $productCategoryPages = ProductCategoryPage::get()->filter(['ParentID' => $iParentID]);
            if ($productCategoryPages) {
                //assuming product category pages only have children and no products themselves
                foreach ($productCategoryPages as $productCategoryPage) 
                    $productList = $this->ProductsRecursive($productCategoryPage->ID,$productList);
            }
            else {
                //assuming you only are collecting product pages that have no children
                foreach (ProductPage::get()->filter(['ParentID' => $iParentID]) as $productPage)
                    $productList->push($productPage);
            }
            return $productList;
        }
    
        public function Products() {
            return $this->ProductsRecursive($this->ID, ArrayList::create());
        }
    }