In classic MVC, you have models that loosely corresponds to the underlying database tables. Why would Joomla need both models and tables?


Some quotes from provided links:

In summary, the model focuses on business logic and the table on data entities in the database. By entity, I mean a single row in a table in a database.

"So, a JModel class would have one, or more, JTable classes as properties?"

That is the idea.

  • Are you referring to the JTable class? – Lodder Dec 14 '16 at 11:41
  • @Lodder Yes, JTable and JModel classes. – Olle Härstedt Dec 14 '16 at 14:43
  • Perhaps this and this may help – Lodder Dec 14 '16 at 14:45
  • I believe there is no real reason for this. Just some Joomla legacy – Alexandr Jan 31 '17 at 21:31

Class JTable is created to work strictly on database object. Table object should create,modify,delete,check data in database. It can be used in many models, helpers, modules. Single model some times have to use multiple tables and it can happen in many models. If you use JTable you don't have to rewrite SQL in every model just to retrieve or modify a single table record. Not to mention that changes in a JTable work in every place where table was used.

Class JModel is created to work on data. Usually it works on database data but also files, images or simply calculations. Models don't require a JTable, you can easly write a working JModel without it. But when you have to create for example a user account on a complex portal it is possible user data will consist not only of a single table but also example data or records in other tables. Using JTable will allow you to save time on writing CRUD methods and make sure that in every place a single table is used SQL will always be up-to-date.

Having those two separated allows developer to create various extensions that are not tied strictly to database. For example gallery that in model will not only work on database object but also images. Developer does write image processing code and leaves SQL for creating and updating database to JTable.

It can be a bit confusing at the beginning but after few extension you'll see that it is actually helpful. The bigger the extension the more time JTable+JModel will save for developer. The only problem is that it isn't properly explained in documentation.

Here are some useful links

  • But that should be part of an object hierarchy, not two different classes. – Olle Härstedt Jan 15 '17 at 0:12
  • No it shouldn't. Table can be used in many places. If you merge model and table you have to use a complete model or write SQL for simple get methods in for example modules and plugins. Framework have to be versatile. Creating SQL in every place where you want to use same table is a waste. When you update the JTable it is updated in every place you use it. You don't have to rewrite it. Not to mention that no one requires from you to use JTable. You can write extensions without it and they will work as intended. But try to build something bigger. You will see how helpful JTableit is. – Artur Stępień Jan 15 '17 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.