Category entity

Phproberto\Joomla\Entity\Categories\Category

Represents a com_categories category.

Index

Usage

To start using this entity you have to load the phproberto_library and add the use statement like:

\JLoader::import('phproberto_entity.library');

use Phproberto\Joomla\Entity\Categories\Category;

// Load the category with id = 1
$category = Category::instance(1);

Methods

This entity has these methods available:

Traits

This class allows you to use methods defined in these traits:

searchAncestors(array $options = [])

Search category ancestors. Parent category and parents of the parent category.

Parameters:

  • array $options (optional): Search options. For filters, limit, ordering, etc.

Returns:

\Phproberto\Joomla\Entity\Collection

Examples:

$category = Category::instance(34);

// Search the category ancestor of first level
$ancestors = $category->searchAncestors(['filter.level' => 1]);

searchChildren(array $options = [])

Search category children. These are direct child categories.

Parameters:

  • array $options (optional): Search options. For filters, limit, ordering, etc.

Returns:

\Phproberto\Joomla\Entity\Collection

Examples:

$category = Category::instance(34);

// Get first 5 category children
$childCategories = $category->searchChildren(['list.limit' => 5]);

searchDescendants(array $options = [])

Search category descendants. These are direct child categories and their subcategories.

Parameters:

  • array $options (optional): Search options. For filters, limit, ordering, etc.

Returns:

\Phproberto\Joomla\Entity\Collection

Examples:

$category = Category::instance(34);

// Get category descendants of level 4
$childCategories = $category->searchDescendants(['filter.level' => 4]);

table($name = ‘’, $prefix = null, $options = array())

Get a table instance. Defauts to \CategoriesTableCategory. Most of the times you don’t want to access this because entity handles most stuff that requires table access.

Parameters:

  • string $name (optional): Table name. Defaults to Content.
  • string $prefix (optional): Class prefix. Defaults to JTable.
  • array $options (optional): Configuration array for the table.

Returns:

\JTable

Throws:

\InvalidArgumentException if table cannot be loaded.

Examples:

$category = Category::instance(34);

// Load another category through \CategoriesTableCategory
$table = $category->table();
$table->load(35);

validator()

Get the category validator. This may be useful when you want to apply a specific validation rule to a category.

Parameters:

None

Returns:

\Phproberto\Joomla\Entity\Categories\Validation\CategoryValidator

Examples:

$category = Category::instance(34);

$validator = $category->validator();

// Add a custom rule that checks that category level is greater than 2
$validator->addRule(
	new CustomRule(
		function ($value, 'Only level > 2 allowed')
		{
			return (int) $value > 2;
		}
	),
	'level'
);
$validator->validate();