Trongate PHP Framework Docs
Introduction
Basic Concepts
Understanding Routing
Intercepting Requests
Module Fundamentals
Database Operations
Templates
Helpers
Form Handling
Form Validation
Working With Files
Image Manipulation
Working With Dates & Times
Language Control
Authorization & Authentication
Tips And Best Practices

Model Files

Model files are optional PHP files that can be used to handle data.

Don't need one? Skip this page. Need data handling? Read on!

You really don't need to use model files, if you insist.

However, using model files give you an opportunity to dramatically simplify your controller files.

Having small, easy to manage controller files can really help to make life easy - especially if you're returning to some application code that you haven't looked at for years.

It's all about separation of concerns, baby. Oooh yeah!

The Rules

  • It's one model file per module.
  • Each model file exists at the same directory level as its corresponding controller file.
  • Model files are named as <Module>_model.php
  • Model file names should be snake_case with an uppercase first character.
  • All model files should contain one PHP class that extends Model.

CRITICAL: Model Files Are Private to Their Module

Model files should ONLY be used by their corresponding controller file. Other modules must NOT directly access another module's model file.

If another module needs data operations from your module, expose a public controller method that calls your model internally.

Pattern Code
✗ WRONG $this->users->model->get_active()
✓ CORRECT $this->users->get_active()

In the correct example above, get_active() is a public controller method in Users.php that internally calls the model. This maintains proper encapsulation and ensures each module's data layer remains private.

Where They Live

The text above demonstrates some file and directory locations for a hypothetical 'users' module.

Naming Rules

Thing Rule Example
Model File <Module>_model.php Users_model.php
Class Name <Module>_model extends Model class Users_model extends Model

Primary Job: To Handle Data

Controllers stay skinny. Models do the heavy lifting.

From controller:

Secondary Job: Multiple Databases

Trongate v2 introduces the ability to interact with more than one database by setting groups of database configuration groups within config/database.php.

Example config file: config/database.php

Usually, with Trongate, database queries are handled using $this->db. For example:

If you happen to be running code from a model file, you can query other databases (not just your default database) by using $this-><other-database>. For example:

Controllers cannot query secondary databases. If you run $this->analytics from a controller file, it will attempt to load an 'analytics' module.

Querying secondary databases is model territory. Architecture matters.

Module Interaction Pattern

When one module needs functionality from another module, follow this pattern:

  1. Module A calls public methods in Module B's CONTROLLER
  2. Module B's controller uses its own MODEL internally
  3. Module A never directly accesses Module B's model

This ensures each module's data layer remains encapsulated and promotes clean, maintainable architecture.

Need data? One model. Done.