Silverstripe,仅显示某些类别

时间:2017-10-26 12:26:32

标签: silverstripe

我认为这是一个很长的镜头,但希望有人可以帮助我,下面是我认为获得所有产品的部分,但我只想显示某些类别而不是显示所有类别。这是一个使用简单'简单的主题的silverstripe网站。我从未与SS合作过,所以任何帮助都是超级的。

<?php

class ConceptPage extends Page
{
    static $has_one = array(
    );

    static $many_many = array(
        'Products' => 'Product'
    );

    static $allowed_children = array(
        'none' => 'none'
    );

    function getCMSFields()
    {
        $fields = parent::getCMSFields();
        return $fields;
    }
}

class ConceptPage_Controller extends Page_Controller
{
    static $allowed_actions = array(
        'show',
    );

    public function init()
    {
        parent::init();

        Requirements::css('products/css/products.css');
    }

    //Return the list of products for this category
    public function getProductsList()
    {
    /*
    $products =  $this->Products(Null, 'Sort ASC');
        foreach($products as $product){
            $productsArray[] = $this->Link() . 'show/' . $product->URLSegment;
        }
        if(count($productsArray) > 0){
            GoogleSitemap::register_routes($productsArray);
        }
        return $products;
    */
        return $this->Products(Null, 'Sort ASC');
    }
    public function getProductsListDE()
    {
        $products =  $this->Products(Null, 'Sort ASC');
        $productsList = new ArrayList();
        foreach ($products as $product) {
            $product->Title = $product->Title_DE;
            $product->SubTitle = $product->SubTitle_DE;
            $productsList->push($product);
        }

        return $productsList;
    }

    //Get's the current product from the URL, if any
    public function getCurrentProduct()
    {
        $Params = $this->getURLParams();
        $URLSegment = Convert::raw2sql($Params['ID']);

        if($URLSegment && $Product = DataObject::get_one('Product', "URLSegment = '" . $URLSegment . "'"))
        {
            return $Product;
        }
    }

    //Shows the Product detail page
    function show()
    {
        //Get the Product
        if($Product = $this->getCurrentProduct())
        {
            $Data = array(
                'Product' => $Product,
                'MetaTitle' => $Product->Title,
                'ExtraLink' => 'show/' . $Product->URLSegment
            );

            $this->addToBreadcrumbs($Product);
            //return our $Data array to use, rendering with the ProductPage.ss template
            return $this->customise($Data)->renderWith(array('ProductPage', 'Page'));
        }
        else //Product not found
        {
            return $this->httpError(404, 'Sorry that product could not be found');
        }
    }

}

1 个答案:

答案 0 :(得分:3)

这是一个非常开放和广泛的问题,但一般来说,假设您的产品有many_manyCategories,而Categorybelongs_many_many到{ {1}},它看起来像这样:

Product

我还会建议一些事情:

  • public function getProductsList() { $categoryID = $this->request->getVar('category'); if ($categoryID) { $category = Category::get()->byID($categoryID); if (!$category) return $this->httpError(404, 'Category not found'); return $category->Products(); } return $this->Products(Null, 'Sort ASC'); } 上添加Link()方法以生成Product个链接。
  • 'show/...上使用private static $default_sort = 'Sort ASC,而不是每次都对该排序进行硬编码。

相关:https://www.silverstripe.org/learn/lessons/creating-filtered-views