Archived talk

Developing a MVC Component/Using the database

From Joomla! Documentation

Revision as of 16:35, 21 February 2015 by Ncramp (talk | contribs) (Provide method to alter BOM for SQL files)

This should really not be the default developing MVC component, the default should redirect to the documentation for whatever is current and this should be on a 1.6 specific page.

In what sense is it the default? Chris Davenport 20:03, 4 March 2010 (UTC)

I'm not sure is this good place to report issues with some sample code found in this tutorial, but it looks like JDatabaseQuery class has not actually being implemented in Joomla 1.6.0 Alpha2. This is causing problems with implementing Menu Item "Required Parameters". Comments in following code explain this a little bit better.

File: /administrator/components/com_testing/models/fields/testing.php (part of Joomla 1.6 test component)

    26                 $db =& JFactory::getDBO();
    27                 /* JDatabaseQuery class could not be found, so following 3 lines caused error
    28                 $query = new JDatabaseQuery;
    29                 $query->select('id,greeting');
    30                 $query->from('#__testing');
    31                 */
    32                 //manually creating query string instead
    33                 $query = 'SELECT id,greeting FROM #__testing';
    34                 $db->setQuery($query);

I recon this is something that would be fixed by actually implementing JDatabaseQuery class once Joomla 1.6 is in beta or latter stage, but for know it could be confusing to those of use following this tutorial. :)

Ivan Rajkovic 8 April 2010 (UTC)

Not working as expected?[edit]

I may be doing something wrong, but I've copied all the code for the first 6 steps, and on this step I run into a problem. My menu item doesn't have the option to select the id under any of the settings. It does show the ID of the menu item itself, so perhaps there is a conflict based on the name??

No New Field In Menu Item[edit]

I have the same problem. I have been trying to get the additional field in the menu item. It does not seem to work. I have tried it on Beta10, Beta5 and Beta1 - no luck. I have even tried the above manually built query (I don't think that is the problem) Any Suggestions? Rpwolfgram 02:45, 2 October 2010 (UTC)

Improper Implementation of the MVC Pattern[edit]

As I was reading through this article on implementing an MVC component, I couldn't help but notice a complete violation of programming standards regarding encapsulation. Please see the chunk of code below that I pulled from the database usage page of this article.

public function getMsg() 
	if (!isset($this->msg)) 
		$id = JRequest::getInt('id', 1);
		// Get a TableHelloWorld instance
		$table = $this->getTable();

		// Load the message

		// Assign the message
		$this->msg = $table->greeting;
	return $this->msg;

Take notice that the "id" value that is supplied via a request variable is retrieved from within the model method itself. This is completely improper as you now have a model which is no longer portable from one platform (i.e. Joomla) to another (i.e. Zend Framework). Models should always be encapsulated thus meaning any required parameters (such as "id") should be passed into the model method as a parameter. This example needs corrected and the entire article re-evaluated as a whole to ensure that you are not continuing the vicious cycle of bad programming.

Submitted By: Matt Scott (

Date: 2012-05-04

UPDATE - 2012-05-04: I have since updated both pages specifically related to the model example to reflect the proper methodologies of implementation.

[MisterEmme] I think the updated version is not working properly: maybe that's bacause $id parameter is not provided to the model

View doesn't pass the selected ID[edit]

It took me a while to figure this out but the selected ID from the Menu is never passed to the model. Thus no matter what option you choose, it will always display "Hello World" as the default for $id is 1

Somewhere in the code the view or model needs to get the ID.

$pk = JRequest::getInt('id');

Using that as the ID in the getMsg function will display the proper selection from the menu parameters.

Part 06: site/models/helloworld.php not updated[edit]

The code is different from the archive. msg instead of messages!

Error in sample code[edit]

I've also spent half a day with some of the problems described above, namely lost parameter and no message selection list in menu item. Solution that worked for me was a small change in default.xml. <message>COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC</message> should be <message><![CDATA[COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC]]></message> Now I have the functionality described in the tutorial.

Backticks in MySQL code preventing table prefix expansion[edit]

I'm new to Joomla but I was getting errors when I used the MySQL specified for admin/sql/install.mysql.utf8.sql The #__ wasn't being replaced with the configured table prefix so it was trying to create a table named #__helloworld instead of jos_helloworld. I was able to resolve this by removing the backticks from the MySQL file.

admin/sql/install.mysql.utf8.sql and admin/sql/updates/mysql/0.0.6.sql

DROP TABLE IF EXISTS #__helloworld;

CREATE TABLE #__helloworld (
  greeting varchar(25) NOT NULL,
   PRIMARY KEY  (id)

INSERT INTO #__helloworld (greeting) VALUES
        ('Hello Database World!'),
        ('Goodbye Database World!');

AnthonyGeoghegan (talk) 08:49, 29 May 2013 (CDT)

Provide method to alter BOM for SQL files[edit]

I had a problem getting this part of the MVC tutorial to work - the issue due to BOM being included in the SQL file as the documentation correctly makes aware to readers. I did not have a editor available for my Linux OS with a easy way to remove BOM. I used the following steps instead, which may be useful for others:

First i prepared the SQL statements from the tutorial in two files: install.sql and uninstall.sql.

I then ran the following terminal command, which removed BOM and saved new files in Joomla naming spec.

nathan@T3-ibm:/var/www/helloworld/admin/sql$ awk '{if(NR==1)sub(/^\xef\xbb\xbf/,"");print}' install.sql > install.mysql.utf8.sql nathan@T3-ibm:/var/www/helloworld/admin/sql$ awk '{if(NR==1)sub(/^\xef\xbb\xbf/,"");print}' uninstall.sql > uninstall.mysql.utf8.sql

I make new files because comparing the filesizes gives an indication that the command was carried out - a little sanity check before zipping the archive etc.

These files can be deleted.

What is required now is to copy and paste the install.mysql.utf8.sql file to helloworld/admin/sql/updates/mysql, and rename the file as 0.0.6.sql

This worked for me pretty well, might be useful for others too.