Book Image

Mastering Joomla! 1.5 Extension and Framework Development Second Edition

Book Image

Mastering Joomla! 1.5 Extension and Framework Development Second Edition

Overview of this book

Joomla! is one of the world’s top open source content management systems, which enables you to build websites and powerful online applications. Out of the box, Joomla! does a great job of managing the content needed to enhance the functionality of your website. But for many people, the true power of Joomla! lies in its application framework that makes it possible for thousands of developers around the world to create powerful add-ons and extensions. This book will guide you through the complexities of implementing add-ons, components, modules, and plugins in Joomla! 1.5. If you feel that you’ve mastered the basics of creating Joomla! extensions, then this book will take you to the next level. Packed with expert advice on all aspects of programming with Joomla!, this book is an invaluable reference guide you will turn to again and again for your Joomla! development tasks. The book starts by explaining the structure and design of the Joomla! Framework. Then we move on to extending and storing data in standard fields and dealing with multilingual requirements. Further, you will discover best practices, design, and coding methods for Joomla! components, modules, plugins, and other extensions. Along the way, you will actually design and create a component, module, and plugin that work together to add functionality to the Joomla! framework. You will also learn about customizing the page output using JavaScript effects, making use of Web Services from within Joomla! and ensuring that your code is secure and error-free. You will discover how to easily create sophisticated extensions that are robust, user-friendly, and professional by taking advantage of the many libraries and utilities provided by the Joomla! framework. A comprehensive, up-to-date reference to the Joomla! Framework API is also included in the book. Finally, we will discuss advanced methods for rendering your extensions more robust, secure, and professional.
Table of Contents (18 chapters)
Mastering Joomla! 1.5 Extension and Framework Development
Credits
About the Author
About the Reviewer
Preface

JDatabase


abstract, extends JObject , located in / joomla/database/database.php

This is the base database connector class. There are two core subclasses (sometimes called drivers or adapters), JDatabaseMySQL and JDatabaseMySQLi. Additional subclasses, enabling support for other database servers may be included with future releases of Joomla!. For more information about the JDatabase class refer to Chapter 3, The Database.

Direct descendents

JDatabaseMySQL

MySQL database driver

JDatabaseMySQLi

MySQLi database driver

Properties

string $name

Database driver name

mixed $_cursor

Result of last mysql_query() call

boolean $_debug

Debug mode: 0=disabled, 1=enabled

string $_errorMsg

Error message from last query

integer $_errorNum

Error number from last query

boolean $_hasQuoted

There are specific field names to be quoted

integer $_limit

Maximum number of records to return from a query

array $_log

Query history (only if debug is enabled)

string $_nameQuote

Named SQL element quotes (tables, fields, databases)

string $_nullDate

Null date string

integer $_offset

Record offset

array $_quoted

Array of values that should be quoted

mixed $_resource

Database resource

string $_sql

Current query

string $_table_prefix

Database table prefix, default is 'jos_'

integer $_ticker

Number of queries executed (only if debug is enabled)

boolean $_utf

Indicates whether the database supports UTF-8

Inherited properties

Inherited from JObject:

  • JObject::_errors

Inherited methods

Inherited from JObject:

  • JObject::JObject()

  • JObject::__construct()

  • JObject::get()

  • JObject::getError()

  • JObject::getErrors()

  • JObject::getProperties()

  • JObject::getPublicProperties()

  • JObject::set()

  • JObject::setError()

  • JObject::setProperties()

  • JObject::toString()

Methods

Constructor __construct

Builds a new JDatabase object and initializes internal properties. Subclasses also connect to the specified database. The $options array normally includes the keys host, user, password, database, prefix, and select.

Redefines JObject::__construct(); overridden in descendant classes.

JDatabase __construct()

  • array $options: An associative array of database properties

  • object: Returns a new JDatabase object

Destructor __destruct

Runs when the JDatabase object is destroyed, ensuring that the database connection is cleanly closed.

Redefines JObject::__destruct(); overridden in descendant classes.

boolean __destruct()

  • boolean: Returns true on success

addQuoted

This method adds a field name or an array of field names to the list of names that should always be encapsulated in quotes. Sets the protected variable $_hasQuoted to true.

void addQuoted($quoted)

  • mixed $quoted: Field name or an array of field names

  • voi d: No return type

connected

