Working with the database and models in ZOO/JBZoo

10.0/10 rating (1 votes)

Models are a set of classes for working with data (storing, processing etc.).

Additionally, each class usually represents particular system entity (item, category, type etc.).

In this case we’ll only talk about those that work with the database of a component.

It should be noted that the things we’ll examine can hardly be called models. But still they perform their functions - abstracting from the database.

It is recommended to use these entities to work with the database. This way it will not break the integrity of the data in the system and various events can be used. We write this for those who like to work with the database using clean SQL-queries.

Fetching the data from the database

Standard things can be done using table helper.

Partially we talked about this in the article about ZOO (JBZoo) component API. Now let’s examine more complex examples.

// taking a link in order to shorten our code
$itemModel = $this->app->table->item;
$categoryModel = $this->app->table->category;
// items of a specific category
$items = $itemModel->getByCategoryl($appId, $catId);
// get items by author
$items = $itemModel->getByUser($appId, $userId);
// simple arbitrary fetch
// $ids = arbitrary set of item's IDs
$conditions = array(
     'id IN (' . implode(',', $ids) . ')'
$items = $itemModel->all(compact('conditions')); // common way to write in ZOO
$items = $itemModel->all(array('conditions' => $conditions)); // more transparent record
// getting first item by alphabet from an application with id=10
$categories = $categoryModel->all(array(
    'conditions' => array(
        'application_id=?', 10
    'order' => 'name',
    'limit' => 1,

Standard ZOO constructor for fetching is not very flexible. That’s why we used JBZoo to add our models. They all are located here:


Let’s examine a couple of examples:

$options = array(
    'category_nested' => true, // select from nested categories
    'limit' => array(2, 10), // offset and limit
    'user' => true,  // take into account author ID (current user)
    'published' => 1, // take into account publish state
    'order' => 'alpha' // sorting
// $appId, $catId, $typeId - can be a specific value or an array
$items = JBModelItem::model()->getList($appId, $catId, $typeId, $options);
// arbitrary filtering based on JBZoo index
// $elements array fully matches the structure of the query from a filter
$elements = array(
    '<ELEMENT_ID>' => 'Value',
$items = JBModelFilter::model()->filter($elements);

Correctly deleting from the database

An item has a lot of dependences in the database (comments, an index, categories, applications, tags etc.). These relations extend almost to all ZOO tables.

In order to correctly delete an item (or some other entity) only ZOO methods should be called. Otherwise you’ll have various junk in the database and as a result - hidden database errors and excess size.

For these tasks there is a delete() method in each table class.

$tableItem = $this->app->table->item;
$item = $tableItem->get(42);

Thus, all relations and the record itself are deleted + system trigger (event) is called which will execute all attached actions, for example, delete images related to items.


Still gotta questions, who can help me?

Visit our special JBZoo tech support forum which is specifically meant for JBZoo so the priority of the response and its promptness are much better there than in any other place. With inactive account with a “Plus” sign in a tariff plan you can ask for support in one of these sections or use any of our contacts.