A Joomla! MVC component [not] explained!

We would not explain the operation of the MVC design in Joomla!TM, it has already been described by the authors themselves.

A curiosity: the 'metaphor' model-view-controller, is not proper a recent idea, in fact it dates back to 1979, and it is described in an article on SmallTalk by Trygve Reenskaug. We started talking about 'design pattern' since August 1994 after the publication of "Design Patterns - Elements of reusable Object-Oriented Software" by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, reading strongly recommend to anyone, with some knowledge of OOP, who wants to improve himself.

this text is from an old article of mine, published in a printed magazine!

 

How to write a MVC component for Joomla!TM 2.5 and Joomla!TM 3.5 with Marco's Component Maker for Joomla!

In order to explain how to act on a component generated by the components generator, I wrote this series of articles about how to create a simple component for managing a book list: we will use  marco's! Component maker for Joomla!, for building a quick books management component.

Because this component generator works on 'database first' approach, you need a database to scaffold the MVC component; this is the sql you need to create the component, of course you have to set the right table prefix:

CREATE TABLE IF NOT EXISTS `#__mybook_authors` (
  `author_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'PK',
  `name` varchar(100) NOT NULL,
  `book_published` int(11) NOT NULL DEFAULT '0' COMMENT 'number of books published',
  `published` tinyint(1) NOT NULL DEFAULT '1',
  `checked_out` int(10) unsigned NOT NULL DEFAULT '0',
  `checked_out_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `hits` int(10) unsigned NOT NULL DEFAULT '0',
  `ordering` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`author_id`),
  KEY `author_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `#__mybook_books` (
  `book_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key',
  `author_id` int(11) NOT NULL COMMENT 'FK to authors'' table',
  `editor_id` int(11) NOT NULL COMMENT 'FK to editors'' table',
  `title` varchar(100) NOT NULL DEFAULT '',
  `date_published` date NOT NULL COMMENT 'book publishing date',
  `notes` text NOT NULL,
  `published` tinyint(1) NOT NULL DEFAULT '1',
  `checked_out` int(10) unsigned NOT NULL DEFAULT '0',
  `checked_out_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `hits` int(10) unsigned NOT NULL DEFAULT '0',
  `ordering` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`book_id`),
  KEY `book_author_id` (`author_id`),
  KEY `book_editor_id` (`editor_id`),
  KEY `book_published` (`published`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='books table';

CREATE TABLE IF NOT EXISTS `#__mybook_editors` (
  `editor_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'PK',
  `name` varchar(100) NOT NULL,
  `city` varchar(50) NOT NULL COMMENT 'editor''s city',
  `published` tinyint(1) NOT NULL DEFAULT '1',
  `checked_out` int(10) unsigned NOT NULL DEFAULT '0',
  `checked_out_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `hits` int(10) unsigned NOT NULL DEFAULT '0',
  `ordering` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`editor_id`),
  KEY `editor_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 According to joomla styles code, are present the fields suggestest by frameworks (published, checked_out, checked_out_time, hits, ordering).

 

Now create the component, according to the instructions, and using this data suggested...

1. set up & configuration files

Download the joomla's MVC component generator and run it. Try first whitout install the mysql library.

If you get an error (missing library) download and install mysql connector from http://dev.mysql.com/downloads/connector/. Set right version of connector in marcoComponentMaker.exe.config. At the time of writing, the latest version of the mysql connector is 6.8.3
<bindingRedirect oldVersion="6.7.4.0" newVersion="6.8.3.0"/>

 

2. run the component creator for joomla!

These instructions refer to the next to came version 2.5.04, so some options could not be present.
Anyway you can safely use current version.

First step/page:

  • Component name:
    insert 'MyBooks manager' or what you like
  • Safe name:
    insert 'mybooks'; this is the name used by joomla to identify the component (com_mybooks), so you must use a valid joomla component name!
  • Table prefix:
    versions prev 2.5.04: this is the tables' prefix used by your joomla installation. The component generator need to know this to replace the prefix with '#__'
    versions 2.5.04+: moved in database configuration.
  • Generate translation:
    select your own locale (one ore more)

keep default values in other fields and press 'Next Step'

 

3. select db

Select the database. You can create a new connection by editing mysql credentials in this page (remember to save).

Please pay attention: if you don't use localhos as server address be sure that your mysql server accept tcp/ip connection from your machine, and user is granted to access from your ip! Refer to mysql docs for proper configuration.

 

 

4. select tables

Select the 3 tables we need to generate the Joomla! 3.5 MVC component: j30_mybook_books, j30_mybook_authors, j30_mybook_editors. in this example we have the prefix j30_, this may change according to your installation.

We also need to define a name for managing the single object and the record set. Joomla! standard use an 's' (plural) to differenziate sigle object and the list of object, you can follow the standard or not (I don't like plurals like 'mouses').

 

Table nameSingle record object nameRecordset object name
j30_mybook_authors author authorlist
j30_mybook_books book booklist
j30_mybook_editors editor editorlist

press 'Next Step'

 

5. configure fields

Now, for each table, we have to select which fields we want to see (Show in list) or use in search (Search) when we are looking at the records list, and which fields we want to edit when we are going to edit the record.

These are only examples, fell free to try every configuration you like.

 

5.1  j30_mybook_authors

 

Field nameField typeShow in listSearchShow in editRequiredGet Field DataForeign keyRender AsValidator client sideValidator server sideComment
author_id int(11)       Select String none none PK
name varchar(100)   Select String none none  
book_published int(11)     Select String integer custom  number of published books
published tinyint(1)     Select Yes No none none  
checked_out int(10) unsigned           Select String none none  
checked_out_time datetime           Select Calendar none none  
hits int(10) unsigned           Select String none none  
ordering int(11)     Select String none none  

 

For an overview on authors we wanto to see: id, the name, the number of published books, we also want to change the display order in front end (ordering). When we have to edit or to create an author, we would like to edit all informations but not joomla reserved (primary key and check out).

We want joomla checks  quickly the number of published books on client side, we, after, will check id on server side: the component generator writes the file \models\rules\authorbookpublished.php and register it as validator in \models\forms\ author.xml; of course we have to write the php check.

the special field published is automatically rendered as a yes/no select list

 


5.2  j30_mybook_books

 

Field nameField typeShow in listSearchShow in editRequiredGet Field DataForeign keyRender AsValidator client sideValidator server sideComment
book_id int(11)       Select String none none primary key
author_id int(11)  

j30_ mybook_authors .name

Custom FieldList.php none none FK to authors' table
editor_id int(11)   j30_ mybook_editors .name Custom FieldList.php none none FK to editors' table
title varchar(100)   Select String none none  
date_published date     Select Calendar none none  
notes text       Select TextArea none none  
published tinyint(1)     Select Yes No none none  
checked_out int(10) unsigned           Select String none none  
checked_out_time datetime           Select Calendar none none  
hits int(10) unsigned  ✔     Select String none none  
ordering int(11)     Select String none none  

For an overview on authors we wanto to see: the title, the author and editor, the number of hits on book details and so on (see Show in list column).

We want to see autor's and editor's name in list, so we have to specify the field to show in the primary key's table (Foreign key)

 

At present time only 'internal team version' allow to handle foreign keys. If you have the standard version you hate to edit query in model to build relations.

When we edit the record, author_id and editor_id are foreign key, but we want to see the name, not the id, and so we need to specify "Custom FieldList.php" in Render As column. This forces the component scaffolder to generate two specific field types, insert them in forms and create related files in models/fields directory! Before you can use the books section, you have to edit \models\fields\bookeditorid.php and \models\fields\bookauthorid.php to set the coorect reference to related table and fields (only for non team version).

We also want to use a rich text editor for editing notes on author, so we choose RTE in Render As column.

 

5.3  j30_mybook_editors

Field nameField typeShow in listSearchShow in editRequiredGet Field DataForeign keyRender AsValidator client sideValidator server sideComment
editor_id int(11)       Select String none none PK
name varchar(100)   Select String none none  
city varchar(50)     Select String none none  
published tinyint(1)     Select Yes No none none  
checked_out int(10) unsigned           Select String none none  
checked_out_time datetime           Select Calendar none none  
hits int(10) unsigned           Select String none none  
ordering int(11)     Select String none none  

At this point, I think no other explanations are needed.

 

Press 'Next Step' twice and 'End Work'. The component in now ready to install (it is in output\j25\com_mybooks sub folder): install it!

You can install it both on Joomla! 2.5 or on a Joomla! 3.x installation.

Remember: befor to use the book section, you have to complete \models\fields\bookeditorid.php and \models\fields\bookauthorid.php for getting authors and editors ids and names.

 

Here you can download the package generated by the marco's component maker team version, this works out of the box.

 

Now you are ready to follow instructions for use profitably marco's Component maker in your work.

Create Joomla! 3.x extensions - Manage the Back End- Part 1

 

END OF UPDATED INSTRUCTIONS


 

Manage the back end article

Manage the front end article