CategoryModel::getTreeAsFlat PHP Method

getTreeAsFlat() public method

public getTreeAsFlat ( integer | string $id, integer | null $offset = null, integer | null $limit = null, string | null $filter = null, string $orderFields = 'Name', string $orderDirection = 'asc' ) : array
$id integer | string The parent category ID or slug.
$offset integer | null Offset results by given value.
$limit integer | null Total number of results should not exceed this value.
$filter string | null Restrict results to only those with names matching this value, if provided.
$orderFields string
$orderDirection string
return array
    public function getTreeAsFlat($id, $offset = null, $limit = null, $filter = null, $orderFields = 'Name', $orderDirection = 'asc')
    {
        $query = $this->SQL->from('Category')->where('DisplayAs <>', 'Heading')->where('ParentCategoryID', $id)->limit($limit, $offset)->orderBy($orderFields, $orderDirection);
        if ($filter) {
            $query->like('Name', $filter);
        }
        $categoryTree = $query->get()->resultArray();
        self::calculateData($categoryTree);
        self::joinUserData($categoryTree);
        foreach ($categoryTree as &$category) {
            // Fix the depth to be relative, not global.
            $category['Depth'] = 1;
            // We don't have children, but trees are expected to have this key.
            $category['Children'] = [];
        }
        return $categoryTree;
    }

Usage Example

 /**
  * Get a list of immediate children for the configured category.
  *
  * @return array|null
  */
 public function getChildren()
 {
     $category = $this->getCategory();
     if ($category && !$this->children) {
         $this->children = $this->categoryModel->getTreeAsFlat(val('CategoryID', $category), 0, $this->getLimit(), $this->getFilter());
         $this->categoryModel->joinRecent($this->children);
     }
     return $this->children;
 }
All Usage Examples Of CategoryModel::getTreeAsFlat