2

I'm trying to develop my first component in Joomla 3.x. I'm new to the MVC paradigm and learning the basics of Joomla development.

To start, I'm trying to develop a simple component to add and display addresses. I have an Address model extending JModelItem and an Addresses model extending JModelList. I am using lookup tables for the state and city (based on zipcode). How should I look-up this information so that I'll have access to it everywhere it might be needed? Should I have models for state, city, zipcode and have the controller add those models for use in the view? Should I use helper classes? Should I use JTable and access the information in a different way? I don't know how this would be done in Joomla and the MVC paradigm.

I'd appreciate any pointers and help that can be given.

1

All of the 3 ways (models, JTable, helper class) are legit ways in Joomla to solve your problem.

I would probably make a JTable class for your look up table (for CRUD operations) and add models (list and item) and the views and controllers for it. So you can modify the lookup data in your backend and if you add some more special fields to your table you could use more of Joomla's built in functionality (e.g. publishing, ordering, etc.).

You could write a method in your single item model to get you the look up data based on a zip parameter or (probably my way) you could write an additional helper class which will call the model's getItem method and set a zip filter:

<?php
defined('_JEXEC') or die;

// Include your single item model
require_once JPATH_ROOT . '/administrator/components/com_mycomponent/models/lookup.php';

class MycomponentHelperLookup
{
    public static function getDataByZip($zip)
    {
        $model = JModelLegacy::getInstance('Lookup', 'MycomponentModel', array('ignore_request' => true));
        $model->setState('filter.zip', $zip);
        return $model->getItem();
    }
}

In this way you have a high level of reuseability and you are able to use a lot of Joomla functionality.

  • I think you're saying each lookup table should have corresponding models for the list and item (e.g. State and States, City and Cities, Zipcode and Zipcodes, etc.). Correct? You're not saying to have a Lookup and Lookups model are you? Much of the data is unlikely to change so I'm not sure that I need a view (or controller). Am I wrong? However, filtering zipcode to get a list of cities would be very desirable. This might be off on a tangent buth ow is JTable used for CRUD operations? It's only good for single items? – nimble Apr 22 '16 at 15:02
  • No, my way would be to have really a "Lookup" and a "Lookups" model. Regarding the CRUD operations: Yes you are right, a JTable class represents a single item. But if you call delete on a list of items for example it will internally loop through that list and call delete from JTable on every item. – fruppel Apr 24 '16 at 9:44
  • How would you do that for the different tables and data? Would you extend from Lookup and override the getTable()? That's the same as having numerous models for the lookups. – nimble Apr 25 '16 at 19:56
  • Why different tables? Maybe I misunderstood you. I thought you have 1 table with the columns zip, state and city – fruppel Apr 26 '16 at 7:55
  • No. I have numerous lookup tables. There's a states table, direction table (north, south, east, west, etc.), and a zipcode table that has cities in it. In the future I'm sure I'll have lookup tables for various other items. I just wasn't sure how to approach them in Joomla and MVC. Creating models seemed like overkill but that is the approach I've moved forward with. – nimble Apr 26 '16 at 15:42

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.