This method determines if the database connection to the server is active. Redefined in descendants:

  • JDatabaseMySQL::connected()

  • JDatabaseMySQLi::connected()

boolean connected()

  • boolean: Returns true if the database connection is currently active

debug

This method sets the debug mode.

void debug($level)

  • integer $level: Debug level: 0=disabled, 1=enabled

  • void: No return type

explain

This abstract method is a diagnostic function that explains the current query. Redefined in descendants:

  • JDatabaseMySQL::explain()

  • JDatabaseMySQLi::explain()

void explain()

  • void: No return type

getAffectedRows

This abstract method returns the total number of records that were affected by the last query. Redefined in descendants:

  • JDatabaseMySQL::getAffectedRows()

  • JDatabaseMySQLi::getAffectedRows()

integer getAffectedRows()

  • integer: Returns the number of records that were affected by the last query

getCollation

This abstract method returns the database collation name. This method is not infallible for MySQL databases; MySQL allows the collation to be set at four different levels, server, database, table, and column. This method returns the collation used by #__content.fulltext; it is possible that the collation may differ elsewhere in the database. This method only works if the database supports UTF-8. Redefined in descendants:

  • JDatabaseMySQL::getCollation()

  • JDatabaseMySQLi::getCollation()

string getCollation()

  • string: Returns the collation name

getConnectors

This method gets an array of database driver names supported in the current environment.

array getConnectors()

  • array: Returns an array of available driver names

getErrorMsg

This method returns the error message from the last query. If no error was encountered it returns an empty string.

string getErrorMsg([$escaped = false])

  • boolean $escaped: If true escape the message with slashes

  • strin g: Returns the error message from the last query

getErrorNum

This method returns the error number from the last query. If no error was encountered it returns a 0 (zero).

integer getErrorNum()

  • integer: Returns the error number from the last query

getEscaped

This method returns an escaped string for use as a value in a query. Redefined in descendants:

  • JDatabaseMySQL::getEscaped()

  • JDatabaseMySQLi::getEscaped()

string getEscaped($text, [$extra = false])

  • string $text: The string to be escaped

  • boolean $extra: If true add additional escaping

  • string: Returns the escaped string

getInstance

This method returns a reference to a global JDatabase object only creating it if it does not already exist. A separate instance will exist for each distinct set of $options. The $options array normally contains the keys defined in the constructor $options array along with the key driver. The driver key value determines the subclass that is instantiated. Currently, the core drivers that are available include MySQL and MySQLi.

JDatabase &getInstance([$options = array()])

  • array $options: Database parameters to be passed to the database driver

  • object: Returns a reference to a JDatabase object

getLog

This method returns the database error log.

array getLog()

  • array: Returns the database error log

getNullDate

This method returns a null date string specific to the current database driver.

string getNullDate()

  • string: Returns quoted null/zero date-time string

getNumRows

This abstract method returns the number of records that were accessed by the most recent query. If $cur is specified, it will determine the number of rows that were returned for the corresponding query. This only works if the query was a SELECT, SHOW, DESCRIBE, or EXPLAIN query. Redefined in descendants:

  • JDatabaseMySQL::getNumRows()

  • JDatabaseMySQLi::getNumRows()

integer getNumRows([$cur = null])

  • resource $cur: The database resource result from the most recent query

  • integer: Returns the number of rows returned from the most recent query

getPrefix

This method returns the database table prefix, normally jos_.

string getPrefix()

  • string: Returns the database table prefix

getQuery

This method returns the active query.

string getQuery()

  • string: Returns the current value of the internal SQL variable

getTableCreate

This abstract method returns the CREATE TABLE statement(s) for each of the table names provided. Redefined in descendants:

  • JDatabaseMySQL::getTableCreate()

  • JDatabaseMySQLi::getTableCreate()

array getTableCreate($tables)

  • mixed $tables: A table name or list of table names

  • array: Returns an associative array of CREATE TABLE statements

getTableFields

This abstract method returns an associative array of table field names and types for the table names provided. Redefined in descendants:

  • JDatabaseMySQL::getTableFields()

  • JDatabaseMySQLi::getTableFields()

array getTableFields($tables, [$typeonly = true])

  • mixed $tables: A table name or an array of table names

  • boolean $typeonly: Only return field types; default is true

  • array: Returns an associative array of field names and their types

getTableList

