Coding standards and hints for Virtuemart » History » Revision 10
« Previous |
Revision 10/14
(diff)
| Next »
Max Milbers, 08/29/2012 12:03 PM
Coding standards and hints for Virtuemart¶
In general we avoid the use of globals. Globals are hard to maintain and they are unsure variables.
Code Formatting:¶
We follow the joomla standard in most cases. We use camelCase for methods. For faster copypasting and working with database fields, both variable and database fields are lowercase and seperated with "_". You may use for internal variables camelCase also.
Example:
function exampleMethod($data = 0) { if (!$product_id) $product_id = JRequest::getInt('product_id', 0); if ($product_id > 0) { ...; } }
SQL Formatting:¶
The queries should follow this format:
$q = 'SELECT `example_id` FROM `#__vm_table` WHERE `user_id`= "' . (int)$user_id . '"';
or if the
$user_id is an array, use $user_id["myId"]
Placing of the ` improves speed, because the sql-engine can parse faster ( and do not have to reparse).
Variable Formatting:¶
Due abstract logic many people name their variables or database fields just "id", "key", "value" and so on.
In our case we want to rewrite the structure of an old code, therefore it is very important to find the variable in the code rather fields in the database.
So we want a kind of this: <classname><tablename>variable like here
$vendor_id or $payment_method_id
Exception from this rule is the use of ordering and published. Ordering and published with 0 and 1 as values are useable with the joomla standard methods.
Filename Formatting:¶
Filenames should be lowercase.
Models filenames (mymodel.php), view direcorynames (myview), all helper files and probably all other files loaded by the Joomla Framework, must be lowercase, no matter how you load the file:
$this->loadHelper('adminMenu'); // looks for the file helpers/adminmenu.php
More complex filenames that are specific to your task and loaded by your own code (using require_once()), e.g. mySpecificClass.php, can be camelcase.
Read this article for more info http://docs.joomla.org/Talk:Developing_a_Model-View-Controller_Component_-_Part_1#Use_lowercase_file_and_folder_names_in_your_components.21
URL Requests:¶
As written in the section Filename Formatting above, Joomla requires lowercase filenames. However, URL requests are not lowercased by Joomla. In order to find the proper views and controllers, make sure you use lowercase only in requests, e.g.:
$this->setRedirect('index.php?option=com_virtuemart&view=updatesMigration', $msg); // Fails
$this->setRedirect('index.php?option=com_virtuemart&view=updatesmigration', $msg); // Works
Character Encoding:¶
Like in joomla we use UTF-8. Please ensure the right character set in your IDE.
Path constants¶
Always use:
JPATH_VM_SITE for the frontend (points to JPATH_SITE.DS.'components'.DS.'com_virtuemart')
JPATH_VM_ADMINISTRATOR for the backend (points to JPATH_SITE.DS.'components'.DS.'com_virtuemart')
This is important for performance and that all pieces of code work in every application that means, plugins, modules and so on.
Database tables and fields¶
Naming convention¶
In general we follow now the nooku conventions for naming tables and fields (http://nooku.assembla.com/spaces/nooku-framework/wiki/KDatabase).
All tables must start with the component name, here virtuemart => #__virtuemart_ and be lowercase.
All non xref tables should use plural. Don't use underscore to make long words readable. Concat them like the germans do, for exampel:
vm_user_info => virtuemart_userinfos, vm_admin_menu => virtuemart_adminmenu, vm_payment_method => virtuemart_paymentmethods, ...
All xref tables are now added with underscore, the use of singular or plural gives a hint on the kind of the xref.
For exampel the xref table linking products to categories looks like #__virtuemart_product_categories. Or the hardcore exampel category_categories :-).
Next is to consider that all primary keys should be always the table name in singular + id on it. Look on this as exampel
CREATE TABLE IF NOT EXISTS `#__virtuemart_product_categories` (
`virtuemart_product_id` int(11) NOT NULL DEFAULT '0',
`virtuemart_category_id` int(11) NOT NULL DEFAULT '0',
Standard publishing fields use:¶
Except the published field we use now the nooku conventions. So we use
`ordering` tinyint(2) NOT NULL,
`shared` tinyint(1) NOT NULL,
`published` tinyint(1) NOT NULL DEFAULT '1',
`created_on` datetime NOT NULL default '0000-00-00 00:00:00',
`created_by` int(11) NOT NULL DEFAULT 0,
`modified_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`modified_by` int(11) NOT NULL DEFAULT 0,
`locked_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`locked_by` int(11) NOT NULL DEFAULT 0,
- publish_up and publish_down formatted in the db as datetime with standard 0000-00-00 00:00:00
The standard query is then
@ $db = &JFactory::getDBO();
$this->_nullDate = $db->getNullDate();
$this->_now = JFactory::getDate()->toMySQL();
$query = 'SELECT * FROM `#__sometable` WHERE yourexampel ="myexampelvalue" AND ';
$query .= ' ( publish_up = '.$this->_db->Quote($this->_nullDate).' OR publish_up <= '.$this->_db->Quote($this->_now).' )' .
' AND ( publish_down = '.$this->_db->Quote($this->_nullDate).' OR publish_down >= '.$this->_db->Quote($this->_now).' ) '; @
Updated by Max Milbers over 12 years ago · 10 revisions