Create components for Joomla! - The example component

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

 

Commenti   

+2 #9 Guest 2013-08-21 09:29
Thanks for the complete information regarding to create components for joomla ,exactly am searching for the same info.
screenshots and the stepwise instruction guided me to know about this concept.
I refer to visit this website for joomla extensions:joom la scheduling extension
Citazione
+2 #8 Guest 2013-02-02 07:23
hi, i have followed every step indicated but at step 2 when i selected the localhost server there are no tables to display. can you help? thanke you and here is the configuration i used:
my server is xampp 1.7.3!

====Answer
Hi,
did you change the configuration file as shown in
www.mmleoni.net/.../ step 2?

bye,
marco
Citazione
-3 #7 Guest 2012-07-18 05:10
I searching for the joomla component but here i got the complete info about that. this enough info for that.
Citazione
-3 #6 Guest 2012-06-22 04:23
hi,
I am trying to find the pedigree componet for Joomla would you know where the link is?
thanks
Marshall
Citazione
-7 #5 Hassan 2011-10-30 13:26
Hi,

I am trying to make this working since past few hours. I have downloaded and tried different mysql drivers. But i am unable to make it work. I am using Xampp version 1.6.8 and mysql-connector -net-6.2.5.msi at the moment. And here is my marcoComponentMaker.exe.config

====Answer

hi Hassan,
if you want to submit code you should use the email at the end of the download page. for security reasons html/js/xml code is stripped out from comments.

anyway I suppose you forgot to update the bindingRedirect directive (see download page).

bye,
marco
Citazione
-3 #4 Sloan 2011-10-20 05:15
Never mind. I read the info in the file that was of the download -- it had the right download link for the MySQL connector

Sorry 'bout that!! >> blush
Citazione
-2 #3 Sloan 2011-10-19 22:02
Hi,

I'm having a bit of trouble connecting to the DB. I can get to it using phpAdmin, but when I start MCM, and on the 2nd form, I select the DB and click on the Select Server button, I get an error message:

System.IO.FileNotFoundException: Could not load file or assembly 'MySql.Data, Version=6.2.3.0 , Culture=neutral , PublicKeyToken= c5687fc88969c44 d' or one of its dependencies. The system cannot find the file specified.
File name: 'MySql.Data, Version=6.2.3.0 , Culture=neutral , PublicKeyToken= c5687fc88969c44 d'
at JoomlaComponentBuilder.joomlaComponentBui
....

I went to the link to get the MySQL connector, and downloaded the ODBC one. The PHP native didn't seem approriate.
Should I change the MySQL.data to something else like MySQL.odbc or similar?
I'm running XAMPP under window 7.

Thanks!
Citazione
+1 #2 Guest 2010-04-11 19:56
Are known limitations, but I found acceptable for early versions of the program. in the future ...
Citazione
0 #1 Guest 2010-04-10 14:05
The program is nice, but it fails with tables which primary key is not the default 'id', for example, 'id_brand'.

Moreover, the columns publish and order are somehow not really usefull in all components.
Citazione

Aggiungi commento

Please note: URL in text are not linked and user's site address is only for internal use and is not published.

Comments are human checked. All spam will be removed, so don't waste your time and, especially, mine!

Codice di sicurezza
Aggiorna

L'estate si avvicina hai già trovato l'albergo per le vacanze? cerca un albergo al mare o un hotel in montagna ove trascorrere le tue vacanze.