How to Get Total Categories?

$allCategories = $this->task->categoryModel->getAll();

Can anybody help me fix this? I am trying to call a count of total categories in a plugin from the category table, regardless of project.

I’m calling like this:

<?= count($allCategories) ?>

The problem is, I don’t know how to call it without a project_id

I found a solution in a different kind of way.

Would you mind telling?

All categories are in stores in the table project_has_categories. I’d write a function to get that content directly.

SELECT COUNT (*) FROM project_has_categories;

Or you iterate through the projects and then through its categories.

hello, I did this:

$allCategories = $this->task->db->table('project_has_categories')->count();

called as:

<?= $allCategories ?>

to show as:

But, if you go via tasks, you’ll omit the global categories, not assigned to a task. Don’t you?

no,… you have to use task if using through a plugin, without it, the entry doesnt work. Also, the data is correct as I am crosschecking it with my working database. All them total are global numbers, per kanboard installation which is the intention.

1 Like

OK, this might work, but IMHO the clean way would be extending the CategoryModel class, adding the missing functions, like e.g. getCategoryCount(). But maybe I miss something. :grin:

I followed this, which made access to the database a lot easier once I figured out how to convert the statement

Yes, of course you should always use the query builder. But this was not the point. I just dislike going via a task, where this is not required. But it’s your decision at the end.

oh I think I have not explained it properly… I am not going via a task… but in to make the variable work via a plugin, you must use via task… although it is not actually going through a task. If you try the code without via task, you will get undefined variable.

I read it in the docs and then @creecros confirmed the same.