This abstract method returns an array of all the tables in the database. Redefined in descendants:

  • JDatabaseMySQL::getTableList()

  • JDatabaseMySQLi::getTableList()

array getTableList()

  • array: Returns an array of all the tables in the database

getTicker

This method returns the total number of queries that have been executed.

integer getTicker()

  • integer: Returns the total number of queries executed

getUTFSupport

This method determines if the database supports UTF-8.

boolean getUTFSupport()

  • boolean: Returns true if the database supports UTF-8

getVersion

This abstract method returns the version of the database server. Redefined in descendants:

  • JDatabaseMySQL::getVersion()

  • JDatabaseMySQLi::getVersion()

string getVersion()

  • string: Returns the version of the database server

hasUTF

This abstract method determines if the database supports UTF-8. You should use getUTFSupport() in preference to this method as it returns a cached value of hasUTF(). Redefined in descendants:

  • JDatabaseMySQL::hasUTF()

  • JDatabaseMySQLi::hasUTF()

boolean hasUTF()

  • boolean: Returns true if the database supports UTF-8

insertid

This abstract method returns the value of the primary key ID inserted as a result of the last query if the query was an INSERT query on a table with an auto-increment primary key. For all other queries a zero will be returned. Redefined in descendants:

  • JDatabaseMySQL::insertid()

  • JDatabaseMySQLi::insertid()

integer insertid()

  • integer: Returns the id value generated from the previous INSERT operation

insertObject

This abstract method inserts a row into a table based on the referenced object's properties. The referenced object's properties must match the table fields.

If the primary key field name (keyName) is specified the object will be updated with the new record's primary key value assuming the table has an auto-incremented primary key. Redefined in descendants:

  • JDatabaseMySQL::insertObject()

  • JDatabaseMySQLi::insertObject()

boolean insertObject($table, &$object, [$keyName = null])

  • string $table: The name of the table

  • object $object: The object whose properties match table fields

  • string $keyName: Optional. The name of the primary key

  • boolean : Returns true upon success

isQuoted

This method determines if the field name is among the field names that should be encapsulated in quotes. If no field names have been specified to be quoted, the method returns true.

boolean isQuoted($fieldName)

  • string $fieldName: The field name

  • boolean: Returns true if the fieldname should be encapsulated in quotes

loadAssoc

This abstract method executes the current query and returns the first row as an associative array.

If no query has been set or the query returns an empty result set, the method will return null. Redefined in descendants:

  • JDatabaseMySQL::loadAssoc()

  • JDatabaseMySQLi::loadAssoc()

mixed loadAssoc()

  • mixed: Returns the first row of the query result as an associative array

loadAssocList

This abstract method executes the current query and returns a two-dimensional array of rows. Each inner array represents a row as an associative array. If the primary key field name is provided, the outer array will be an associative array that uses the primary key value as the array key.

If no query has been set or the query returns an empty result set, the method will return null. Redefined in descendants:

  • JDatabaseMySQL::loadAssocList()

  • JDatabaseMySQLi::loadAssocList()

mixed loadAssocList([$key = ''])

  • string $key: Optional primary key fieldname

  • mixed: Returns an array of associative arrays containing the result set

loadObject

This abstract method executes the current query and returns the first row as a stdClass object.

If no query has been set or the query returns an empty result set, the method will return null. Redefined in descendants:

  • JDatabaseMySQL::loadObject()

  • JDatabaseMySQLi::loadObject()

mixed loadObject()

  • mixed: Returns first row of the query result as stdClass object

loadObjectList

This abstract method executes the current query and returns an array of stdClass objects. Each object represents a row. If the primary key field name is provided the outer array will be an associative array that uses the primary key value as the array key.

If no query has been set or the query returns an empty result set, the method will return null. Redefined in descendants:

  • JDatabaseMySQL::loadObjectList()

  • JDatabaseMySQLi::loadObjectList()

mixed loadObjectList([$key = ''])

  • string $key: Optional primary key field name

  • mixed: Returns an array of associative arrays containing the result set

loadResult

This abstract method executes the current query and returns the first field of the first row returned by the query.

If no query has been set or the query returns an empty result set, the method will return null. Redefined in descendants:

  • JDatabaseMySQL::loadResult()

  • JDatabaseMySQLi::loadResult()

string loadResult()

  • string: Returns the first field of the first row of the query result set

loadResultArray

This abstract method executes the current query and returns an array of a single field/column from the query result set. The column number is 0 (zero) based, for example, the first column = 0.

