What are the differences between Joomla Model Types?
Asked Answered
B

1

16

I'm trying to get to grips with the power behind Joomla (3.x)'s framework.

I've noticed that there are multiple types of model that can be used in a component:


JModelAdmin

Prototype admin model. Acts as a Factory class for application specific objects and provides many supporting API functions.

JModelLegacy

Base class for a Joomla Model Acts as a Factory class for application specific objects and provides many supporting API functions.

JModelList

Model class for handling lists of items. Acts as a Factory class for application specific objects and provides many supporting API functions.

JModelForm

Prototype form model. Acts as a Factory class for application specific objects and provides many supporting API functions.

JModelItem

Prototype item model.


I understand that JModelLegacy seems to be the foundation class. My models have been extending JModelLegacy by default, however, I was wondering if I could be potentially using the benefits from the other classes.

If there was someone who knows about these models, I would appreciate having an explanation about what the differences are between these model classes, and an intended scenario where you would use one over others.

Beabeach answered 11/2, 2014 at 14:23 Comment(0)
S
29

First of all all the classes are available for code study under:

\libraries\legacy\model\

Classes

It's also important to understand that these classes should be used (generally speaking) in connection with the corresponding controllers: JControllerLegacy, JControllerForm and JControllerAdmin.

JModelLegacy - is the base class for the Joomla model (model as in MVC). It will basically work as a Factory class by initializing the database driver object and the table object.

You may want to extend this class if you just want to do some basic SQL queries (any work with JDatabase) and write other business logic.

All models will extend JModelLegacy.


JModelList - is a class for handling lists of items. It provides pagination and filtering. Basically everywhere you display a list of items, you can use JModelList. All core components rely on JModelList.

JModelLegacy example


JModelForm and JModelAdmin are generally associated with forms. Forms can be the the User registration form or creating and editing a record. Forms in Joomla are defined in XML files. JModelForm will load this files as JForm objects, will load form data, preprocess the form and validate it.

This is one of the most powerful classes that you can use. They will do the heavy lifting. When you don't like a specific behaviour you can override it by implementing your own code.

JModelAdmin adds some extra admin functionalities to the form:

  • perform batch operations on records.
  • will do ACL checks on records.

JModelAdmin will generally be used when editing a record or for batch operations, especially in backend.


JModelItem - very unlikely you will need it. It exposes just a method getStoreId() and two properties. You won't need this class, unless for as a naming thing, instead of using JModelLegacy you want to extend it when doing something basic, such as displaying a record. (my personal understanding of things).


Conclusion: Above is a general introduction to this classes, as I see them. My suggestion is to look into detail how Joomla uses them, what methods are exposed and how to use them. They are very powerful classes, and especially for CRUD operations they will help you do all the heavy lifting. After you understand what they do, you can use them and override them when needed.

Scottyscotus answered 11/2, 2014 at 19:8 Comment(2)
This is a refreshingly insightful answer! Outside the question scope, is it then recommended to use two models per entity in the admin area then? One for editing a specific record, and one to list all records?Beabeach
Yes, two models. You would also probably want to have two controllers. Like record and records. To put it simply: edit one record => JModelAdmin; view multiple records => JModelList.Scottyscotus

© 2022 - 2024 — McMap. All rights reserved.