Loading Modules From Models
Most of the time, model files only need database access. That's their job.
But sometimes you need a model to use another module - email sending, file processing, API calls, payment gateways, etc.
Trongate v2 lets you do this explicitly with $this->module().
Heads up: This is an edge case. Most models won't need to load and use other modules. If you're just using using your model files for database queries, you can skip this page entirely.
The Syntax
To use a module from within a model file, explicitly load it first:
That's it. Call $this->module('whatever') before using $this->whatever.
Database vs. Module: The Key Difference
There's an important distinction to understand:
Database Access (No module() call needed)
Module Usage (Requires explicit loading)
Why the difference? Database groups are defined in config/database.php, so Trongate knows they're databases. Modules must be explicitly loaded to signal your intent to use a module rather than access a database.
Complete Example
Here's a realistic scenario: processing an order that requires database queries, tax calculation, and email sending.
In this example:
$this->dbworks automatically (default database)$this->analyticsworks automatically (configured database group)$this->taxrequires$this->module('tax')first$this->email_senderrequires$this->module('email_sender')first
Loading Multiple Modules
If you need several modules, load them all at the start of your method for clarity:
Common Use Cases
You'll typically need $this->module() in models when:
- Sending emails after database operations
- Processing payments and updating order records
- Calling external APIs based on database data
- Generating PDFs or files from database content
- Complex calculations that live in separate modules (tax, shipping, discounts)
- Logging or notifications after data changes
Keep it simple: If your model is loading 5+ modules, consider whether some logic belongs in the controller instead. Models should focus on data operations.
What About Controllers?
Controllers work differently. They support both explicit and automatic module loading:
In models, you must use explicit $this->module() calls. This keeps the distinction clear: databases are automatic, modules are explicit.
Troubleshooting
Error: "Undefined property: Model::$something"
You tried to use a module without loading it first.
Fix: Add $this->module('something') before using $this->something.
Error: "Module controller not found"
The module doesn't exist or isn't named correctly.
Check:
- Module directory exists:
modules/whatever/ - Controller file exists:
modules/whatever/Whatever.php - Class name matches:
class Whatever extends Trongate
Best Practices
- Load modules at the start of your method for clarity
- Database access never needs
module()calls - If you forget to load a module, the error message will remind you
- Keep models focused on data - don't turn them into mini-applications
- When in doubt, put complex orchestration in the controller
We're continually improving the Trongate documentation. If anything is incorrect, unclear, incomplete, or could be better, we'd genuinely appreciate your input.
Share your thoughts in the Documentation Feedback.