If no query has been set or the query returns an empty result set, the method will return null. Redefined in descendants:

  • JDatabaseMySQL::loadResultList()

  • JDatabaseMySQLi::loadResultList()

mixed loadResultArray([$numinarray = 0])

  • integer $numinarray: Optional column/field number; defaults to first field

  • mixed: Returns an array or column/field values

loadRow

This abstract method executes the current query and returns the first row as an array.

If no query has been set or the query returns an empty result set, the method will return null. Redefined in descendants:

  • JDatabaseMySQL::loadRow()

  • JDatabaseMySQLi::loadRow()

mixed loadRow()

  • mixe d: Returns first row of the query result as an array

loadRowList

This abstract method executes the current query and returns a two-dimensional array of rows. Each inner array represents a row as an array. If the primary key field name is provided the outer array will be an associative array that uses the primary key value as the array key.

If no query has been set or the query returns an empty result set, the method will return null. Redefined in descendants:

  • JDatabaseMySQL::loadRowList()

  • JDatabaseMySQLi::loadRowList()

array loadRowList([$key = ''])

  • string $key: Optional primary key field name

  • array: Returns an array of arrays containing the result set

nameQuote

This method encapsulates named SQL elements (tables, fields, databases) in quotes. The quotes used are determined by the current database driver. If the element name is using dot-notation (for example, a.b) the name will be returned unquoted.

string nameQuote($s)

  • string $s: The string to encapsulate in quotes

  • string: Returns the quoted string

query

This abstract method executes the current query. If the query is successful and is a SELECT, SHOW, DESCRIBE, or EXPLAIN query a database resource will be returned. If the query is successful and is not one of the prior query types the method will return true; if any query fails false will be returned. Redefined in descendants:

  • JDatabaseMySQL::query()

  • JDatabaseMySQLi::query()

mixed query()

  • mixed: Returns a database resource or true on success; false on failure

queryBatch

This abstract method executes a batch query. If $abort_on_error is true the batch process will stop if an error occurs. If $p_transaction_safe is true then all the queries will only be applied if they are all successful. Redefined in descendants:

  • JDatabaseMySQL::queryBatch()

  • JDatabaseMySQLi::queryBatch()

mixed queryBatch([$abort_on_error = true], [$p_transaction_safe = false])

  • boolean $abort_on_error: Stop batch process on error

  • boolean $p_transaction_safe: Perform as a transaction

  • mixed: Returns true on success; false on failure; or the failed resource

Quote

This method encapsulate $text in quotes. The quote used is determined by the current database driver. If $escaped is true $text is escaped; if false $text is not escaped.

string Quote($text, [$escaped = true])

  • string $text: The string to encapsulate in quotes and escape

  • boolean $escaped: If true $text is escaped; if false $text is not escaped

  • string: Returns the quoted string

replacePrefix

This method replaces all occurrences of the table prefix found in the query string $sql with the value held in the $_table_prefix class variable.

string replacePrefix($sql, [$prefix = '#__'])

  • string $sql: The query string

  • string $prefix: The table prefix to replace in the query string

  • string: Returns the query string with replaced table prefix

setQuery

This method sets the next query to execute. $offset and $limit are used for pagination; in MySQL this relates directly to the LIMIT clause. If you use $offset or $limit, your SQL must not contain a LIMIT clause. $prefix is the string that is replaced by the database table prefix; it would be unusual to change this from the default #__.

void setQuery($sql, $offset = 0, $limit = 0, [$prefix = '#__'])

  • string $sql: The query string

  • integer $offset: The first record to return

  • integer $limit: The maximum number of records

  • string $prefix: The table prefix to replace in the query string

  • void: No return type

setUTF

This abstract method prepares the database connection for UTF-8 strings. Redefined in descendants:

  • JDatabaseMySQL::setUTF()

  • JDatabaseMySQLi::setUTF()

void setUTF()

  • void: No return type

splitSql

This method splits a string of queries into an array of individual queries.

array splitSql($queries)

  • string $queries: The queries to be split

  • array: Returns an array of individual queries

stderr

This method returns an error report of the last error. If $showSQL is true the SQL is included in the report.

string stderr([$showSQL = false])

  • boolean $ showSQL: If true includes the last SQL statement in the report

  • string: Returns an error report

test

