Hi,
The book mentioned is a really good starting point. As your components become more complex you might find some of what I've listed below useful regarding structure.
I've found that when dealing with multiple tables it's best to create a controller/model for each table. Even two doesn't hurt. One for a single entry in the table and another for lists of entries. For example, in your situation, you might have the following:
Component name: com_sportsmanagement
Main Controller:Class Name: SportsManagementController
Filename: /controller.php
Description: This could be used to redirect to your default "list" controller. For example, a list of Leagues or even showing a menu of some sort seeing as you have many tables or entities to manage.
Leagues Controller:Class Name: SportsManagementControllerLeagues
Filename: /controllers/leagues.php
Description: This controller would handle everything to do with lists of Leagues. For example, viewing leagues, delete a league or many selected leagues
League Controller:Class Name: SportsManagementControllerLeague
Filename: /controllers/league.php
Description: This controller would handle adding/modifying/perhaps removing a single League
The same structure would apply to your models and views:
League Model:Class Name: SportsManagementModelLeague
Filename: models/league.php
Description: Contains logic to fetch a single league for display, edit, create (would be array/object representing a new league with default values for some fields etc.)
Leagues Model:Class Name: SportsManagementModelLeagues
Filename: models/leagues.php
Description: Contains logic to fetch a list of leagues, handle pagination, update display order, etc.
Following the above structure, you'll find as things get more complex, it is easier to keep track of code, etc. Also, I've noticed that when using a structure like the above, the Joomla! MVC convenience methods all seem to fall into place.
Things I've noticed that you might want to avoid/keep in mind:
1) First, create a base controller/model and sometimes a base view. You can use these base classes to put common functionality in and share with your other controllers/models/views. A good example of this would be pagination, sort ordering, removing, etc. Even if they are empty to begin with, that's ok, you can add to them as you come across code that you feel will/could be used again. For example:
class SMControllerBase extends JController {}
class SMModelBase extends JModel {}
class SMViewBase extends JView{}
Then you would just extend these classes for your components actual controllers:
class SportsManagementControllerLeague extends SMControllerBase
{
// controller methods and classes here
}
This is not really specific to Joomla!, but rather just good practice in general, so you may already be doing this. I wanted to mention it because I still dive in head first without thinking about things like this.
2) A lot of tutorials show pulling data from the request in the model, with methods like $model->getData(). I prefer at least having a method withing the model that can receive criteria. For example $model->getLeague(1234), where 1234 is the id of the League you are trying to get. If you really want to use the view::get('Data') type convenience methods, wrap the getLeague($id) method in a getData() method.
Example:
class SportsManagementModelLeague extends JModel
{
public function getData()
{
$id = JRequest::getInt('id', 0);
if (!$id) {
return null;
}
return $this->getLeagueById($id);
}
public function getLeagueById ($id)
{
$dbo =& $this->getDBO();
$sql = "SELECT * FROM #__sm_league ";
$sql.= "WHERE id = " . $dbo->quote($id);
$dbo->setQuery($sql);
$result = $db->loadObject();
// might want check for db error here
return $result;
}
}
Hope that helps out,
AJ