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:
- Phproberto\Joomla\Entity\Core\Traits\HasAccess
- Phproberto\Joomla\Entity\Core\Traits\HasAncestors
- Phproberto\Joomla\Entity\Core\Traits\HasAsset
- Phproberto\Joomla\Entity\Core\Traits\HasAssociations
- Phproberto\Joomla\Entity\Core\Traits\HasChildren
- Phproberto\Joomla\Entity\Core\Traits\HasDescendants
- Phproberto\Joomla\Entity\Core\Traits\HasLevel
- Phproberto\Joomla\Entity\Core\Traits\HasMetadata
- Phproberto\Joomla\Entity\Core\Traits\HasParams
- Phproberto\Joomla\Entity\Core\Traits\HasParent
- Phproberto\Joomla\Entity\Core\Traits\HasState
- Phproberto\Joomla\Entity\Core\Traits\HasTranslations
- Phproberto\Joomla\Entity\Users\Traits\HasAuthor
- Phproberto\Joomla\Entity\Users\Traits\HasEditor
- Phproberto\Joomla\Entity\Validation\Traits\HasValidation
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 toContent
.string
$prefix (optional): Class prefix. Defaults toJTable
.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();