This abstract method verifies that the database server connection function has been defined. For MySQL this is mysql_connect() and for MySQLi it is mysqli_connect(). Redefined in descendants:

  • JDatabaseMySQL::test()

  • JDatabaseMySQLi::test()

boolean test()

  • boolean: Returns true if the connection function has been defined

updateObject

This abstract method treats $object as an updated record and attempts to update the specified table from $object. If $updateNulls is true, object properties that are null will still be used to update the record in the table. Redefined in descendants:

  • JDatabaseMySQL::updateObject()

  • JDatabaseMySQLi::updateObject()

boolean updateObject($table, &$object, $keyName, [$updateNulls = true])

  • string $table: The table name to be updated

  • object $object: The record object

  • string $keyName: The primary key name

  • boolean $updateNulls: Update values even if they are null

  • boolean: Returns true on success

ADOdb methods

The following methods are included in the JDatabase class to provide emulation of ADOdb functions. Most methods are empty functions that would require further implementation to work. MySQL and MySQLi databases do not support ADOdb.

BeginTrans

This method emulates ADOdb functionality; it must be overridden in subclasses. If you intend to use this method you must ensure that the database driver supports it.

void BeginTrans()

  • void: No return type

CommitTrans

This method emulates ADOdb functionality; it must be overridden in subclasses. If you intend to use this method you must ensure that the database driver supports it.

void CommitTrans()

  • voi d: No return type

ErrorMsg

This method emulates ADOdb functionality; it returns the error message from the last query; if no error was encountered, the error message will be an empty string.

string ErrorMsg()

  • string: Returns the error message

ErrorNo

This method emulates ADOdb functionality; it returns the error number from the last query; if no error was encountered, the error message will be zero.

integer ErrorNo()

  • integer: Returns the error number

Execute

This method emulates ADOdb functionality; it executes a query. If the query is a SELECT query the results will be returned in a JRecordSet object, if the query is not a SELECT query an empty JRecordSet will be returned upon success, and if the query fails false will be returned.

mixed Execute($query)

  • string $query: Query to execute

  • mixed: Returns JRecordSet object or false on failure

GenID

This method emulates ADOdb functionality; it returns a sequence ID for databases that are sequence aware (sequences are used with databases that allow multiple connections, to reduce the chance of errors). If you are creating an application that relies on sequences, ensure that the JDatabase subclass object supports GenID() fully. Subclasses must implement this method to enable GenID() support. JDatabaseMySQL and JDatabaseMySQLi do not support GenID(); using GenID() with these databases will always return 0.

mixed GenID([$foo1= null], [$foo2 = null])

  • string $foo1: Sequence name

  • string $foo2: Start ID

  • mixed: Returns the sequence ID; can be an integer or a string

GetCol

This method emulates ADOdb functionality; it executes a query and returns an array of the first column from the resultant records.

array GetCol()

  • array: Returns an array of the first column from retrieved records.

GetOne

This method emulates ADOdb functionality; it executes a query and returns the value in the first field in the first record.

mixed GetOne($query)

  • string $query: The query to execute

  • mixed: Returns the value in the first field in the first record

GetRow

This method emulates ADOdb functionality; it executes a query and returns the first row as an array.

array GetRow($query)

  • string $query: The query to execute

  • array: Returns the first row as an array

PageExecute

This method emulates ADOdb functionality; it executes a query and returns the results in a JRecordSet object.

JRecordSet PageExecute($sql, $nrows, $page, [$inputarr = false], [$sec2cache = 0])

  • string $sql: The query to execute

  • integer $nrows: The number of records per page

  • integer $page: The results page [pagination]

  • boolean $inputarr: Ignored; emulation purposes only

  • integer $sec2cache: Ignored; emulation purposes only

  • object: Returns the result in a JRecordSet object

RollbackTrans

This method emulates ADOdb functionality; it must be overridden ins subclasses. If you intend to use this method, please ensure that the database driver supports it.

void RollbackTrans()

  • void: No return type

SelectLimit

This method emulates ADOdb functionality; it executes a query and returns the results in a JRecordSet object. The parameters $offset and $limit are used for pagination; in MySQL databases this relates directly to the LIMIT clause.

JRecordSet SelectLimit($query, $count, $offset)

  • string $query: The query string to execute

  • integer $count: The maximum number of records

  • integer $offset: The first record to return

  • object: Returns a JRecordSet object