<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://docs.sandbox.joomla.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=GreyHead</id>
	<title>Joomla! Documentation - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.sandbox.joomla.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=GreyHead"/>
	<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/Special:Contributions/GreyHead"/>
	<updated>2026-05-16T14:19:03Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.0</generator>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117747</id>
		<title>J3.x:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117747"/>
		<updated>2014-04-26T16:27:05Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* content_history_options */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{copyedit}}&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Joomla&#039;s tagging system is used in all core content extensions and  is designed to be be easy to integrate into other extensions that use standard Joomla design patterns. &lt;br /&gt;
&lt;br /&gt;
Using tags in an extension is fairly straightforward. It requires these changes to a typical extension:&lt;br /&gt;
&lt;br /&gt;
* Register a &#039;Content type&#039; for the extension view(s)&lt;br /&gt;
* Add &#039;Observer methods&#039; to the extension table class(es)&lt;br /&gt;
* Add &#039;Tag fields&#039; to the extension edit forms&lt;br /&gt;
* Add &#039;Tag display&#039; code to the extension view files &lt;br /&gt;
* Optionally, add a batch tagging method to the extension administration&lt;br /&gt;
&lt;br /&gt;
== Register a content type for each view ==&lt;br /&gt;
&lt;br /&gt;
In Joomla! 3 content types e.g. articles, weblinks, contacts, . . . are registered in the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table with a separate record for each content view e.g. article, weblink, article category, weblink category, . . .&lt;br /&gt;
&lt;br /&gt;
Your extension installer needs to create a new record for each view.&lt;br /&gt;
&lt;br /&gt;
Here is the structure of the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table; each column is described in the following sections.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Column name !! Column type !! Purpose &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_id&amp;lt;/tt&amp;gt; || int(10) || Record key&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt;|| varchar(255)|| Type title e.g. &amp;lt;tt&amp;gt;Article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_alias&amp;lt;/tt&amp;gt; || varchar(255)|| Type alias e.g. &amp;lt;tt&amp;gt;com_content.article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt; || varchar(255)|| Information about the Table class&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt; || text || Not currently used&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt; || text || Maps the table column names to standard Joomla! names &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt; || varchar(255)|| Optional: name of a router method&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;content_history_options&amp;lt;/tt&amp;gt; || varchar(5120)|| Optional: ????&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===type_id===&lt;br /&gt;
This is the auto-incremented record key, nothing to be done here.&lt;br /&gt;
&lt;br /&gt;
===type_title===&lt;br /&gt;
The title for your Content Type e.g. Article, Contact, Weblink,  &lt;br /&gt;
You can do this either using sql or postflight by creating an instance of JTableContenttype. Don&#039;t forget a category type if you use the Joomla categories API.&lt;br /&gt;
&lt;br /&gt;
===type_alias===&lt;br /&gt;
A string identifying the component and view (that would be in the page request, typically matching the model name) e.g. &amp;lt;tt&amp;gt;com_contact.contact&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;com_weblinks.weblink&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===table===&lt;br /&gt;
You need to identify which tables used by your component contain records to be tagged. For example the &#039;Contact&#039; view of the Contacts component uses &amp;lt;tt&amp;gt;#__contact_details&amp;lt;/tt&amp;gt; and similarly the &#039;Weblinks&#039; view of the Weblinks component uses &amp;lt;tt&amp;gt;#__weblinks &amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Each of these components uses a class to write to the table: &amp;lt;tt&amp;gt;ContactTableContact&amp;lt;/tt&amp;gt; for the Contacts component and &amp;lt;tt&amp;gt;WeblinksTableWeblink&amp;lt;/tt&amp;gt; for the Weblinks component. You will need the class prefixes e.g. &amp;lt;tt&amp;gt;ContactTable&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;WeblinksTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that Category types all use the &amp;lt;tt&amp;gt;#__categories&amp;lt;/tt&amp;gt; table.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;type_table&amp;lt;/tt&amp;gt; entry gives the complete table class information for the table class as a JSON object with two elements. The first element represents your &amp;quot;special&amp;quot; table and the second an optional common table (otherwise it will default the &amp;lt;tt&amp;gt;JTableCorecontent&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Here is the JSON entry for the Weblink content type:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__weblinks&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Weblink&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;WeblinksTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__ucm_content&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;ucm_id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Corecontent&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;JTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
The values to be included are:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Entry name !! Content &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;dbtable&amp;lt;/tt&amp;gt; || Table name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;key&amp;lt;/tt&amp;gt;|| Primary key name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type&amp;lt;/tt&amp;gt; || Content type&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;prefix&amp;lt;/tt&amp;gt; || Class prefix&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt; || An option array, as used in your component constructor and getInstance() methods, may be empty&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This information enables the tagging system (and other APIs) to access your table easily. &lt;br /&gt;
&lt;br /&gt;
If you are using Joomla categories make sure to create a category type so that they can be tagged. In Joomla 3.1 and 3.1.1 there is an error where the tag field will show even if there is not a type, but this is corrected in 3.1.4.  &lt;br /&gt;
  &lt;br /&gt;
====Notes====&lt;br /&gt;
* The table name for the common table is &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt;; this is incorrect in 3.1 and 3.1.1 data but is not currently used. The data was updated in 3.1.4.&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt; field would potentially be used for display although this is not implemented currently except in the contenttype field.  Usually it should begin with an upper case letter if it is in English. See note about how to make this translatable. To make your type names translatable add &amp;lt;tt&amp;gt;COM_TAGS_CONTENT_TYPE_+type_title=&amp;quot;Type Title&amp;quot;&amp;lt;/tt&amp;gt; in both the ini and sys.ini files.&lt;br /&gt;
&lt;br /&gt;
===rules===&lt;br /&gt;
Rules is currently not used. It will likely be removed in favor of an &amp;lt;tt&amp;gt;asset_id&amp;lt;/tt&amp;gt; for each type, but currently you can ignore this field which will be managed by &amp;lt;tt&amp;gt;JTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===field_mappings===&lt;br /&gt;
This entry maps the names of specific fields in your table to a set of standard Joomla! names. This mapping is stored as a JSON array with the first &#039;common&#039; element mapping to the common fields and the second &#039;special&#039; element mapping the other fields from the table. &lt;br /&gt;
&lt;br /&gt;
Here is the field_mappings entry for the Banner Content Type (chosen because it is a richer example than Article or WebLink). Note that &#039;common&#039; mappings that have no equivalent are included as &#039;null&#039; (leaving them blank may cause sql issues); and the names and values in the &#039;special&#039; element all match as they are limited to this component.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;core_content_item_id&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;core_title&amp;quot;: &amp;quot;name&amp;quot;,&lt;br /&gt;
    &amp;quot;core_state&amp;quot;: &amp;quot;published&amp;quot;,&lt;br /&gt;
    &amp;quot;core_alias&amp;quot;: &amp;quot;alias&amp;quot;,&lt;br /&gt;
    &amp;quot;core_created_time&amp;quot;: &amp;quot;created&amp;quot;,&lt;br /&gt;
    &amp;quot;core_modified_time&amp;quot;: &amp;quot;modified&amp;quot;,&lt;br /&gt;
    &amp;quot;core_body&amp;quot;: &amp;quot;description&amp;quot;,&lt;br /&gt;
    &amp;quot;core_hits&amp;quot;: &amp;quot;null&amp;quot;,&lt;br /&gt;
    &amp;quot;core_publish_up&amp;quot;: &amp;quot;publish_up&amp;quot;,&lt;br /&gt;
    &amp;quot;core_publish_down&amp;quot;: &amp;quot;publish_down&amp;quot;,&lt;br /&gt;
    &amp;quot;core_access&amp;quot;: &amp;quot;access&amp;quot;,&lt;br /&gt;
    &amp;quot;core_params&amp;quot;: &amp;quot;params&amp;quot;,&lt;br /&gt;
    &amp;quot;core_featured&amp;quot;: &amp;quot;null&amp;quot;,&lt;br /&gt;
    &amp;quot;core_metadata&amp;quot;: &amp;quot;metadata&amp;quot;,&lt;br /&gt;
    &amp;quot;core_language&amp;quot;: &amp;quot;language&amp;quot;,&lt;br /&gt;
    &amp;quot;core_images&amp;quot;: &amp;quot;images&amp;quot;,&lt;br /&gt;
    &amp;quot;core_urls&amp;quot;: &amp;quot;link&amp;quot;,&lt;br /&gt;
    &amp;quot;core_version&amp;quot;: &amp;quot;version&amp;quot;,&lt;br /&gt;
    &amp;quot;core_ordering&amp;quot;: &amp;quot;ordering&amp;quot;,&lt;br /&gt;
    &amp;quot;core_metakey&amp;quot;: &amp;quot;metakey&amp;quot;,&lt;br /&gt;
    &amp;quot;core_metadesc&amp;quot;: &amp;quot;metadesc&amp;quot;,&lt;br /&gt;
    &amp;quot;core_catid&amp;quot;: &amp;quot;catid&amp;quot;,&lt;br /&gt;
    &amp;quot;core_xreference&amp;quot;: &amp;quot;null&amp;quot;,&lt;br /&gt;
    &amp;quot;asset_id&amp;quot;: &amp;quot;null&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;imptotal&amp;quot;: &amp;quot;imptotal&amp;quot;,&lt;br /&gt;
    &amp;quot;impmade&amp;quot;: &amp;quot;impmade&amp;quot;,&lt;br /&gt;
    &amp;quot;clicks&amp;quot;: &amp;quot;clicks&amp;quot;,&lt;br /&gt;
    &amp;quot;clickurl&amp;quot;: &amp;quot;clickurl&amp;quot;,&lt;br /&gt;
    &amp;quot;custombannercode&amp;quot;: &amp;quot;custombannercode&amp;quot;,&lt;br /&gt;
    &amp;quot;cid&amp;quot;: &amp;quot;cid&amp;quot;,&lt;br /&gt;
    &amp;quot;purchase_type&amp;quot;: &amp;quot;purchase_type&amp;quot;,&lt;br /&gt;
    &amp;quot;track_impressions&amp;quot;: &amp;quot;track_impressions&amp;quot;,&lt;br /&gt;
    &amp;quot;track_clicks&amp;quot;: &amp;quot;track_clicks&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
At a minimum for common fields you need to map: content_item_id, alias and title in order to successfully create urls in the tagged items list.  You also will probably want: access, status, and language. The special fields are optional.  &lt;br /&gt;
&lt;br /&gt;
====Note====&lt;br /&gt;
* The &amp;lt;tt&amp;gt;JHelperTags&amp;lt;/tt&amp;gt; and JUcm APIs at 3.1.1 supported arrays for this field, but as of 3.1.4 either arrays or objects are supported; the default is objects.&lt;br /&gt;
&lt;br /&gt;
===router===&lt;br /&gt;
This is an optional entry to include the name of a static helper router method for this type found in its front end helpers folder e.g. &amp;lt;tt&amp;gt;WeblinksHelperRoute::getWeblinkRoute&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If you do not have a custom router then Tags falls back to the rules in &amp;lt;tt&amp;gt;JHelperRoute&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If you only store data in &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt; you will eventually be able to leave the router field blank although this option is not currently implemented.&lt;br /&gt;
&lt;br /&gt;
===content_history_options===&lt;br /&gt;
This section was added in Joomla! 3.2 to link to the Content History component.&lt;br /&gt;
&lt;br /&gt;
Here&#039;s the entry for the Banner Content Type:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;formFile&amp;quot;: &amp;quot;administrator\/components\/com_banners\/models\/forms\/banner.xml&amp;quot;,&lt;br /&gt;
  &amp;quot;hideFields&amp;quot;: [&lt;br /&gt;
    &amp;quot;checked_out&amp;quot;,&lt;br /&gt;
    &amp;quot;checked_out_time&amp;quot;,&lt;br /&gt;
    &amp;quot;version&amp;quot;,&lt;br /&gt;
    &amp;quot;reset&amp;quot;&lt;br /&gt;
  ],&lt;br /&gt;
  &amp;quot;ignoreChanges&amp;quot;: [&lt;br /&gt;
    &amp;quot;modified_by&amp;quot;,&lt;br /&gt;
    &amp;quot;modified&amp;quot;,&lt;br /&gt;
    &amp;quot;checked_out&amp;quot;,&lt;br /&gt;
    &amp;quot;checked_out_time&amp;quot;,&lt;br /&gt;
    &amp;quot;version&amp;quot;,&lt;br /&gt;
    &amp;quot;imptotal&amp;quot;,&lt;br /&gt;
    &amp;quot;impmade&amp;quot;,&lt;br /&gt;
    &amp;quot;reset&amp;quot;&lt;br /&gt;
  ],&lt;br /&gt;
  &amp;quot;convertToInt&amp;quot;: [&lt;br /&gt;
    &amp;quot;publish_up&amp;quot;,&lt;br /&gt;
    &amp;quot;publish_down&amp;quot;,&lt;br /&gt;
    &amp;quot;ordering&amp;quot;&lt;br /&gt;
  ],&lt;br /&gt;
  &amp;quot;displayLookup&amp;quot;: [&lt;br /&gt;
    {&lt;br /&gt;
      &amp;quot;sourceColumn&amp;quot;: &amp;quot;catid&amp;quot;,&lt;br /&gt;
      &amp;quot;targetTable&amp;quot;: &amp;quot;#__categories&amp;quot;,&lt;br /&gt;
      &amp;quot;targetColumn&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
      &amp;quot;displayColumn&amp;quot;: &amp;quot;title&amp;quot;&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
      &amp;quot;sourceColumn&amp;quot;: &amp;quot;cid&amp;quot;,&lt;br /&gt;
      &amp;quot;targetTable&amp;quot;: &amp;quot;#__banner_clients&amp;quot;,&lt;br /&gt;
      &amp;quot;targetColumn&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
      &amp;quot;displayColumn&amp;quot;: &amp;quot;name&amp;quot;&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
      &amp;quot;sourceColumn&amp;quot;: &amp;quot;created_by&amp;quot;,&lt;br /&gt;
      &amp;quot;targetTable&amp;quot;: &amp;quot;#__users&amp;quot;,&lt;br /&gt;
      &amp;quot;targetColumn&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
      &amp;quot;displayColumn&amp;quot;: &amp;quot;name&amp;quot;&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
      &amp;quot;sourceColumn&amp;quot;: &amp;quot;modified_by&amp;quot;,&lt;br /&gt;
      &amp;quot;targetTable&amp;quot;: &amp;quot;#__users&amp;quot;,&lt;br /&gt;
      &amp;quot;targetColumn&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
      &amp;quot;displayColumn&amp;quot;: &amp;quot;name&amp;quot;&lt;br /&gt;
    }&lt;br /&gt;
  ]&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Creating the record===&lt;br /&gt;
You can create records in the installer in one of three ways:&lt;br /&gt;
* in the [[J2.5:Managing_Component_Updates_(Script.php)#postflight|postflight]] method&lt;br /&gt;
* by creating a &amp;lt;tt&amp;gt;JTableContenttype&amp;lt;/tt&amp;gt; instance and adding a row, or&lt;br /&gt;
* by using SQL to create a record directly&lt;br /&gt;
&lt;br /&gt;
== Modify your component&#039;s table class (or classes if you have multiple tables) ==&lt;br /&gt;
&lt;br /&gt;
Add the following to your JTable constructor:&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;_observers = new JObserverUpdater($this);&lt;br /&gt;
JObserverMapper::attachAllObservers($this);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The addition material formerly located here is no longer needed after 3.1.4&#039;&#039;&#039; Please read the document history for this page if you are required to support 3.1.0 or 3.1.1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;For joomla 3.2 I needed this line:&#039;&#039;&#039; ( Dont forget to replace weblinks by your component name )&lt;br /&gt;
&lt;br /&gt;
JObserverMapper::addObserverClassToClass(&#039;JTableObserverTags&#039;, &#039;WeblinksTableWeblink&#039;, array(&#039;typeAlias&#039; =&amp;gt; &#039;com_weblinks.weblink&#039;));&lt;br /&gt;
&lt;br /&gt;
== Add tags to the getItem() method of the model ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: this was only required in 3.1.0 and 3.1.1. It should not be used in 3.1.4 or later.&#039;&#039;&#039; Please read the history of this page if you need instructions for older versions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add a tag field to edit screens ==&lt;br /&gt;
&lt;br /&gt;
In any edit layouts where you want to allow tagging, you need to add the field to the xml and the appropriate layouts if necessary. The core layouts us a JLayout to manage this and the same layout by any extension.&lt;br /&gt;
&lt;br /&gt;
Update: Note that in &#039;&#039;&#039;3.1.1 only&#039;&#039;&#039; there is special handling of this in the core.  Tags in the edit screen &#039;&#039;&#039;MUST&#039;&#039;&#039; be part of a metadata &amp;lt;fields&amp;gt;&amp;lt;/fields&amp;gt; group.  The core provides two JLayouts to help you manage standard layouts, one (details) for the metadata and one for the sidebar that includes the tabs. In update an extension to 3.1.4 you may need to fix adjust your edit views.&lt;br /&gt;
&lt;br /&gt;
In 3.1.4 or later this special handling is not necessary.  Best practice is to use the standard JLayouts since this provides a consistent experience for users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field loads all the Javascript libraries required. You don&#039;t need to worry about that.&lt;br /&gt;
&lt;br /&gt;
The field supports two modes:  &lt;br /&gt;
* &#039;&#039;&#039;Nested tags mode.&#039;&#039;&#039; Hierarchical tag list. Doesn&#039;t support on the fly tag creation.&lt;br /&gt;
* &#039;&#039;&#039;AJAX mode.&#039;&#039;&#039;  Tags are searched while user types (3 min. chars required to launch the AJAX search). Custom tags are added by pressing ENTER or COMMA keys. Tags show the global route/path. Example: &amp;lt;code&amp;gt;grandpa/parent/tag&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field mode can be forced or use the com_tags setting &amp;lt;code&amp;gt;Tag field mode&amp;lt;/code&amp;gt; to determine its mode. To set/force the field mode we have to add &#039;&#039;&#039;mode=&amp;quot;ajax&amp;quot;&#039;&#039;&#039; or &#039;&#039;&#039;mode=&amp;quot;nested&amp;quot;&#039;&#039;&#039; to the tag field definition.  &lt;br /&gt;
&lt;br /&gt;
Example of forced AJAX mode:  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The field also includes an attribute to allow/deny the user to enter custom values. Currently this only works in AJAX mode. The attribute has to be added to the field definition like **custom=&amp;quot;allow&amp;quot;** or **custom=&amp;quot;deny&amp;quot;**  &lt;br /&gt;
&lt;br /&gt;
Example field definition with custom tags denied:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot; custom=&amp;quot;denied&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
if it is not already there. Usually multiple should be true unless you have a specific reason for it not to be.&lt;br /&gt;
In the core components in administrator, the field is in the group shown on the right, below the language field.&lt;br /&gt;
&lt;br /&gt;
Note: As of 3.1.2 if you wish to use the field to designate parent tags you must add parent=&amp;quot;parent&amp;quot; to the xml definition of the field.&lt;br /&gt;
&lt;br /&gt;
== Prepare the view ==&lt;br /&gt;
&lt;br /&gt;
Add an appropriate version of this to your view.html.php file before loading the layout:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$item-&amp;gt;tags = new JHelperTags;&lt;br /&gt;
$item-&amp;gt;tags-&amp;gt;getItemTags(&#039;com_newsfeeds.newsfeed.&#039; , $this-&amp;gt;item-&amp;gt;id); &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter should match a type in the types table. The second parameter is the primary key under which this record is stored in your table. This would be used in any display in any view where you want the tags associated with the item to display.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Set up the display ==&lt;br /&gt;
&lt;br /&gt;
In any layout where you want to display the tags associated with an item add:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Changing the object and property names as appropriate.&lt;br /&gt;
&lt;br /&gt;
You will most likely want to add the show_tags parameter to the item parameters, the menu item parameters and component configuration as appropriate for your use case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;?php if ($this-&amp;gt;params-&amp;gt;get(&#039;show_tags&#039;, 1) &amp;amp;&amp;amp; !empty($this-&amp;gt;item-&amp;gt;tags)) : ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
	&amp;lt;?php endif; ?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Batch processing ==&lt;br /&gt;
If you want to add the ability to do batch tagging to a backend list view do the following.&lt;br /&gt;
&lt;br /&gt;
Add tag to the default_batch layout&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
        &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&lt;br /&gt;
				&amp;lt;?php echo JHtml::_(&#039;batch.tag&#039;);?&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;/div&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add tag to the button class in the modal footer (Not totally necessary at this point, but good for potential future changes e.g. if unTag is added.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;button class=&amp;quot;btn&amp;quot; type=&amp;quot;button&amp;quot; onclick=&amp;quot;document.id(&#039;batch-category-id&#039;).value=&#039;&#039;;document.id(&#039;batch-access&#039;).value=&#039;&#039;;document.id(&#039;batch-language-id&#039;).value=&#039;&#039;;document.id(&#039;batch-user-id&#039;).value=&#039;&#039;;document.id(&#039;batch-tag-id)&#039;).value=&#039;&#039;&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a batch method to your model if you are not extending JModelAdmin or overriding the batch method. ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Batch tag a list of item.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @param   integer  $value     The value of the new tag.&lt;br /&gt;
	 * @param   array    $pks       An array of row IDs.&lt;br /&gt;
	 * @param   array    $contexts  An array of item contexts.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @return  void.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @since   3.1&lt;br /&gt;
	 */&lt;br /&gt;
	protected function batchTag($value, $pks, $contexts)&lt;br /&gt;
	{&lt;br /&gt;
		$tagsHelper = new JHelperTags();&lt;br /&gt;
		$tagsHelper-&amp;gt;tagItems($value, $pks, $contexts);&lt;br /&gt;
&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And modify your batch method by adding&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		if (!empty($commands[&#039;tag&#039;]))&lt;br /&gt;
		{&lt;br /&gt;
			if (!$this-&amp;gt;batchTag($commands[&#039;tag&#039;], $pks, $contexts))&lt;br /&gt;
			{&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			$done = true;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pay attention to any JSON encoded strings that you need special handling for in batch processing -- remember that you are saving a copy of the core fields and you need save to work as expected.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s it, now create some tags, tag some items and you are set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]][[Category:Content Tags]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117746</id>
		<title>J3.x:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117746"/>
		<updated>2014-04-26T16:03:34Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* Register a content type for each view */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{copyedit}}&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Joomla&#039;s tagging system is used in all core content extensions and  is designed to be be easy to integrate into other extensions that use standard Joomla design patterns. &lt;br /&gt;
&lt;br /&gt;
Using tags in an extension is fairly straightforward. It requires these changes to a typical extension:&lt;br /&gt;
&lt;br /&gt;
* Register a &#039;Content type&#039; for the extension view(s)&lt;br /&gt;
* Add &#039;Observer methods&#039; to the extension table class(es)&lt;br /&gt;
* Add &#039;Tag fields&#039; to the extension edit forms&lt;br /&gt;
* Add &#039;Tag display&#039; code to the extension view files &lt;br /&gt;
* Optionally, add a batch tagging method to the extension administration&lt;br /&gt;
&lt;br /&gt;
== Register a content type for each view ==&lt;br /&gt;
&lt;br /&gt;
In Joomla! 3 content types e.g. articles, weblinks, contacts, . . . are registered in the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table with a separate record for each content view e.g. article, weblink, article category, weblink category, . . .&lt;br /&gt;
&lt;br /&gt;
Your extension installer needs to create a new record for each view.&lt;br /&gt;
&lt;br /&gt;
Here is the structure of the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table; each column is described in the following sections.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Column name !! Column type !! Purpose &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_id&amp;lt;/tt&amp;gt; || int(10) || Record key&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt;|| varchar(255)|| Type title e.g. &amp;lt;tt&amp;gt;Article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_alias&amp;lt;/tt&amp;gt; || varchar(255)|| Type alias e.g. &amp;lt;tt&amp;gt;com_content.article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt; || varchar(255)|| Information about the Table class&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt; || text || Not currently used&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt; || text || Maps the table column names to standard Joomla! names &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt; || varchar(255)|| Optional: name of a router method&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;content_history_options&amp;lt;/tt&amp;gt; || varchar(5120)|| Optional: ????&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===type_id===&lt;br /&gt;
This is the auto-incremented record key, nothing to be done here.&lt;br /&gt;
&lt;br /&gt;
===type_title===&lt;br /&gt;
The title for your Content Type e.g. Article, Contact, Weblink,  &lt;br /&gt;
You can do this either using sql or postflight by creating an instance of JTableContenttype. Don&#039;t forget a category type if you use the Joomla categories API.&lt;br /&gt;
&lt;br /&gt;
===type_alias===&lt;br /&gt;
A string identifying the component and view (that would be in the page request, typically matching the model name) e.g. &amp;lt;tt&amp;gt;com_contact.contact&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;com_weblinks.weblink&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===table===&lt;br /&gt;
You need to identify which tables used by your component contain records to be tagged. For example the &#039;Contact&#039; view of the Contacts component uses &amp;lt;tt&amp;gt;#__contact_details&amp;lt;/tt&amp;gt; and similarly the &#039;Weblinks&#039; view of the Weblinks component uses &amp;lt;tt&amp;gt;#__weblinks &amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Each of these components uses a class to write to the table: &amp;lt;tt&amp;gt;ContactTableContact&amp;lt;/tt&amp;gt; for the Contacts component and &amp;lt;tt&amp;gt;WeblinksTableWeblink&amp;lt;/tt&amp;gt; for the Weblinks component. You will need the class prefixes e.g. &amp;lt;tt&amp;gt;ContactTable&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;WeblinksTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that Category types all use the &amp;lt;tt&amp;gt;#__categories&amp;lt;/tt&amp;gt; table.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;type_table&amp;lt;/tt&amp;gt; entry gives the complete table class information for the table class as a JSON object with two elements. The first element represents your &amp;quot;special&amp;quot; table and the second an optional common table (otherwise it will default the &amp;lt;tt&amp;gt;JTableCorecontent&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Here is the JSON entry for the Weblink content type:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__weblinks&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Weblink&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;WeblinksTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__ucm_content&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;ucm_id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Corecontent&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;JTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
The values to be included are:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Entry name !! Content &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;dbtable&amp;lt;/tt&amp;gt; || Table name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;key&amp;lt;/tt&amp;gt;|| Primary key name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type&amp;lt;/tt&amp;gt; || Content type&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;prefix&amp;lt;/tt&amp;gt; || Class prefix&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt; || An option array, as used in your component constructor and getInstance() methods, may be empty&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This information enables the tagging system (and other APIs) to access your table easily. &lt;br /&gt;
&lt;br /&gt;
If you are using Joomla categories make sure to create a category type so that they can be tagged. In Joomla 3.1 and 3.1.1 there is an error where the tag field will show even if there is not a type, but this is corrected in 3.1.4.  &lt;br /&gt;
  &lt;br /&gt;
====Notes====&lt;br /&gt;
* The table name for the common table is &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt;; this is incorrect in 3.1 and 3.1.1 data but is not currently used. The data was updated in 3.1.4.&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt; field would potentially be used for display although this is not implemented currently except in the contenttype field.  Usually it should begin with an upper case letter if it is in English. See note about how to make this translatable. To make your type names translatable add &amp;lt;tt&amp;gt;COM_TAGS_CONTENT_TYPE_+type_title=&amp;quot;Type Title&amp;quot;&amp;lt;/tt&amp;gt; in both the ini and sys.ini files.&lt;br /&gt;
&lt;br /&gt;
===rules===&lt;br /&gt;
Rules is currently not used. It will likely be removed in favor of an &amp;lt;tt&amp;gt;asset_id&amp;lt;/tt&amp;gt; for each type, but currently you can ignore this field which will be managed by &amp;lt;tt&amp;gt;JTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
===field_mappings===&lt;br /&gt;
This entry maps the names of specific fields in your table to a set of standard Joomla! names. This mapping is stored as a JSON array with the first &#039;common&#039; element mapping to the common fields and the second &#039;special&#039; element mapping the other fields from the table. &lt;br /&gt;
&lt;br /&gt;
Here is the field_mappings entry for the Banner Content Type (chosen because it is a richer example than Article or WebLink). Note that &#039;common&#039; mappings that have no equivalent are included as &#039;null&#039; (leaving them blank may cause sql issues); and the names and values in the &#039;special&#039; element all match as they are limited to this component.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;core_content_item_id&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;core_title&amp;quot;: &amp;quot;name&amp;quot;,&lt;br /&gt;
    &amp;quot;core_state&amp;quot;: &amp;quot;published&amp;quot;,&lt;br /&gt;
    &amp;quot;core_alias&amp;quot;: &amp;quot;alias&amp;quot;,&lt;br /&gt;
    &amp;quot;core_created_time&amp;quot;: &amp;quot;created&amp;quot;,&lt;br /&gt;
    &amp;quot;core_modified_time&amp;quot;: &amp;quot;modified&amp;quot;,&lt;br /&gt;
    &amp;quot;core_body&amp;quot;: &amp;quot;description&amp;quot;,&lt;br /&gt;
    &amp;quot;core_hits&amp;quot;: &amp;quot;null&amp;quot;,&lt;br /&gt;
    &amp;quot;core_publish_up&amp;quot;: &amp;quot;publish_up&amp;quot;,&lt;br /&gt;
    &amp;quot;core_publish_down&amp;quot;: &amp;quot;publish_down&amp;quot;,&lt;br /&gt;
    &amp;quot;core_access&amp;quot;: &amp;quot;access&amp;quot;,&lt;br /&gt;
    &amp;quot;core_params&amp;quot;: &amp;quot;params&amp;quot;,&lt;br /&gt;
    &amp;quot;core_featured&amp;quot;: &amp;quot;null&amp;quot;,&lt;br /&gt;
    &amp;quot;core_metadata&amp;quot;: &amp;quot;metadata&amp;quot;,&lt;br /&gt;
    &amp;quot;core_language&amp;quot;: &amp;quot;language&amp;quot;,&lt;br /&gt;
    &amp;quot;core_images&amp;quot;: &amp;quot;images&amp;quot;,&lt;br /&gt;
    &amp;quot;core_urls&amp;quot;: &amp;quot;link&amp;quot;,&lt;br /&gt;
    &amp;quot;core_version&amp;quot;: &amp;quot;version&amp;quot;,&lt;br /&gt;
    &amp;quot;core_ordering&amp;quot;: &amp;quot;ordering&amp;quot;,&lt;br /&gt;
    &amp;quot;core_metakey&amp;quot;: &amp;quot;metakey&amp;quot;,&lt;br /&gt;
    &amp;quot;core_metadesc&amp;quot;: &amp;quot;metadesc&amp;quot;,&lt;br /&gt;
    &amp;quot;core_catid&amp;quot;: &amp;quot;catid&amp;quot;,&lt;br /&gt;
    &amp;quot;core_xreference&amp;quot;: &amp;quot;null&amp;quot;,&lt;br /&gt;
    &amp;quot;asset_id&amp;quot;: &amp;quot;null&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;imptotal&amp;quot;: &amp;quot;imptotal&amp;quot;,&lt;br /&gt;
    &amp;quot;impmade&amp;quot;: &amp;quot;impmade&amp;quot;,&lt;br /&gt;
    &amp;quot;clicks&amp;quot;: &amp;quot;clicks&amp;quot;,&lt;br /&gt;
    &amp;quot;clickurl&amp;quot;: &amp;quot;clickurl&amp;quot;,&lt;br /&gt;
    &amp;quot;custombannercode&amp;quot;: &amp;quot;custombannercode&amp;quot;,&lt;br /&gt;
    &amp;quot;cid&amp;quot;: &amp;quot;cid&amp;quot;,&lt;br /&gt;
    &amp;quot;purchase_type&amp;quot;: &amp;quot;purchase_type&amp;quot;,&lt;br /&gt;
    &amp;quot;track_impressions&amp;quot;: &amp;quot;track_impressions&amp;quot;,&lt;br /&gt;
    &amp;quot;track_clicks&amp;quot;: &amp;quot;track_clicks&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
At a minimum for common fields you need to map: content_item_id, alias and title in order to successfully create urls in the tagged items list.  You also will probably want: access, status, and language. The special fields are optional.  &lt;br /&gt;
&lt;br /&gt;
====Note====&lt;br /&gt;
* The &amp;lt;tt&amp;gt;JHelperTags&amp;lt;/tt&amp;gt; and JUcm APIs at 3.1.1 supported arrays for this field, but as of 3.1.4 either arrays or objects are supported; the default is objects.&lt;br /&gt;
&lt;br /&gt;
===router===&lt;br /&gt;
This is an optional entry to include the name of a static helper router method for this type found in its front end helpers folder e.g. &amp;lt;tt&amp;gt;WeblinksHelperRoute::getWeblinkRoute&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If you do not have a custom router then Tags falls back to the rules in &amp;lt;tt&amp;gt;JHelperRoute&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If you only store data in &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt; you will eventually be able to leave the router field blank although this option is not currently implemented.&lt;br /&gt;
&lt;br /&gt;
===content_history_options===&lt;br /&gt;
?? What does this section do ??&lt;br /&gt;
&lt;br /&gt;
Here&#039;s the entry for the Banner Content Type:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;formFile&amp;quot;: &amp;quot;administrator\/components\/com_banners\/models\/forms\/banner.xml&amp;quot;,&lt;br /&gt;
  &amp;quot;hideFields&amp;quot;: [&lt;br /&gt;
    &amp;quot;checked_out&amp;quot;,&lt;br /&gt;
    &amp;quot;checked_out_time&amp;quot;,&lt;br /&gt;
    &amp;quot;version&amp;quot;,&lt;br /&gt;
    &amp;quot;reset&amp;quot;&lt;br /&gt;
  ],&lt;br /&gt;
  &amp;quot;ignoreChanges&amp;quot;: [&lt;br /&gt;
    &amp;quot;modified_by&amp;quot;,&lt;br /&gt;
    &amp;quot;modified&amp;quot;,&lt;br /&gt;
    &amp;quot;checked_out&amp;quot;,&lt;br /&gt;
    &amp;quot;checked_out_time&amp;quot;,&lt;br /&gt;
    &amp;quot;version&amp;quot;,&lt;br /&gt;
    &amp;quot;imptotal&amp;quot;,&lt;br /&gt;
    &amp;quot;impmade&amp;quot;,&lt;br /&gt;
    &amp;quot;reset&amp;quot;&lt;br /&gt;
  ],&lt;br /&gt;
  &amp;quot;convertToInt&amp;quot;: [&lt;br /&gt;
    &amp;quot;publish_up&amp;quot;,&lt;br /&gt;
    &amp;quot;publish_down&amp;quot;,&lt;br /&gt;
    &amp;quot;ordering&amp;quot;&lt;br /&gt;
  ],&lt;br /&gt;
  &amp;quot;displayLookup&amp;quot;: [&lt;br /&gt;
    {&lt;br /&gt;
      &amp;quot;sourceColumn&amp;quot;: &amp;quot;catid&amp;quot;,&lt;br /&gt;
      &amp;quot;targetTable&amp;quot;: &amp;quot;#__categories&amp;quot;,&lt;br /&gt;
      &amp;quot;targetColumn&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
      &amp;quot;displayColumn&amp;quot;: &amp;quot;title&amp;quot;&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
      &amp;quot;sourceColumn&amp;quot;: &amp;quot;cid&amp;quot;,&lt;br /&gt;
      &amp;quot;targetTable&amp;quot;: &amp;quot;#__banner_clients&amp;quot;,&lt;br /&gt;
      &amp;quot;targetColumn&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
      &amp;quot;displayColumn&amp;quot;: &amp;quot;name&amp;quot;&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
      &amp;quot;sourceColumn&amp;quot;: &amp;quot;created_by&amp;quot;,&lt;br /&gt;
      &amp;quot;targetTable&amp;quot;: &amp;quot;#__users&amp;quot;,&lt;br /&gt;
      &amp;quot;targetColumn&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
      &amp;quot;displayColumn&amp;quot;: &amp;quot;name&amp;quot;&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
      &amp;quot;sourceColumn&amp;quot;: &amp;quot;modified_by&amp;quot;,&lt;br /&gt;
      &amp;quot;targetTable&amp;quot;: &amp;quot;#__users&amp;quot;,&lt;br /&gt;
      &amp;quot;targetColumn&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
      &amp;quot;displayColumn&amp;quot;: &amp;quot;name&amp;quot;&lt;br /&gt;
    }&lt;br /&gt;
  ]&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Creating the record===&lt;br /&gt;
You can create records in the installer in one of three ways:&lt;br /&gt;
* in the [[J2.5:Managing_Component_Updates_(Script.php)#postflight|postflight]] method&lt;br /&gt;
* by creating a &amp;lt;tt&amp;gt;JTableContenttype&amp;lt;/tt&amp;gt; instance and adding a row, or&lt;br /&gt;
* by using SQL to create a record directly&lt;br /&gt;
&lt;br /&gt;
== Modify your component&#039;s table class (or classes if you have multiple tables) ==&lt;br /&gt;
&lt;br /&gt;
Add the following to your JTable constructor:&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;_observers = new JObserverUpdater($this);&lt;br /&gt;
JObserverMapper::attachAllObservers($this);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The addition material formerly located here is no longer needed after 3.1.4&#039;&#039;&#039; Please read the document history for this page if you are required to support 3.1.0 or 3.1.1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;For joomla 3.2 I needed this line:&#039;&#039;&#039; ( Dont forget to replace weblinks by your component name )&lt;br /&gt;
&lt;br /&gt;
JObserverMapper::addObserverClassToClass(&#039;JTableObserverTags&#039;, &#039;WeblinksTableWeblink&#039;, array(&#039;typeAlias&#039; =&amp;gt; &#039;com_weblinks.weblink&#039;));&lt;br /&gt;
&lt;br /&gt;
== Add tags to the getItem() method of the model ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: this was only required in 3.1.0 and 3.1.1. It should not be used in 3.1.4 or later.&#039;&#039;&#039; Please read the history of this page if you need instructions for older versions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add a tag field to edit screens ==&lt;br /&gt;
&lt;br /&gt;
In any edit layouts where you want to allow tagging, you need to add the field to the xml and the appropriate layouts if necessary. The core layouts us a JLayout to manage this and the same layout by any extension.&lt;br /&gt;
&lt;br /&gt;
Update: Note that in &#039;&#039;&#039;3.1.1 only&#039;&#039;&#039; there is special handling of this in the core.  Tags in the edit screen &#039;&#039;&#039;MUST&#039;&#039;&#039; be part of a metadata &amp;lt;fields&amp;gt;&amp;lt;/fields&amp;gt; group.  The core provides two JLayouts to help you manage standard layouts, one (details) for the metadata and one for the sidebar that includes the tabs. In update an extension to 3.1.4 you may need to fix adjust your edit views.&lt;br /&gt;
&lt;br /&gt;
In 3.1.4 or later this special handling is not necessary.  Best practice is to use the standard JLayouts since this provides a consistent experience for users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field loads all the Javascript libraries required. You don&#039;t need to worry about that.&lt;br /&gt;
&lt;br /&gt;
The field supports two modes:  &lt;br /&gt;
* &#039;&#039;&#039;Nested tags mode.&#039;&#039;&#039; Hierarchical tag list. Doesn&#039;t support on the fly tag creation.&lt;br /&gt;
* &#039;&#039;&#039;AJAX mode.&#039;&#039;&#039;  Tags are searched while user types (3 min. chars required to launch the AJAX search). Custom tags are added by pressing ENTER or COMMA keys. Tags show the global route/path. Example: &amp;lt;code&amp;gt;grandpa/parent/tag&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field mode can be forced or use the com_tags setting &amp;lt;code&amp;gt;Tag field mode&amp;lt;/code&amp;gt; to determine its mode. To set/force the field mode we have to add &#039;&#039;&#039;mode=&amp;quot;ajax&amp;quot;&#039;&#039;&#039; or &#039;&#039;&#039;mode=&amp;quot;nested&amp;quot;&#039;&#039;&#039; to the tag field definition.  &lt;br /&gt;
&lt;br /&gt;
Example of forced AJAX mode:  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The field also includes an attribute to allow/deny the user to enter custom values. Currently this only works in AJAX mode. The attribute has to be added to the field definition like **custom=&amp;quot;allow&amp;quot;** or **custom=&amp;quot;deny&amp;quot;**  &lt;br /&gt;
&lt;br /&gt;
Example field definition with custom tags denied:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot; custom=&amp;quot;denied&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
if it is not already there. Usually multiple should be true unless you have a specific reason for it not to be.&lt;br /&gt;
In the core components in administrator, the field is in the group shown on the right, below the language field.&lt;br /&gt;
&lt;br /&gt;
Note: As of 3.1.2 if you wish to use the field to designate parent tags you must add parent=&amp;quot;parent&amp;quot; to the xml definition of the field.&lt;br /&gt;
&lt;br /&gt;
== Prepare the view ==&lt;br /&gt;
&lt;br /&gt;
Add an appropriate version of this to your view.html.php file before loading the layout:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$item-&amp;gt;tags = new JHelperTags;&lt;br /&gt;
$item-&amp;gt;tags-&amp;gt;getItemTags(&#039;com_newsfeeds.newsfeed.&#039; , $this-&amp;gt;item-&amp;gt;id); &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter should match a type in the types table. The second parameter is the primary key under which this record is stored in your table. This would be used in any display in any view where you want the tags associated with the item to display.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Set up the display ==&lt;br /&gt;
&lt;br /&gt;
In any layout where you want to display the tags associated with an item add:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Changing the object and property names as appropriate.&lt;br /&gt;
&lt;br /&gt;
You will most likely want to add the show_tags parameter to the item parameters, the menu item parameters and component configuration as appropriate for your use case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;?php if ($this-&amp;gt;params-&amp;gt;get(&#039;show_tags&#039;, 1) &amp;amp;&amp;amp; !empty($this-&amp;gt;item-&amp;gt;tags)) : ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
	&amp;lt;?php endif; ?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Batch processing ==&lt;br /&gt;
If you want to add the ability to do batch tagging to a backend list view do the following.&lt;br /&gt;
&lt;br /&gt;
Add tag to the default_batch layout&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
        &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&lt;br /&gt;
				&amp;lt;?php echo JHtml::_(&#039;batch.tag&#039;);?&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;/div&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add tag to the button class in the modal footer (Not totally necessary at this point, but good for potential future changes e.g. if unTag is added.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;button class=&amp;quot;btn&amp;quot; type=&amp;quot;button&amp;quot; onclick=&amp;quot;document.id(&#039;batch-category-id&#039;).value=&#039;&#039;;document.id(&#039;batch-access&#039;).value=&#039;&#039;;document.id(&#039;batch-language-id&#039;).value=&#039;&#039;;document.id(&#039;batch-user-id&#039;).value=&#039;&#039;;document.id(&#039;batch-tag-id)&#039;).value=&#039;&#039;&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a batch method to your model if you are not extending JModelAdmin or overriding the batch method. ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Batch tag a list of item.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @param   integer  $value     The value of the new tag.&lt;br /&gt;
	 * @param   array    $pks       An array of row IDs.&lt;br /&gt;
	 * @param   array    $contexts  An array of item contexts.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @return  void.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @since   3.1&lt;br /&gt;
	 */&lt;br /&gt;
	protected function batchTag($value, $pks, $contexts)&lt;br /&gt;
	{&lt;br /&gt;
		$tagsHelper = new JHelperTags();&lt;br /&gt;
		$tagsHelper-&amp;gt;tagItems($value, $pks, $contexts);&lt;br /&gt;
&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And modify your batch method by adding&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		if (!empty($commands[&#039;tag&#039;]))&lt;br /&gt;
		{&lt;br /&gt;
			if (!$this-&amp;gt;batchTag($commands[&#039;tag&#039;], $pks, $contexts))&lt;br /&gt;
			{&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			$done = true;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pay attention to any JSON encoded strings that you need special handling for in batch processing -- remember that you are saving a copy of the core fields and you need save to work as expected.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s it, now create some tags, tag some items and you are set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]][[Category:Content Tags]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117745</id>
		<title>J3.x:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117745"/>
		<updated>2014-04-26T15:56:00Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* Register a content type for each view */  Added Content History Options&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{copyedit}}&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Joomla&#039;s tagging system is used in all core content extensions and  is designed to be be easy to integrate into other extensions that use standard Joomla design patterns. &lt;br /&gt;
&lt;br /&gt;
Using tags in an extension is fairly straightforward. It requires these changes to a typical extension:&lt;br /&gt;
&lt;br /&gt;
* Register a &#039;Content type&#039; for the extension view(s)&lt;br /&gt;
* Add &#039;Observer methods&#039; to the extension table class(es)&lt;br /&gt;
* Add &#039;Tag fields&#039; to the extension edit forms&lt;br /&gt;
* Add &#039;Tag display&#039; code to the extension view files &lt;br /&gt;
* Optionally, add a batch tagging method to the extension administration&lt;br /&gt;
&lt;br /&gt;
== Register a content type for each view ==&lt;br /&gt;
&lt;br /&gt;
In Joomla! 3 content types e.g. articles, weblinks, contacts, . . . are registered in the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table with a separate record for each content view e.g. article, weblink, article category, weblink category, . . .&lt;br /&gt;
&lt;br /&gt;
Your extension installer needs to create a new record for each view.&lt;br /&gt;
&lt;br /&gt;
You can create records in the installer in three different ways:&lt;br /&gt;
* in the [[J2.5:Managing_Component_Updates_(Script.php)#postflight|postflight]] method&lt;br /&gt;
* by creating a &amp;lt;tt&amp;gt;JTableContenttype&amp;lt;/tt&amp;gt; instance and adding a row, or&lt;br /&gt;
* by using SQL to create a record directly&lt;br /&gt;
&lt;br /&gt;
The structure of the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table is fairly straightforward:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Column name !! Column type !! Purpose &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_id&amp;lt;/tt&amp;gt; || int(10) || Record key&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt;|| varchar(255)|| Type title e.g. &amp;lt;tt&amp;gt;Article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_alias&amp;lt;/tt&amp;gt; || varchar(255)|| Type alias e.g. &amp;lt;tt&amp;gt;com_content.article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt; || varchar(255)|| Information about the Table class&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt; || text || Not currently used&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt; || text || Maps the table column names to standard Joomla! names &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt; || varchar(255)|| Optional: name of a router method&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;content_history_options&amp;lt;/tt&amp;gt; || varchar(5120)|| Optional: ????&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Construct the Content Type entry===&lt;br /&gt;
&lt;br /&gt;
For each table/option/view you will need to make an entry in &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table (see above).  &lt;br /&gt;
&lt;br /&gt;
====type_id====&lt;br /&gt;
This is the auto-incremented record key, nothing to be done here.&lt;br /&gt;
&lt;br /&gt;
====type_title====&lt;br /&gt;
&lt;br /&gt;
The title for your Content Type e.g. Article, Contact, Weblink,  &lt;br /&gt;
You can do this either using sql or postflight by creating an instance of JTableContenttype. Don&#039;t forget a category type if you use the Joomla categories API.&lt;br /&gt;
&lt;br /&gt;
====type_alias====&lt;br /&gt;
&lt;br /&gt;
A string identifying the component and view (that would be in the page request, typically matching the model name) e.g. &amp;lt;tt&amp;gt;com_contact.contact&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;com_weblinks.weblink&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====table====&lt;br /&gt;
&lt;br /&gt;
You need to identify which tables used by your component contain records to be tagged. For example the &#039;Contact&#039; view of the Contacts component uses &amp;lt;tt&amp;gt;#__contact_details&amp;lt;/tt&amp;gt; and similarly the &#039;Weblinks&#039; view of the Weblinks component uses &amp;lt;tt&amp;gt;#__weblinks &amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Each of these components uses a class to write to the table: &amp;lt;tt&amp;gt;ContactTableContact&amp;lt;/tt&amp;gt; for the Contacts component and &amp;lt;tt&amp;gt;WeblinksTableWeblink&amp;lt;/tt&amp;gt; for the Weblinks component. You will need the class prefixes e.g. &amp;lt;tt&amp;gt;ContactTable&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;WeblinksTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that Category types all use the &amp;lt;tt&amp;gt;#__categories&amp;lt;/tt&amp;gt; table.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;type_table&amp;lt;/tt&amp;gt; entry gives the complete table class information for the table class as a JSON object with two elements. The first element represents your &amp;quot;special&amp;quot; table and the second an optional common table (otherwise it will default the &amp;lt;tt&amp;gt;JTableCorecontent&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Here is the JSON entry for the Weblink content type:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__weblinks&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Weblink&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;WeblinksTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__ucm_content&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;ucm_id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Corecontent&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;JTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
The values to be included are:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Entry name !! Content &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;dbtable&amp;lt;/tt&amp;gt; || Table name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;key&amp;lt;/tt&amp;gt;|| Primary key name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type&amp;lt;/tt&amp;gt; || Content type&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;prefix&amp;lt;/tt&amp;gt; || Class prefix&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt; || An option array, as used in your component constructor and getInstance() methods, may be empty&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This information enables the tagging system (and other APIs) to access your table easily. &lt;br /&gt;
&lt;br /&gt;
If you are using Joomla categories make sure to create a category type so that they can be tagged. In Joomla 3.1 and 3.1.1 there is an error where the tag field will show even if there is not a type, but this is corrected in 3.1.4.  &lt;br /&gt;
  &lt;br /&gt;
=====Notes=====&lt;br /&gt;
&lt;br /&gt;
* The table name for the common table is &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt;; this is incorrect in 3.1 and 3.1.1 data but is not currently used. The data was updated in 3.1.4.&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt; field would potentially be used for display although this is not implemented currently except in the contenttype field.  Usually it should begin with an upper case letter if it is in English. See note about how to make this translatable. To make your type names translatable add &amp;lt;tt&amp;gt;COM_TAGS_CONTENT_TYPE_+type_title=&amp;quot;Type Title&amp;quot;&amp;lt;/tt&amp;gt; in both the ini and sys.ini files.&lt;br /&gt;
&lt;br /&gt;
====rules====&lt;br /&gt;
&lt;br /&gt;
Rules is currently not used. It will likely be removed in favor of an &amp;lt;tt&amp;gt;asset_id&amp;lt;/tt&amp;gt; for each type, but currently you can ignore this field which will be managed by &amp;lt;tt&amp;gt;JTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====field_mappings====&lt;br /&gt;
&lt;br /&gt;
This entry maps the names of specific fields in your table to a set of standard Joomla! names. This mapping is stored as a JSON array with the first &#039;common&#039; element mapping to the common fields and the second &#039;special&#039; element mapping the other fields from the table. &lt;br /&gt;
&lt;br /&gt;
Here is the field_mappings entry for the Banner Content Type (chosen because it is a richer example than Article or WebLink). Note that &#039;common&#039; mappings that have no equivalent are included as &#039;null&#039; (leaving them blank may cause sql issues); and the names and values in the &#039;special&#039; element all match as they are limited to this component.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;core_content_item_id&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;core_title&amp;quot;: &amp;quot;name&amp;quot;,&lt;br /&gt;
    &amp;quot;core_state&amp;quot;: &amp;quot;published&amp;quot;,&lt;br /&gt;
    &amp;quot;core_alias&amp;quot;: &amp;quot;alias&amp;quot;,&lt;br /&gt;
    &amp;quot;core_created_time&amp;quot;: &amp;quot;created&amp;quot;,&lt;br /&gt;
    &amp;quot;core_modified_time&amp;quot;: &amp;quot;modified&amp;quot;,&lt;br /&gt;
    &amp;quot;core_body&amp;quot;: &amp;quot;description&amp;quot;,&lt;br /&gt;
    &amp;quot;core_hits&amp;quot;: &amp;quot;null&amp;quot;,&lt;br /&gt;
    &amp;quot;core_publish_up&amp;quot;: &amp;quot;publish_up&amp;quot;,&lt;br /&gt;
    &amp;quot;core_publish_down&amp;quot;: &amp;quot;publish_down&amp;quot;,&lt;br /&gt;
    &amp;quot;core_access&amp;quot;: &amp;quot;access&amp;quot;,&lt;br /&gt;
    &amp;quot;core_params&amp;quot;: &amp;quot;params&amp;quot;,&lt;br /&gt;
    &amp;quot;core_featured&amp;quot;: &amp;quot;null&amp;quot;,&lt;br /&gt;
    &amp;quot;core_metadata&amp;quot;: &amp;quot;metadata&amp;quot;,&lt;br /&gt;
    &amp;quot;core_language&amp;quot;: &amp;quot;language&amp;quot;,&lt;br /&gt;
    &amp;quot;core_images&amp;quot;: &amp;quot;images&amp;quot;,&lt;br /&gt;
    &amp;quot;core_urls&amp;quot;: &amp;quot;link&amp;quot;,&lt;br /&gt;
    &amp;quot;core_version&amp;quot;: &amp;quot;version&amp;quot;,&lt;br /&gt;
    &amp;quot;core_ordering&amp;quot;: &amp;quot;ordering&amp;quot;,&lt;br /&gt;
    &amp;quot;core_metakey&amp;quot;: &amp;quot;metakey&amp;quot;,&lt;br /&gt;
    &amp;quot;core_metadesc&amp;quot;: &amp;quot;metadesc&amp;quot;,&lt;br /&gt;
    &amp;quot;core_catid&amp;quot;: &amp;quot;catid&amp;quot;,&lt;br /&gt;
    &amp;quot;core_xreference&amp;quot;: &amp;quot;null&amp;quot;,&lt;br /&gt;
    &amp;quot;asset_id&amp;quot;: &amp;quot;null&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;imptotal&amp;quot;: &amp;quot;imptotal&amp;quot;,&lt;br /&gt;
    &amp;quot;impmade&amp;quot;: &amp;quot;impmade&amp;quot;,&lt;br /&gt;
    &amp;quot;clicks&amp;quot;: &amp;quot;clicks&amp;quot;,&lt;br /&gt;
    &amp;quot;clickurl&amp;quot;: &amp;quot;clickurl&amp;quot;,&lt;br /&gt;
    &amp;quot;custombannercode&amp;quot;: &amp;quot;custombannercode&amp;quot;,&lt;br /&gt;
    &amp;quot;cid&amp;quot;: &amp;quot;cid&amp;quot;,&lt;br /&gt;
    &amp;quot;purchase_type&amp;quot;: &amp;quot;purchase_type&amp;quot;,&lt;br /&gt;
    &amp;quot;track_impressions&amp;quot;: &amp;quot;track_impressions&amp;quot;,&lt;br /&gt;
    &amp;quot;track_clicks&amp;quot;: &amp;quot;track_clicks&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
At a minimum for common fields you need to map: content_item_id, alias and title in order to successfully create urls in the tagged items list.  You also will probably want: access, status, and language. The special fields are optional.  &lt;br /&gt;
&lt;br /&gt;
=====Note=====&lt;br /&gt;
* The &amp;lt;tt&amp;gt;JHelperTags&amp;lt;/tt&amp;gt; and JUcm APIs at 3.1.1 supported arrays for this field, but as of 3.1.4 either arrays or objects are supported; the default is objects.&lt;br /&gt;
&lt;br /&gt;
====router====&lt;br /&gt;
&lt;br /&gt;
This is an optional entry to include the name of a static helper router method for this type found in its front end helpers folder e.g. &amp;lt;tt&amp;gt;WeblinksHelperRoute::getWeblinkRoute&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If you do not have a custom router then Tags falls back to the rules in &amp;lt;tt&amp;gt;JHelperRoute&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If you only store data in &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt; you will eventually be able to leave the router field blank although this option is not currently implemented.&lt;br /&gt;
&lt;br /&gt;
====content_history_options====&lt;br /&gt;
&lt;br /&gt;
????&lt;br /&gt;
&lt;br /&gt;
Here&#039;s the entry for the Banner Content Type:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;formFile&amp;quot;: &amp;quot;administrator\/components\/com_banners\/models\/forms\/banner.xml&amp;quot;,&lt;br /&gt;
  &amp;quot;hideFields&amp;quot;: [&lt;br /&gt;
    &amp;quot;checked_out&amp;quot;,&lt;br /&gt;
    &amp;quot;checked_out_time&amp;quot;,&lt;br /&gt;
    &amp;quot;version&amp;quot;,&lt;br /&gt;
    &amp;quot;reset&amp;quot;&lt;br /&gt;
  ],&lt;br /&gt;
  &amp;quot;ignoreChanges&amp;quot;: [&lt;br /&gt;
    &amp;quot;modified_by&amp;quot;,&lt;br /&gt;
    &amp;quot;modified&amp;quot;,&lt;br /&gt;
    &amp;quot;checked_out&amp;quot;,&lt;br /&gt;
    &amp;quot;checked_out_time&amp;quot;,&lt;br /&gt;
    &amp;quot;version&amp;quot;,&lt;br /&gt;
    &amp;quot;imptotal&amp;quot;,&lt;br /&gt;
    &amp;quot;impmade&amp;quot;,&lt;br /&gt;
    &amp;quot;reset&amp;quot;&lt;br /&gt;
  ],&lt;br /&gt;
  &amp;quot;convertToInt&amp;quot;: [&lt;br /&gt;
    &amp;quot;publish_up&amp;quot;,&lt;br /&gt;
    &amp;quot;publish_down&amp;quot;,&lt;br /&gt;
    &amp;quot;ordering&amp;quot;&lt;br /&gt;
  ],&lt;br /&gt;
  &amp;quot;displayLookup&amp;quot;: [&lt;br /&gt;
    {&lt;br /&gt;
      &amp;quot;sourceColumn&amp;quot;: &amp;quot;catid&amp;quot;,&lt;br /&gt;
      &amp;quot;targetTable&amp;quot;: &amp;quot;#__categories&amp;quot;,&lt;br /&gt;
      &amp;quot;targetColumn&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
      &amp;quot;displayColumn&amp;quot;: &amp;quot;title&amp;quot;&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
      &amp;quot;sourceColumn&amp;quot;: &amp;quot;cid&amp;quot;,&lt;br /&gt;
      &amp;quot;targetTable&amp;quot;: &amp;quot;#__banner_clients&amp;quot;,&lt;br /&gt;
      &amp;quot;targetColumn&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
      &amp;quot;displayColumn&amp;quot;: &amp;quot;name&amp;quot;&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
      &amp;quot;sourceColumn&amp;quot;: &amp;quot;created_by&amp;quot;,&lt;br /&gt;
      &amp;quot;targetTable&amp;quot;: &amp;quot;#__users&amp;quot;,&lt;br /&gt;
      &amp;quot;targetColumn&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
      &amp;quot;displayColumn&amp;quot;: &amp;quot;name&amp;quot;&lt;br /&gt;
    },&lt;br /&gt;
    {&lt;br /&gt;
      &amp;quot;sourceColumn&amp;quot;: &amp;quot;modified_by&amp;quot;,&lt;br /&gt;
      &amp;quot;targetTable&amp;quot;: &amp;quot;#__users&amp;quot;,&lt;br /&gt;
      &amp;quot;targetColumn&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
      &amp;quot;displayColumn&amp;quot;: &amp;quot;name&amp;quot;&lt;br /&gt;
    }&lt;br /&gt;
  ]&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Modify your component&#039;s table class (or classes if you have multiple tables) ==&lt;br /&gt;
&lt;br /&gt;
Add the following to your JTable constructor:&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;_observers = new JObserverUpdater($this);&lt;br /&gt;
JObserverMapper::attachAllObservers($this);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The addition material formerly located here is no longer needed after 3.1.4&#039;&#039;&#039; Please read the document history for this page if you are required to support 3.1.0 or 3.1.1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;For joomla 3.2 I needed this line:&#039;&#039;&#039; ( Dont forget to replace weblinks by your component name )&lt;br /&gt;
&lt;br /&gt;
JObserverMapper::addObserverClassToClass(&#039;JTableObserverTags&#039;, &#039;WeblinksTableWeblink&#039;, array(&#039;typeAlias&#039; =&amp;gt; &#039;com_weblinks.weblink&#039;));&lt;br /&gt;
&lt;br /&gt;
== Add tags to the getItem() method of the model ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: this was only required in 3.1.0 and 3.1.1. It should not be used in 3.1.4 or later.&#039;&#039;&#039; Please read the history of this page if you need instructions for older versions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add a tag field to edit screens ==&lt;br /&gt;
&lt;br /&gt;
In any edit layouts where you want to allow tagging, you need to add the field to the xml and the appropriate layouts if necessary. The core layouts us a JLayout to manage this and the same layout by any extension.&lt;br /&gt;
&lt;br /&gt;
Update: Note that in &#039;&#039;&#039;3.1.1 only&#039;&#039;&#039; there is special handling of this in the core.  Tags in the edit screen &#039;&#039;&#039;MUST&#039;&#039;&#039; be part of a metadata &amp;lt;fields&amp;gt;&amp;lt;/fields&amp;gt; group.  The core provides two JLayouts to help you manage standard layouts, one (details) for the metadata and one for the sidebar that includes the tabs. In update an extension to 3.1.4 you may need to fix adjust your edit views.&lt;br /&gt;
&lt;br /&gt;
In 3.1.4 or later this special handling is not necessary.  Best practice is to use the standard JLayouts since this provides a consistent experience for users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field loads all the Javascript libraries required. You don&#039;t need to worry about that.&lt;br /&gt;
&lt;br /&gt;
The field supports two modes:  &lt;br /&gt;
* &#039;&#039;&#039;Nested tags mode.&#039;&#039;&#039; Hierarchical tag list. Doesn&#039;t support on the fly tag creation.&lt;br /&gt;
* &#039;&#039;&#039;AJAX mode.&#039;&#039;&#039;  Tags are searched while user types (3 min. chars required to launch the AJAX search). Custom tags are added by pressing ENTER or COMMA keys. Tags show the global route/path. Example: &amp;lt;code&amp;gt;grandpa/parent/tag&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field mode can be forced or use the com_tags setting &amp;lt;code&amp;gt;Tag field mode&amp;lt;/code&amp;gt; to determine its mode. To set/force the field mode we have to add &#039;&#039;&#039;mode=&amp;quot;ajax&amp;quot;&#039;&#039;&#039; or &#039;&#039;&#039;mode=&amp;quot;nested&amp;quot;&#039;&#039;&#039; to the tag field definition.  &lt;br /&gt;
&lt;br /&gt;
Example of forced AJAX mode:  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The field also includes an attribute to allow/deny the user to enter custom values. Currently this only works in AJAX mode. The attribute has to be added to the field definition like **custom=&amp;quot;allow&amp;quot;** or **custom=&amp;quot;deny&amp;quot;**  &lt;br /&gt;
&lt;br /&gt;
Example field definition with custom tags denied:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot; custom=&amp;quot;denied&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
if it is not already there. Usually multiple should be true unless you have a specific reason for it not to be.&lt;br /&gt;
In the core components in administrator, the field is in the group shown on the right, below the language field.&lt;br /&gt;
&lt;br /&gt;
Note: As of 3.1.2 if you wish to use the field to designate parent tags you must add parent=&amp;quot;parent&amp;quot; to the xml definition of the field.&lt;br /&gt;
&lt;br /&gt;
== Prepare the view ==&lt;br /&gt;
&lt;br /&gt;
Add an appropriate version of this to your view.html.php file before loading the layout:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$item-&amp;gt;tags = new JHelperTags;&lt;br /&gt;
$item-&amp;gt;tags-&amp;gt;getItemTags(&#039;com_newsfeeds.newsfeed.&#039; , $this-&amp;gt;item-&amp;gt;id); &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter should match a type in the types table. The second parameter is the primary key under which this record is stored in your table. This would be used in any display in any view where you want the tags associated with the item to display.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Set up the display ==&lt;br /&gt;
&lt;br /&gt;
In any layout where you want to display the tags associated with an item add:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Changing the object and property names as appropriate.&lt;br /&gt;
&lt;br /&gt;
You will most likely want to add the show_tags parameter to the item parameters, the menu item parameters and component configuration as appropriate for your use case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;?php if ($this-&amp;gt;params-&amp;gt;get(&#039;show_tags&#039;, 1) &amp;amp;&amp;amp; !empty($this-&amp;gt;item-&amp;gt;tags)) : ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
	&amp;lt;?php endif; ?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Batch processing ==&lt;br /&gt;
If you want to add the ability to do batch tagging to a backend list view do the following.&lt;br /&gt;
&lt;br /&gt;
Add tag to the default_batch layout&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
        &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&lt;br /&gt;
				&amp;lt;?php echo JHtml::_(&#039;batch.tag&#039;);?&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;/div&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add tag to the button class in the modal footer (Not totally necessary at this point, but good for potential future changes e.g. if unTag is added.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;button class=&amp;quot;btn&amp;quot; type=&amp;quot;button&amp;quot; onclick=&amp;quot;document.id(&#039;batch-category-id&#039;).value=&#039;&#039;;document.id(&#039;batch-access&#039;).value=&#039;&#039;;document.id(&#039;batch-language-id&#039;).value=&#039;&#039;;document.id(&#039;batch-user-id&#039;).value=&#039;&#039;;document.id(&#039;batch-tag-id)&#039;).value=&#039;&#039;&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a batch method to your model if you are not extending JModelAdmin or overriding the batch method. ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Batch tag a list of item.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @param   integer  $value     The value of the new tag.&lt;br /&gt;
	 * @param   array    $pks       An array of row IDs.&lt;br /&gt;
	 * @param   array    $contexts  An array of item contexts.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @return  void.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @since   3.1&lt;br /&gt;
	 */&lt;br /&gt;
	protected function batchTag($value, $pks, $contexts)&lt;br /&gt;
	{&lt;br /&gt;
		$tagsHelper = new JHelperTags();&lt;br /&gt;
		$tagsHelper-&amp;gt;tagItems($value, $pks, $contexts);&lt;br /&gt;
&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And modify your batch method by adding&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		if (!empty($commands[&#039;tag&#039;]))&lt;br /&gt;
		{&lt;br /&gt;
			if (!$this-&amp;gt;batchTag($commands[&#039;tag&#039;], $pks, $contexts))&lt;br /&gt;
			{&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			$done = true;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pay attention to any JSON encoded strings that you need special handling for in batch processing -- remember that you are saving a copy of the core fields and you need save to work as expected.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s it, now create some tags, tag some items and you are set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]][[Category:Content Tags]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117744</id>
		<title>J3.x:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117744"/>
		<updated>2014-04-26T15:49:46Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* Note */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{copyedit}}&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Joomla&#039;s tagging system is used in all core content extensions and  is designed to be be easy to integrate into other extensions that use standard Joomla design patterns. &lt;br /&gt;
&lt;br /&gt;
Using tags in an extension is fairly straightforward. It requires these changes to a typical extension:&lt;br /&gt;
&lt;br /&gt;
* Register a &#039;Content type&#039; for the extension view(s)&lt;br /&gt;
* Add &#039;Observer methods&#039; to the extension table class(es)&lt;br /&gt;
* Add &#039;Tag fields&#039; to the extension edit forms&lt;br /&gt;
* Add &#039;Tag display&#039; code to the extension view files &lt;br /&gt;
* Optionally, add a batch tagging method to the extension administration&lt;br /&gt;
&lt;br /&gt;
== Register a content type for each view ==&lt;br /&gt;
&lt;br /&gt;
In Joomla! 3 content types e.g. articles, weblinks, contacts, . . . are registered in the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table with a separate record for each content view e.g. article, weblink, article category, weblink category, . . .&lt;br /&gt;
&lt;br /&gt;
Your extension installer needs to create a new record for each view.&lt;br /&gt;
&lt;br /&gt;
You can create records in the installer in three different ways:&lt;br /&gt;
* in the [[J2.5:Managing_Component_Updates_(Script.php)#postflight|postflight]] method&lt;br /&gt;
* by creating a &amp;lt;tt&amp;gt;JTableContenttype&amp;lt;/tt&amp;gt; instance and adding a row, or&lt;br /&gt;
* by using SQL to create a record directly&lt;br /&gt;
&lt;br /&gt;
The structure of the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table is fairly straightforward:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Column name !! Column type !! Purpose &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_id&amp;lt;/tt&amp;gt; || int(10) || Record key&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt;|| varchar(255)|| Type title e.g. &amp;lt;tt&amp;gt;Article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_alias&amp;lt;/tt&amp;gt; || varchar(255)|| Type alias e.g. &amp;lt;tt&amp;gt;com_content.article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt; || varchar(255)|| Information about the Table class&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt; || text || Not currently used&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt; || text || Maps the table column names to standard Joomla! names &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt; || varchar(255)|| Optional: name of a router method&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Construct the Content Type entry===&lt;br /&gt;
&lt;br /&gt;
For each table/option/view you will need to make an entry in &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table (see above).  &lt;br /&gt;
&lt;br /&gt;
====type_id====&lt;br /&gt;
This is the auto-incremented record key, nothing to be done here.&lt;br /&gt;
&lt;br /&gt;
====type_title====&lt;br /&gt;
&lt;br /&gt;
The title for your Content Type e.g. Article, Contact, Weblink,  &lt;br /&gt;
You can do this either using sql or postflight by creating an instance of JTableContenttype. Don&#039;t forget a category type if you use the Joomla categories API.&lt;br /&gt;
&lt;br /&gt;
====type_alias====&lt;br /&gt;
&lt;br /&gt;
A string identifying the component and view (that would be in the page request, typically matching the model name) e.g. &amp;lt;tt&amp;gt;com_contact.contact&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;com_weblinks.weblink&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====table====&lt;br /&gt;
&lt;br /&gt;
You need to identify which tables used by your component contain records to be tagged. For example the &#039;Contact&#039; view of the Contacts component uses &amp;lt;tt&amp;gt;#__contact_details&amp;lt;/tt&amp;gt; and similarly the &#039;Weblinks&#039; view of the Weblinks component uses &amp;lt;tt&amp;gt;#__weblinks &amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Each of these components uses a class to write to the table: &amp;lt;tt&amp;gt;ContactTableContact&amp;lt;/tt&amp;gt; for the Contacts component and &amp;lt;tt&amp;gt;WeblinksTableWeblink&amp;lt;/tt&amp;gt; for the Weblinks component. You will need the class prefixes e.g. &amp;lt;tt&amp;gt;ContactTable&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;WeblinksTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that Category types all use the &amp;lt;tt&amp;gt;#__categories&amp;lt;/tt&amp;gt; table.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;type_table&amp;lt;/tt&amp;gt; entry gives the complete table class information for the table class as a JSON object with two elements. The first element represents your &amp;quot;special&amp;quot; table and the second an optional common table (otherwise it will default the &amp;lt;tt&amp;gt;JTableCorecontent&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Here is the JSON entry for the Weblink content type:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__weblinks&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Weblink&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;WeblinksTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__ucm_content&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;ucm_id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Corecontent&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;JTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
The values to be included are:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Entry name !! Content &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;dbtable&amp;lt;/tt&amp;gt; || Table name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;key&amp;lt;/tt&amp;gt;|| Primary key name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type&amp;lt;/tt&amp;gt; || Content type&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;prefix&amp;lt;/tt&amp;gt; || Class prefix&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt; || An option array, as used in your component constructor and getInstance() methods, may be empty&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This information enables the tagging system (and other APIs) to access your table easily. &lt;br /&gt;
&lt;br /&gt;
If you are using Joomla categories make sure to create a category type so that they can be tagged. In Joomla 3.1 and 3.1.1 there is an error where the tag field will show even if there is not a type, but this is corrected in 3.1.4.  &lt;br /&gt;
  &lt;br /&gt;
=====Notes=====&lt;br /&gt;
&lt;br /&gt;
* The table name for the common table is &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt;; this is incorrect in 3.1 and 3.1.1 data but is not currently used. The data was updated in 3.1.4.&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt; field would potentially be used for display although this is not implemented currently except in the contenttype field.  Usually it should begin with an upper case letter if it is in English. See note about how to make this translatable. To make your type names translatable add &amp;lt;tt&amp;gt;COM_TAGS_CONTENT_TYPE_+type_title=&amp;quot;Type Title&amp;quot;&amp;lt;/tt&amp;gt; in both the ini and sys.ini files.&lt;br /&gt;
&lt;br /&gt;
====rules====&lt;br /&gt;
&lt;br /&gt;
Rules is currently not used. It will likely be removed in favor of an &amp;lt;tt&amp;gt;asset_id&amp;lt;/tt&amp;gt; for each type, but currently you can ignore this field which will be managed by &amp;lt;tt&amp;gt;JTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====field_mappings====&lt;br /&gt;
&lt;br /&gt;
This entry maps the names of specific fields in your table to a set of standard Joomla! names. This mapping is stored as a JSON array with the first &#039;common&#039; element mapping to the common fields and the second &#039;special&#039; element mapping the other fields from the table. &lt;br /&gt;
&lt;br /&gt;
Here is the field_mappings entry for the Banner Content Type (chosen because it is a richer example than Article or WebLink). Note that &#039;common&#039; mappings that have no equivalent are included as &#039;null&#039; (leaving them blank may cause sql issues); and the names and values in the &#039;special&#039; element all match as they are limited to this component.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;core_content_item_id&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;core_title&amp;quot;: &amp;quot;name&amp;quot;,&lt;br /&gt;
    &amp;quot;core_state&amp;quot;: &amp;quot;published&amp;quot;,&lt;br /&gt;
    &amp;quot;core_alias&amp;quot;: &amp;quot;alias&amp;quot;,&lt;br /&gt;
    &amp;quot;core_created_time&amp;quot;: &amp;quot;created&amp;quot;,&lt;br /&gt;
    &amp;quot;core_modified_time&amp;quot;: &amp;quot;modified&amp;quot;,&lt;br /&gt;
    &amp;quot;core_body&amp;quot;: &amp;quot;description&amp;quot;,&lt;br /&gt;
    &amp;quot;core_hits&amp;quot;: &amp;quot;null&amp;quot;,&lt;br /&gt;
    &amp;quot;core_publish_up&amp;quot;: &amp;quot;publish_up&amp;quot;,&lt;br /&gt;
    &amp;quot;core_publish_down&amp;quot;: &amp;quot;publish_down&amp;quot;,&lt;br /&gt;
    &amp;quot;core_access&amp;quot;: &amp;quot;access&amp;quot;,&lt;br /&gt;
    &amp;quot;core_params&amp;quot;: &amp;quot;params&amp;quot;,&lt;br /&gt;
    &amp;quot;core_featured&amp;quot;: &amp;quot;null&amp;quot;,&lt;br /&gt;
    &amp;quot;core_metadata&amp;quot;: &amp;quot;metadata&amp;quot;,&lt;br /&gt;
    &amp;quot;core_language&amp;quot;: &amp;quot;language&amp;quot;,&lt;br /&gt;
    &amp;quot;core_images&amp;quot;: &amp;quot;images&amp;quot;,&lt;br /&gt;
    &amp;quot;core_urls&amp;quot;: &amp;quot;link&amp;quot;,&lt;br /&gt;
    &amp;quot;core_version&amp;quot;: &amp;quot;version&amp;quot;,&lt;br /&gt;
    &amp;quot;core_ordering&amp;quot;: &amp;quot;ordering&amp;quot;,&lt;br /&gt;
    &amp;quot;core_metakey&amp;quot;: &amp;quot;metakey&amp;quot;,&lt;br /&gt;
    &amp;quot;core_metadesc&amp;quot;: &amp;quot;metadesc&amp;quot;,&lt;br /&gt;
    &amp;quot;core_catid&amp;quot;: &amp;quot;catid&amp;quot;,&lt;br /&gt;
    &amp;quot;core_xreference&amp;quot;: &amp;quot;null&amp;quot;,&lt;br /&gt;
    &amp;quot;asset_id&amp;quot;: &amp;quot;null&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;imptotal&amp;quot;: &amp;quot;imptotal&amp;quot;,&lt;br /&gt;
    &amp;quot;impmade&amp;quot;: &amp;quot;impmade&amp;quot;,&lt;br /&gt;
    &amp;quot;clicks&amp;quot;: &amp;quot;clicks&amp;quot;,&lt;br /&gt;
    &amp;quot;clickurl&amp;quot;: &amp;quot;clickurl&amp;quot;,&lt;br /&gt;
    &amp;quot;custombannercode&amp;quot;: &amp;quot;custombannercode&amp;quot;,&lt;br /&gt;
    &amp;quot;cid&amp;quot;: &amp;quot;cid&amp;quot;,&lt;br /&gt;
    &amp;quot;purchase_type&amp;quot;: &amp;quot;purchase_type&amp;quot;,&lt;br /&gt;
    &amp;quot;track_impressions&amp;quot;: &amp;quot;track_impressions&amp;quot;,&lt;br /&gt;
    &amp;quot;track_clicks&amp;quot;: &amp;quot;track_clicks&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
At a minimum for common fields you need to map: content_item_id, alias and title in order to successfully create urls in the tagged items list.  You also will probably want: access, status, and language. The special fields are optional.  &lt;br /&gt;
&lt;br /&gt;
=====Note=====&lt;br /&gt;
* The &amp;lt;tt&amp;gt;JHelperTags&amp;lt;/tt&amp;gt; and JUcm APIs at 3.1.1 supported arrays for this field, but as of 3.1.4 either arrays or objects are supported; the default is objects.&lt;br /&gt;
&lt;br /&gt;
====router====&lt;br /&gt;
&lt;br /&gt;
This is an optional entry to include the name of a static helper router method for this type found in its front end helpers folder e.g. &amp;lt;tt&amp;gt;WeblinksHelperRoute::getWeblinkRoute&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If you do not have a custom router then Tags falls back to the rules in &amp;lt;tt&amp;gt;JHelperRoute&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If you only store data in &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt; you will eventually be able to leave the router field blank although this option is not currently implemented.&lt;br /&gt;
&lt;br /&gt;
== Modify your component&#039;s table class (or classes if you have multiple tables) ==&lt;br /&gt;
&lt;br /&gt;
Add the following to your JTable constructor:&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;_observers = new JObserverUpdater($this);&lt;br /&gt;
JObserverMapper::attachAllObservers($this);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The addition material formerly located here is no longer needed after 3.1.4&#039;&#039;&#039; Please read the document history for this page if you are required to support 3.1.0 or 3.1.1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;For joomla 3.2 I needed this line:&#039;&#039;&#039; ( Dont forget to replace weblinks by your component name )&lt;br /&gt;
&lt;br /&gt;
JObserverMapper::addObserverClassToClass(&#039;JTableObserverTags&#039;, &#039;WeblinksTableWeblink&#039;, array(&#039;typeAlias&#039; =&amp;gt; &#039;com_weblinks.weblink&#039;));&lt;br /&gt;
&lt;br /&gt;
== Add tags to the getItem() method of the model ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: this was only required in 3.1.0 and 3.1.1. It should not be used in 3.1.4 or later.&#039;&#039;&#039; Please read the history of this page if you need instructions for older versions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add a tag field to edit screens ==&lt;br /&gt;
&lt;br /&gt;
In any edit layouts where you want to allow tagging, you need to add the field to the xml and the appropriate layouts if necessary. The core layouts us a JLayout to manage this and the same layout by any extension.&lt;br /&gt;
&lt;br /&gt;
Update: Note that in &#039;&#039;&#039;3.1.1 only&#039;&#039;&#039; there is special handling of this in the core.  Tags in the edit screen &#039;&#039;&#039;MUST&#039;&#039;&#039; be part of a metadata &amp;lt;fields&amp;gt;&amp;lt;/fields&amp;gt; group.  The core provides two JLayouts to help you manage standard layouts, one (details) for the metadata and one for the sidebar that includes the tabs. In update an extension to 3.1.4 you may need to fix adjust your edit views.&lt;br /&gt;
&lt;br /&gt;
In 3.1.4 or later this special handling is not necessary.  Best practice is to use the standard JLayouts since this provides a consistent experience for users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field loads all the Javascript libraries required. You don&#039;t need to worry about that.&lt;br /&gt;
&lt;br /&gt;
The field supports two modes:  &lt;br /&gt;
* &#039;&#039;&#039;Nested tags mode.&#039;&#039;&#039; Hierarchical tag list. Doesn&#039;t support on the fly tag creation.&lt;br /&gt;
* &#039;&#039;&#039;AJAX mode.&#039;&#039;&#039;  Tags are searched while user types (3 min. chars required to launch the AJAX search). Custom tags are added by pressing ENTER or COMMA keys. Tags show the global route/path. Example: &amp;lt;code&amp;gt;grandpa/parent/tag&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field mode can be forced or use the com_tags setting &amp;lt;code&amp;gt;Tag field mode&amp;lt;/code&amp;gt; to determine its mode. To set/force the field mode we have to add &#039;&#039;&#039;mode=&amp;quot;ajax&amp;quot;&#039;&#039;&#039; or &#039;&#039;&#039;mode=&amp;quot;nested&amp;quot;&#039;&#039;&#039; to the tag field definition.  &lt;br /&gt;
&lt;br /&gt;
Example of forced AJAX mode:  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The field also includes an attribute to allow/deny the user to enter custom values. Currently this only works in AJAX mode. The attribute has to be added to the field definition like **custom=&amp;quot;allow&amp;quot;** or **custom=&amp;quot;deny&amp;quot;**  &lt;br /&gt;
&lt;br /&gt;
Example field definition with custom tags denied:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot; custom=&amp;quot;denied&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
if it is not already there. Usually multiple should be true unless you have a specific reason for it not to be.&lt;br /&gt;
In the core components in administrator, the field is in the group shown on the right, below the language field.&lt;br /&gt;
&lt;br /&gt;
Note: As of 3.1.2 if you wish to use the field to designate parent tags you must add parent=&amp;quot;parent&amp;quot; to the xml definition of the field.&lt;br /&gt;
&lt;br /&gt;
== Prepare the view ==&lt;br /&gt;
&lt;br /&gt;
Add an appropriate version of this to your view.html.php file before loading the layout:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$item-&amp;gt;tags = new JHelperTags;&lt;br /&gt;
$item-&amp;gt;tags-&amp;gt;getItemTags(&#039;com_newsfeeds.newsfeed.&#039; , $this-&amp;gt;item-&amp;gt;id); &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter should match a type in the types table. The second parameter is the primary key under which this record is stored in your table. This would be used in any display in any view where you want the tags associated with the item to display.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Set up the display ==&lt;br /&gt;
&lt;br /&gt;
In any layout where you want to display the tags associated with an item add:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Changing the object and property names as appropriate.&lt;br /&gt;
&lt;br /&gt;
You will most likely want to add the show_tags parameter to the item parameters, the menu item parameters and component configuration as appropriate for your use case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;?php if ($this-&amp;gt;params-&amp;gt;get(&#039;show_tags&#039;, 1) &amp;amp;&amp;amp; !empty($this-&amp;gt;item-&amp;gt;tags)) : ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
	&amp;lt;?php endif; ?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Batch processing ==&lt;br /&gt;
If you want to add the ability to do batch tagging to a backend list view do the following.&lt;br /&gt;
&lt;br /&gt;
Add tag to the default_batch layout&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
        &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&lt;br /&gt;
				&amp;lt;?php echo JHtml::_(&#039;batch.tag&#039;);?&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;/div&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add tag to the button class in the modal footer (Not totally necessary at this point, but good for potential future changes e.g. if unTag is added.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;button class=&amp;quot;btn&amp;quot; type=&amp;quot;button&amp;quot; onclick=&amp;quot;document.id(&#039;batch-category-id&#039;).value=&#039;&#039;;document.id(&#039;batch-access&#039;).value=&#039;&#039;;document.id(&#039;batch-language-id&#039;).value=&#039;&#039;;document.id(&#039;batch-user-id&#039;).value=&#039;&#039;;document.id(&#039;batch-tag-id)&#039;).value=&#039;&#039;&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a batch method to your model if you are not extending JModelAdmin or overriding the batch method. ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Batch tag a list of item.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @param   integer  $value     The value of the new tag.&lt;br /&gt;
	 * @param   array    $pks       An array of row IDs.&lt;br /&gt;
	 * @param   array    $contexts  An array of item contexts.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @return  void.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @since   3.1&lt;br /&gt;
	 */&lt;br /&gt;
	protected function batchTag($value, $pks, $contexts)&lt;br /&gt;
	{&lt;br /&gt;
		$tagsHelper = new JHelperTags();&lt;br /&gt;
		$tagsHelper-&amp;gt;tagItems($value, $pks, $contexts);&lt;br /&gt;
&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And modify your batch method by adding&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		if (!empty($commands[&#039;tag&#039;]))&lt;br /&gt;
		{&lt;br /&gt;
			if (!$this-&amp;gt;batchTag($commands[&#039;tag&#039;], $pks, $contexts))&lt;br /&gt;
			{&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			$done = true;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pay attention to any JSON encoded strings that you need special handling for in batch processing -- remember that you are saving a copy of the core fields and you need save to work as expected.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s it, now create some tags, tag some items and you are set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]][[Category:Content Tags]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117743</id>
		<title>J3.x:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117743"/>
		<updated>2014-04-26T15:48:24Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* router */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{copyedit}}&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Joomla&#039;s tagging system is used in all core content extensions and  is designed to be be easy to integrate into other extensions that use standard Joomla design patterns. &lt;br /&gt;
&lt;br /&gt;
Using tags in an extension is fairly straightforward. It requires these changes to a typical extension:&lt;br /&gt;
&lt;br /&gt;
* Register a &#039;Content type&#039; for the extension view(s)&lt;br /&gt;
* Add &#039;Observer methods&#039; to the extension table class(es)&lt;br /&gt;
* Add &#039;Tag fields&#039; to the extension edit forms&lt;br /&gt;
* Add &#039;Tag display&#039; code to the extension view files &lt;br /&gt;
* Optionally, add a batch tagging method to the extension administration&lt;br /&gt;
&lt;br /&gt;
== Register a content type for each view ==&lt;br /&gt;
&lt;br /&gt;
In Joomla! 3 content types e.g. articles, weblinks, contacts, . . . are registered in the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table with a separate record for each content view e.g. article, weblink, article category, weblink category, . . .&lt;br /&gt;
&lt;br /&gt;
Your extension installer needs to create a new record for each view.&lt;br /&gt;
&lt;br /&gt;
You can create records in the installer in three different ways:&lt;br /&gt;
* in the [[J2.5:Managing_Component_Updates_(Script.php)#postflight|postflight]] method&lt;br /&gt;
* by creating a &amp;lt;tt&amp;gt;JTableContenttype&amp;lt;/tt&amp;gt; instance and adding a row, or&lt;br /&gt;
* by using SQL to create a record directly&lt;br /&gt;
&lt;br /&gt;
The structure of the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table is fairly straightforward:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Column name !! Column type !! Purpose &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_id&amp;lt;/tt&amp;gt; || int(10) || Record key&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt;|| varchar(255)|| Type title e.g. &amp;lt;tt&amp;gt;Article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_alias&amp;lt;/tt&amp;gt; || varchar(255)|| Type alias e.g. &amp;lt;tt&amp;gt;com_content.article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt; || varchar(255)|| Information about the Table class&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt; || text || Not currently used&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt; || text || Maps the table column names to standard Joomla! names &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt; || varchar(255)|| Optional: name of a router method&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Construct the Content Type entry===&lt;br /&gt;
&lt;br /&gt;
For each table/option/view you will need to make an entry in &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table (see above).  &lt;br /&gt;
&lt;br /&gt;
====type_id====&lt;br /&gt;
This is the auto-incremented record key, nothing to be done here.&lt;br /&gt;
&lt;br /&gt;
====type_title====&lt;br /&gt;
&lt;br /&gt;
The title for your Content Type e.g. Article, Contact, Weblink,  &lt;br /&gt;
You can do this either using sql or postflight by creating an instance of JTableContenttype. Don&#039;t forget a category type if you use the Joomla categories API.&lt;br /&gt;
&lt;br /&gt;
====type_alias====&lt;br /&gt;
&lt;br /&gt;
A string identifying the component and view (that would be in the page request, typically matching the model name) e.g. &amp;lt;tt&amp;gt;com_contact.contact&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;com_weblinks.weblink&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====table====&lt;br /&gt;
&lt;br /&gt;
You need to identify which tables used by your component contain records to be tagged. For example the &#039;Contact&#039; view of the Contacts component uses &amp;lt;tt&amp;gt;#__contact_details&amp;lt;/tt&amp;gt; and similarly the &#039;Weblinks&#039; view of the Weblinks component uses &amp;lt;tt&amp;gt;#__weblinks &amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Each of these components uses a class to write to the table: &amp;lt;tt&amp;gt;ContactTableContact&amp;lt;/tt&amp;gt; for the Contacts component and &amp;lt;tt&amp;gt;WeblinksTableWeblink&amp;lt;/tt&amp;gt; for the Weblinks component. You will need the class prefixes e.g. &amp;lt;tt&amp;gt;ContactTable&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;WeblinksTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that Category types all use the &amp;lt;tt&amp;gt;#__categories&amp;lt;/tt&amp;gt; table.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;type_table&amp;lt;/tt&amp;gt; entry gives the complete table class information for the table class as a JSON object with two elements. The first element represents your &amp;quot;special&amp;quot; table and the second an optional common table (otherwise it will default the &amp;lt;tt&amp;gt;JTableCorecontent&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Here is the JSON entry for the Weblink content type:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__weblinks&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Weblink&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;WeblinksTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__ucm_content&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;ucm_id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Corecontent&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;JTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
The values to be included are:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Entry name !! Content &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;dbtable&amp;lt;/tt&amp;gt; || Table name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;key&amp;lt;/tt&amp;gt;|| Primary key name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type&amp;lt;/tt&amp;gt; || Content type&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;prefix&amp;lt;/tt&amp;gt; || Class prefix&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt; || An option array, as used in your component constructor and getInstance() methods, may be empty&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This information enables the tagging system (and other APIs) to access your table easily. &lt;br /&gt;
&lt;br /&gt;
If you are using Joomla categories make sure to create a category type so that they can be tagged. In Joomla 3.1 and 3.1.1 there is an error where the tag field will show even if there is not a type, but this is corrected in 3.1.4.  &lt;br /&gt;
  &lt;br /&gt;
=====Notes=====&lt;br /&gt;
&lt;br /&gt;
* The table name for the common table is &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt;; this is incorrect in 3.1 and 3.1.1 data but is not currently used. The data was updated in 3.1.4.&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt; field would potentially be used for display although this is not implemented currently except in the contenttype field.  Usually it should begin with an upper case letter if it is in English. See note about how to make this translatable. To make your type names translatable add &amp;lt;tt&amp;gt;COM_TAGS_CONTENT_TYPE_+type_title=&amp;quot;Type Title&amp;quot;&amp;lt;/tt&amp;gt; in both the ini and sys.ini files.&lt;br /&gt;
&lt;br /&gt;
====rules====&lt;br /&gt;
&lt;br /&gt;
Rules is currently not used. It will likely be removed in favor of an &amp;lt;tt&amp;gt;asset_id&amp;lt;/tt&amp;gt; for each type, but currently you can ignore this field which will be managed by &amp;lt;tt&amp;gt;JTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====field_mappings====&lt;br /&gt;
&lt;br /&gt;
This entry maps the names of specific fields in your table to a set of standard Joomla! names. This mapping is stored as a JSON array with the first &#039;common&#039; element mapping to the common fields and the second &#039;special&#039; element mapping the other fields from the table. &lt;br /&gt;
&lt;br /&gt;
Here is the field_mappings entry for the Banner Content Type (chosen because it is a richer example than Article or WebLink). Note that &#039;common&#039; mappings that have no equivalent are included as &#039;null&#039; (leaving them blank may cause sql issues); and the names and values in the &#039;special&#039; element all match as they are limited to this component.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;core_content_item_id&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;core_title&amp;quot;: &amp;quot;name&amp;quot;,&lt;br /&gt;
    &amp;quot;core_state&amp;quot;: &amp;quot;published&amp;quot;,&lt;br /&gt;
    &amp;quot;core_alias&amp;quot;: &amp;quot;alias&amp;quot;,&lt;br /&gt;
    &amp;quot;core_created_time&amp;quot;: &amp;quot;created&amp;quot;,&lt;br /&gt;
    &amp;quot;core_modified_time&amp;quot;: &amp;quot;modified&amp;quot;,&lt;br /&gt;
    &amp;quot;core_body&amp;quot;: &amp;quot;description&amp;quot;,&lt;br /&gt;
    &amp;quot;core_hits&amp;quot;: &amp;quot;null&amp;quot;,&lt;br /&gt;
    &amp;quot;core_publish_up&amp;quot;: &amp;quot;publish_up&amp;quot;,&lt;br /&gt;
    &amp;quot;core_publish_down&amp;quot;: &amp;quot;publish_down&amp;quot;,&lt;br /&gt;
    &amp;quot;core_access&amp;quot;: &amp;quot;access&amp;quot;,&lt;br /&gt;
    &amp;quot;core_params&amp;quot;: &amp;quot;params&amp;quot;,&lt;br /&gt;
    &amp;quot;core_featured&amp;quot;: &amp;quot;null&amp;quot;,&lt;br /&gt;
    &amp;quot;core_metadata&amp;quot;: &amp;quot;metadata&amp;quot;,&lt;br /&gt;
    &amp;quot;core_language&amp;quot;: &amp;quot;language&amp;quot;,&lt;br /&gt;
    &amp;quot;core_images&amp;quot;: &amp;quot;images&amp;quot;,&lt;br /&gt;
    &amp;quot;core_urls&amp;quot;: &amp;quot;link&amp;quot;,&lt;br /&gt;
    &amp;quot;core_version&amp;quot;: &amp;quot;version&amp;quot;,&lt;br /&gt;
    &amp;quot;core_ordering&amp;quot;: &amp;quot;ordering&amp;quot;,&lt;br /&gt;
    &amp;quot;core_metakey&amp;quot;: &amp;quot;metakey&amp;quot;,&lt;br /&gt;
    &amp;quot;core_metadesc&amp;quot;: &amp;quot;metadesc&amp;quot;,&lt;br /&gt;
    &amp;quot;core_catid&amp;quot;: &amp;quot;catid&amp;quot;,&lt;br /&gt;
    &amp;quot;core_xreference&amp;quot;: &amp;quot;null&amp;quot;,&lt;br /&gt;
    &amp;quot;asset_id&amp;quot;: &amp;quot;null&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;imptotal&amp;quot;: &amp;quot;imptotal&amp;quot;,&lt;br /&gt;
    &amp;quot;impmade&amp;quot;: &amp;quot;impmade&amp;quot;,&lt;br /&gt;
    &amp;quot;clicks&amp;quot;: &amp;quot;clicks&amp;quot;,&lt;br /&gt;
    &amp;quot;clickurl&amp;quot;: &amp;quot;clickurl&amp;quot;,&lt;br /&gt;
    &amp;quot;custombannercode&amp;quot;: &amp;quot;custombannercode&amp;quot;,&lt;br /&gt;
    &amp;quot;cid&amp;quot;: &amp;quot;cid&amp;quot;,&lt;br /&gt;
    &amp;quot;purchase_type&amp;quot;: &amp;quot;purchase_type&amp;quot;,&lt;br /&gt;
    &amp;quot;track_impressions&amp;quot;: &amp;quot;track_impressions&amp;quot;,&lt;br /&gt;
    &amp;quot;track_clicks&amp;quot;: &amp;quot;track_clicks&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
At a minimum for common fields you need to map: content_item_id, alias and title in order to successfully create urls in the tagged items list.  You also will probably want: access, status, and language. The special fields are optional.  &lt;br /&gt;
&lt;br /&gt;
=====Note=====&lt;br /&gt;
* The &amp;lt;tt&amp;gt;JHelperTags&amp;lt;/tt&amp;gt; and JUcm APIS at 3.1.1 supported arrays for this field, but as of 3.1.4 either arrays or objects are supported; the default is objects.&lt;br /&gt;
&lt;br /&gt;
====router====&lt;br /&gt;
&lt;br /&gt;
This is an optional entry to include the name of a static helper router method for this type found in its front end helpers folder e.g. &amp;lt;tt&amp;gt;WeblinksHelperRoute::getWeblinkRoute&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If you do not have a custom router then Tags falls back to the rules in &amp;lt;tt&amp;gt;JHelperRoute&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
If you only store data in &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt; you will eventually be able to leave the router field blank although this option is not currently implemented.&lt;br /&gt;
&lt;br /&gt;
== Modify your component&#039;s table class (or classes if you have multiple tables) ==&lt;br /&gt;
&lt;br /&gt;
Add the following to your JTable constructor:&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;_observers = new JObserverUpdater($this);&lt;br /&gt;
JObserverMapper::attachAllObservers($this);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The addition material formerly located here is no longer needed after 3.1.4&#039;&#039;&#039; Please read the document history for this page if you are required to support 3.1.0 or 3.1.1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;For joomla 3.2 I needed this line:&#039;&#039;&#039; ( Dont forget to replace weblinks by your component name )&lt;br /&gt;
&lt;br /&gt;
JObserverMapper::addObserverClassToClass(&#039;JTableObserverTags&#039;, &#039;WeblinksTableWeblink&#039;, array(&#039;typeAlias&#039; =&amp;gt; &#039;com_weblinks.weblink&#039;));&lt;br /&gt;
&lt;br /&gt;
== Add tags to the getItem() method of the model ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: this was only required in 3.1.0 and 3.1.1. It should not be used in 3.1.4 or later.&#039;&#039;&#039; Please read the history of this page if you need instructions for older versions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add a tag field to edit screens ==&lt;br /&gt;
&lt;br /&gt;
In any edit layouts where you want to allow tagging, you need to add the field to the xml and the appropriate layouts if necessary. The core layouts us a JLayout to manage this and the same layout by any extension.&lt;br /&gt;
&lt;br /&gt;
Update: Note that in &#039;&#039;&#039;3.1.1 only&#039;&#039;&#039; there is special handling of this in the core.  Tags in the edit screen &#039;&#039;&#039;MUST&#039;&#039;&#039; be part of a metadata &amp;lt;fields&amp;gt;&amp;lt;/fields&amp;gt; group.  The core provides two JLayouts to help you manage standard layouts, one (details) for the metadata and one for the sidebar that includes the tabs. In update an extension to 3.1.4 you may need to fix adjust your edit views.&lt;br /&gt;
&lt;br /&gt;
In 3.1.4 or later this special handling is not necessary.  Best practice is to use the standard JLayouts since this provides a consistent experience for users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field loads all the Javascript libraries required. You don&#039;t need to worry about that.&lt;br /&gt;
&lt;br /&gt;
The field supports two modes:  &lt;br /&gt;
* &#039;&#039;&#039;Nested tags mode.&#039;&#039;&#039; Hierarchical tag list. Doesn&#039;t support on the fly tag creation.&lt;br /&gt;
* &#039;&#039;&#039;AJAX mode.&#039;&#039;&#039;  Tags are searched while user types (3 min. chars required to launch the AJAX search). Custom tags are added by pressing ENTER or COMMA keys. Tags show the global route/path. Example: &amp;lt;code&amp;gt;grandpa/parent/tag&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field mode can be forced or use the com_tags setting &amp;lt;code&amp;gt;Tag field mode&amp;lt;/code&amp;gt; to determine its mode. To set/force the field mode we have to add &#039;&#039;&#039;mode=&amp;quot;ajax&amp;quot;&#039;&#039;&#039; or &#039;&#039;&#039;mode=&amp;quot;nested&amp;quot;&#039;&#039;&#039; to the tag field definition.  &lt;br /&gt;
&lt;br /&gt;
Example of forced AJAX mode:  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The field also includes an attribute to allow/deny the user to enter custom values. Currently this only works in AJAX mode. The attribute has to be added to the field definition like **custom=&amp;quot;allow&amp;quot;** or **custom=&amp;quot;deny&amp;quot;**  &lt;br /&gt;
&lt;br /&gt;
Example field definition with custom tags denied:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot; custom=&amp;quot;denied&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
if it is not already there. Usually multiple should be true unless you have a specific reason for it not to be.&lt;br /&gt;
In the core components in administrator, the field is in the group shown on the right, below the language field.&lt;br /&gt;
&lt;br /&gt;
Note: As of 3.1.2 if you wish to use the field to designate parent tags you must add parent=&amp;quot;parent&amp;quot; to the xml definition of the field.&lt;br /&gt;
&lt;br /&gt;
== Prepare the view ==&lt;br /&gt;
&lt;br /&gt;
Add an appropriate version of this to your view.html.php file before loading the layout:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$item-&amp;gt;tags = new JHelperTags;&lt;br /&gt;
$item-&amp;gt;tags-&amp;gt;getItemTags(&#039;com_newsfeeds.newsfeed.&#039; , $this-&amp;gt;item-&amp;gt;id); &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter should match a type in the types table. The second parameter is the primary key under which this record is stored in your table. This would be used in any display in any view where you want the tags associated with the item to display.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Set up the display ==&lt;br /&gt;
&lt;br /&gt;
In any layout where you want to display the tags associated with an item add:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Changing the object and property names as appropriate.&lt;br /&gt;
&lt;br /&gt;
You will most likely want to add the show_tags parameter to the item parameters, the menu item parameters and component configuration as appropriate for your use case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;?php if ($this-&amp;gt;params-&amp;gt;get(&#039;show_tags&#039;, 1) &amp;amp;&amp;amp; !empty($this-&amp;gt;item-&amp;gt;tags)) : ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
	&amp;lt;?php endif; ?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Batch processing ==&lt;br /&gt;
If you want to add the ability to do batch tagging to a backend list view do the following.&lt;br /&gt;
&lt;br /&gt;
Add tag to the default_batch layout&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
        &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&lt;br /&gt;
				&amp;lt;?php echo JHtml::_(&#039;batch.tag&#039;);?&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;/div&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add tag to the button class in the modal footer (Not totally necessary at this point, but good for potential future changes e.g. if unTag is added.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;button class=&amp;quot;btn&amp;quot; type=&amp;quot;button&amp;quot; onclick=&amp;quot;document.id(&#039;batch-category-id&#039;).value=&#039;&#039;;document.id(&#039;batch-access&#039;).value=&#039;&#039;;document.id(&#039;batch-language-id&#039;).value=&#039;&#039;;document.id(&#039;batch-user-id&#039;).value=&#039;&#039;;document.id(&#039;batch-tag-id)&#039;).value=&#039;&#039;&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a batch method to your model if you are not extending JModelAdmin or overriding the batch method. ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Batch tag a list of item.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @param   integer  $value     The value of the new tag.&lt;br /&gt;
	 * @param   array    $pks       An array of row IDs.&lt;br /&gt;
	 * @param   array    $contexts  An array of item contexts.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @return  void.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @since   3.1&lt;br /&gt;
	 */&lt;br /&gt;
	protected function batchTag($value, $pks, $contexts)&lt;br /&gt;
	{&lt;br /&gt;
		$tagsHelper = new JHelperTags();&lt;br /&gt;
		$tagsHelper-&amp;gt;tagItems($value, $pks, $contexts);&lt;br /&gt;
&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And modify your batch method by adding&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		if (!empty($commands[&#039;tag&#039;]))&lt;br /&gt;
		{&lt;br /&gt;
			if (!$this-&amp;gt;batchTag($commands[&#039;tag&#039;], $pks, $contexts))&lt;br /&gt;
			{&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			$done = true;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pay attention to any JSON encoded strings that you need special handling for in batch processing -- remember that you are saving a copy of the core fields and you need save to work as expected.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s it, now create some tags, tag some items and you are set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]][[Category:Content Tags]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117742</id>
		<title>J3.x:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117742"/>
		<updated>2014-04-26T15:46:03Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* field_mappings */  Updated and edited&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{copyedit}}&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Joomla&#039;s tagging system is used in all core content extensions and  is designed to be be easy to integrate into other extensions that use standard Joomla design patterns. &lt;br /&gt;
&lt;br /&gt;
Using tags in an extension is fairly straightforward. It requires these changes to a typical extension:&lt;br /&gt;
&lt;br /&gt;
* Register a &#039;Content type&#039; for the extension view(s)&lt;br /&gt;
* Add &#039;Observer methods&#039; to the extension table class(es)&lt;br /&gt;
* Add &#039;Tag fields&#039; to the extension edit forms&lt;br /&gt;
* Add &#039;Tag display&#039; code to the extension view files &lt;br /&gt;
* Optionally, add a batch tagging method to the extension administration&lt;br /&gt;
&lt;br /&gt;
== Register a content type for each view ==&lt;br /&gt;
&lt;br /&gt;
In Joomla! 3 content types e.g. articles, weblinks, contacts, . . . are registered in the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table with a separate record for each content view e.g. article, weblink, article category, weblink category, . . .&lt;br /&gt;
&lt;br /&gt;
Your extension installer needs to create a new record for each view.&lt;br /&gt;
&lt;br /&gt;
You can create records in the installer in three different ways:&lt;br /&gt;
* in the [[J2.5:Managing_Component_Updates_(Script.php)#postflight|postflight]] method&lt;br /&gt;
* by creating a &amp;lt;tt&amp;gt;JTableContenttype&amp;lt;/tt&amp;gt; instance and adding a row, or&lt;br /&gt;
* by using SQL to create a record directly&lt;br /&gt;
&lt;br /&gt;
The structure of the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table is fairly straightforward:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Column name !! Column type !! Purpose &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_id&amp;lt;/tt&amp;gt; || int(10) || Record key&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt;|| varchar(255)|| Type title e.g. &amp;lt;tt&amp;gt;Article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_alias&amp;lt;/tt&amp;gt; || varchar(255)|| Type alias e.g. &amp;lt;tt&amp;gt;com_content.article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt; || varchar(255)|| Information about the Table class&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt; || text || Not currently used&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt; || text || Maps the table column names to standard Joomla! names &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt; || varchar(255)|| Optional: name of a router method&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Construct the Content Type entry===&lt;br /&gt;
&lt;br /&gt;
For each table/option/view you will need to make an entry in &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table (see above).  &lt;br /&gt;
&lt;br /&gt;
====type_id====&lt;br /&gt;
This is the auto-incremented record key, nothing to be done here.&lt;br /&gt;
&lt;br /&gt;
====type_title====&lt;br /&gt;
&lt;br /&gt;
The title for your Content Type e.g. Article, Contact, Weblink,  &lt;br /&gt;
You can do this either using sql or postflight by creating an instance of JTableContenttype. Don&#039;t forget a category type if you use the Joomla categories API.&lt;br /&gt;
&lt;br /&gt;
====type_alias====&lt;br /&gt;
&lt;br /&gt;
A string identifying the component and view (that would be in the page request, typically matching the model name) e.g. &amp;lt;tt&amp;gt;com_contact.contact&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;com_weblinks.weblink&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====table====&lt;br /&gt;
&lt;br /&gt;
You need to identify which tables used by your component contain records to be tagged. For example the &#039;Contact&#039; view of the Contacts component uses &amp;lt;tt&amp;gt;#__contact_details&amp;lt;/tt&amp;gt; and similarly the &#039;Weblinks&#039; view of the Weblinks component uses &amp;lt;tt&amp;gt;#__weblinks &amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Each of these components uses a class to write to the table: &amp;lt;tt&amp;gt;ContactTableContact&amp;lt;/tt&amp;gt; for the Contacts component and &amp;lt;tt&amp;gt;WeblinksTableWeblink&amp;lt;/tt&amp;gt; for the Weblinks component. You will need the class prefixes e.g. &amp;lt;tt&amp;gt;ContactTable&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;WeblinksTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that Category types all use the &amp;lt;tt&amp;gt;#__categories&amp;lt;/tt&amp;gt; table.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;type_table&amp;lt;/tt&amp;gt; entry gives the complete table class information for the table class as a JSON object with two elements. The first element represents your &amp;quot;special&amp;quot; table and the second an optional common table (otherwise it will default the &amp;lt;tt&amp;gt;JTableCorecontent&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Here is the JSON entry for the Weblink content type:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__weblinks&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Weblink&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;WeblinksTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__ucm_content&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;ucm_id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Corecontent&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;JTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
The values to be included are:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Entry name !! Content &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;dbtable&amp;lt;/tt&amp;gt; || Table name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;key&amp;lt;/tt&amp;gt;|| Primary key name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type&amp;lt;/tt&amp;gt; || Content type&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;prefix&amp;lt;/tt&amp;gt; || Class prefix&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt; || An option array, as used in your component constructor and getInstance() methods, may be empty&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This information enables the tagging system (and other APIs) to access your table easily. &lt;br /&gt;
&lt;br /&gt;
If you are using Joomla categories make sure to create a category type so that they can be tagged. In Joomla 3.1 and 3.1.1 there is an error where the tag field will show even if there is not a type, but this is corrected in 3.1.4.  &lt;br /&gt;
  &lt;br /&gt;
=====Notes=====&lt;br /&gt;
&lt;br /&gt;
* The table name for the common table is &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt;; this is incorrect in 3.1 and 3.1.1 data but is not currently used. The data was updated in 3.1.4.&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt; field would potentially be used for display although this is not implemented currently except in the contenttype field.  Usually it should begin with an upper case letter if it is in English. See note about how to make this translatable. To make your type names translatable add &amp;lt;tt&amp;gt;COM_TAGS_CONTENT_TYPE_+type_title=&amp;quot;Type Title&amp;quot;&amp;lt;/tt&amp;gt; in both the ini and sys.ini files.&lt;br /&gt;
&lt;br /&gt;
====rules====&lt;br /&gt;
&lt;br /&gt;
Rules is currently not used. It will likely be removed in favor of an &amp;lt;tt&amp;gt;asset_id&amp;lt;/tt&amp;gt; for each type, but currently you can ignore this field which will be managed by &amp;lt;tt&amp;gt;JTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====field_mappings====&lt;br /&gt;
&lt;br /&gt;
This entry maps the names of specific fields in your table to a set of standard Joomla! names. This mapping is stored as a JSON array with the first &#039;common&#039; element mapping to the common fields and the second &#039;special&#039; element mapping the other fields from the table. &lt;br /&gt;
&lt;br /&gt;
Here is the field_mappings entry for the Banner Content Type (chosen because it is a richer example than Article or WebLink). Note that &#039;common&#039; mappings that have no equivalent are included as &#039;null&#039; (leaving them blank may cause sql issues); and the names and values in the &#039;special&#039; element all match as they are limited to this component.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;core_content_item_id&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;core_title&amp;quot;: &amp;quot;name&amp;quot;,&lt;br /&gt;
    &amp;quot;core_state&amp;quot;: &amp;quot;published&amp;quot;,&lt;br /&gt;
    &amp;quot;core_alias&amp;quot;: &amp;quot;alias&amp;quot;,&lt;br /&gt;
    &amp;quot;core_created_time&amp;quot;: &amp;quot;created&amp;quot;,&lt;br /&gt;
    &amp;quot;core_modified_time&amp;quot;: &amp;quot;modified&amp;quot;,&lt;br /&gt;
    &amp;quot;core_body&amp;quot;: &amp;quot;description&amp;quot;,&lt;br /&gt;
    &amp;quot;core_hits&amp;quot;: &amp;quot;null&amp;quot;,&lt;br /&gt;
    &amp;quot;core_publish_up&amp;quot;: &amp;quot;publish_up&amp;quot;,&lt;br /&gt;
    &amp;quot;core_publish_down&amp;quot;: &amp;quot;publish_down&amp;quot;,&lt;br /&gt;
    &amp;quot;core_access&amp;quot;: &amp;quot;access&amp;quot;,&lt;br /&gt;
    &amp;quot;core_params&amp;quot;: &amp;quot;params&amp;quot;,&lt;br /&gt;
    &amp;quot;core_featured&amp;quot;: &amp;quot;null&amp;quot;,&lt;br /&gt;
    &amp;quot;core_metadata&amp;quot;: &amp;quot;metadata&amp;quot;,&lt;br /&gt;
    &amp;quot;core_language&amp;quot;: &amp;quot;language&amp;quot;,&lt;br /&gt;
    &amp;quot;core_images&amp;quot;: &amp;quot;images&amp;quot;,&lt;br /&gt;
    &amp;quot;core_urls&amp;quot;: &amp;quot;link&amp;quot;,&lt;br /&gt;
    &amp;quot;core_version&amp;quot;: &amp;quot;version&amp;quot;,&lt;br /&gt;
    &amp;quot;core_ordering&amp;quot;: &amp;quot;ordering&amp;quot;,&lt;br /&gt;
    &amp;quot;core_metakey&amp;quot;: &amp;quot;metakey&amp;quot;,&lt;br /&gt;
    &amp;quot;core_metadesc&amp;quot;: &amp;quot;metadesc&amp;quot;,&lt;br /&gt;
    &amp;quot;core_catid&amp;quot;: &amp;quot;catid&amp;quot;,&lt;br /&gt;
    &amp;quot;core_xreference&amp;quot;: &amp;quot;null&amp;quot;,&lt;br /&gt;
    &amp;quot;asset_id&amp;quot;: &amp;quot;null&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;imptotal&amp;quot;: &amp;quot;imptotal&amp;quot;,&lt;br /&gt;
    &amp;quot;impmade&amp;quot;: &amp;quot;impmade&amp;quot;,&lt;br /&gt;
    &amp;quot;clicks&amp;quot;: &amp;quot;clicks&amp;quot;,&lt;br /&gt;
    &amp;quot;clickurl&amp;quot;: &amp;quot;clickurl&amp;quot;,&lt;br /&gt;
    &amp;quot;custombannercode&amp;quot;: &amp;quot;custombannercode&amp;quot;,&lt;br /&gt;
    &amp;quot;cid&amp;quot;: &amp;quot;cid&amp;quot;,&lt;br /&gt;
    &amp;quot;purchase_type&amp;quot;: &amp;quot;purchase_type&amp;quot;,&lt;br /&gt;
    &amp;quot;track_impressions&amp;quot;: &amp;quot;track_impressions&amp;quot;,&lt;br /&gt;
    &amp;quot;track_clicks&amp;quot;: &amp;quot;track_clicks&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
At a minimum for common fields you need to map: content_item_id, alias and title in order to successfully create urls in the tagged items list.  You also will probably want: access, status, and language. The special fields are optional.  &lt;br /&gt;
&lt;br /&gt;
=====Note=====&lt;br /&gt;
* The &amp;lt;tt&amp;gt;JHelperTags&amp;lt;/tt&amp;gt; and JUcm APIS at 3.1.1 supported arrays for this field, but as of 3.1.4 either arrays or objects are supported; the default is objects.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Router&#039;&#039;&#039; is an optional entry to include the name of a static helper router method for this type found in its front end helpers folder. &lt;br /&gt;
&lt;br /&gt;
If you only store data in &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt; you will eventually be able to leave the router field blank although this option is not currently implemented. If you do not have a custom router tags falls back to the rules found in &amp;lt;tt&amp;gt;JHelperRoute&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Modify your component&#039;s table class (or classes if you have multiple tables) ==&lt;br /&gt;
&lt;br /&gt;
Add the following to your JTable constructor:&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;_observers = new JObserverUpdater($this);&lt;br /&gt;
JObserverMapper::attachAllObservers($this);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The addition material formerly located here is no longer needed after 3.1.4&#039;&#039;&#039; Please read the document history for this page if you are required to support 3.1.0 or 3.1.1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;For joomla 3.2 I needed this line:&#039;&#039;&#039; ( Dont forget to replace weblinks by your component name )&lt;br /&gt;
&lt;br /&gt;
JObserverMapper::addObserverClassToClass(&#039;JTableObserverTags&#039;, &#039;WeblinksTableWeblink&#039;, array(&#039;typeAlias&#039; =&amp;gt; &#039;com_weblinks.weblink&#039;));&lt;br /&gt;
&lt;br /&gt;
== Add tags to the getItem() method of the model ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: this was only required in 3.1.0 and 3.1.1. It should not be used in 3.1.4 or later.&#039;&#039;&#039; Please read the history of this page if you need instructions for older versions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add a tag field to edit screens ==&lt;br /&gt;
&lt;br /&gt;
In any edit layouts where you want to allow tagging, you need to add the field to the xml and the appropriate layouts if necessary. The core layouts us a JLayout to manage this and the same layout by any extension.&lt;br /&gt;
&lt;br /&gt;
Update: Note that in &#039;&#039;&#039;3.1.1 only&#039;&#039;&#039; there is special handling of this in the core.  Tags in the edit screen &#039;&#039;&#039;MUST&#039;&#039;&#039; be part of a metadata &amp;lt;fields&amp;gt;&amp;lt;/fields&amp;gt; group.  The core provides two JLayouts to help you manage standard layouts, one (details) for the metadata and one for the sidebar that includes the tabs. In update an extension to 3.1.4 you may need to fix adjust your edit views.&lt;br /&gt;
&lt;br /&gt;
In 3.1.4 or later this special handling is not necessary.  Best practice is to use the standard JLayouts since this provides a consistent experience for users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field loads all the Javascript libraries required. You don&#039;t need to worry about that.&lt;br /&gt;
&lt;br /&gt;
The field supports two modes:  &lt;br /&gt;
* &#039;&#039;&#039;Nested tags mode.&#039;&#039;&#039; Hierarchical tag list. Doesn&#039;t support on the fly tag creation.&lt;br /&gt;
* &#039;&#039;&#039;AJAX mode.&#039;&#039;&#039;  Tags are searched while user types (3 min. chars required to launch the AJAX search). Custom tags are added by pressing ENTER or COMMA keys. Tags show the global route/path. Example: &amp;lt;code&amp;gt;grandpa/parent/tag&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field mode can be forced or use the com_tags setting &amp;lt;code&amp;gt;Tag field mode&amp;lt;/code&amp;gt; to determine its mode. To set/force the field mode we have to add &#039;&#039;&#039;mode=&amp;quot;ajax&amp;quot;&#039;&#039;&#039; or &#039;&#039;&#039;mode=&amp;quot;nested&amp;quot;&#039;&#039;&#039; to the tag field definition.  &lt;br /&gt;
&lt;br /&gt;
Example of forced AJAX mode:  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The field also includes an attribute to allow/deny the user to enter custom values. Currently this only works in AJAX mode. The attribute has to be added to the field definition like **custom=&amp;quot;allow&amp;quot;** or **custom=&amp;quot;deny&amp;quot;**  &lt;br /&gt;
&lt;br /&gt;
Example field definition with custom tags denied:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot; custom=&amp;quot;denied&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
if it is not already there. Usually multiple should be true unless you have a specific reason for it not to be.&lt;br /&gt;
In the core components in administrator, the field is in the group shown on the right, below the language field.&lt;br /&gt;
&lt;br /&gt;
Note: As of 3.1.2 if you wish to use the field to designate parent tags you must add parent=&amp;quot;parent&amp;quot; to the xml definition of the field.&lt;br /&gt;
&lt;br /&gt;
== Prepare the view ==&lt;br /&gt;
&lt;br /&gt;
Add an appropriate version of this to your view.html.php file before loading the layout:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$item-&amp;gt;tags = new JHelperTags;&lt;br /&gt;
$item-&amp;gt;tags-&amp;gt;getItemTags(&#039;com_newsfeeds.newsfeed.&#039; , $this-&amp;gt;item-&amp;gt;id); &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter should match a type in the types table. The second parameter is the primary key under which this record is stored in your table. This would be used in any display in any view where you want the tags associated with the item to display.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Set up the display ==&lt;br /&gt;
&lt;br /&gt;
In any layout where you want to display the tags associated with an item add:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Changing the object and property names as appropriate.&lt;br /&gt;
&lt;br /&gt;
You will most likely want to add the show_tags parameter to the item parameters, the menu item parameters and component configuration as appropriate for your use case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;?php if ($this-&amp;gt;params-&amp;gt;get(&#039;show_tags&#039;, 1) &amp;amp;&amp;amp; !empty($this-&amp;gt;item-&amp;gt;tags)) : ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
	&amp;lt;?php endif; ?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Batch processing ==&lt;br /&gt;
If you want to add the ability to do batch tagging to a backend list view do the following.&lt;br /&gt;
&lt;br /&gt;
Add tag to the default_batch layout&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
        &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&lt;br /&gt;
				&amp;lt;?php echo JHtml::_(&#039;batch.tag&#039;);?&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;/div&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add tag to the button class in the modal footer (Not totally necessary at this point, but good for potential future changes e.g. if unTag is added.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;button class=&amp;quot;btn&amp;quot; type=&amp;quot;button&amp;quot; onclick=&amp;quot;document.id(&#039;batch-category-id&#039;).value=&#039;&#039;;document.id(&#039;batch-access&#039;).value=&#039;&#039;;document.id(&#039;batch-language-id&#039;).value=&#039;&#039;;document.id(&#039;batch-user-id&#039;).value=&#039;&#039;;document.id(&#039;batch-tag-id)&#039;).value=&#039;&#039;&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a batch method to your model if you are not extending JModelAdmin or overriding the batch method. ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Batch tag a list of item.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @param   integer  $value     The value of the new tag.&lt;br /&gt;
	 * @param   array    $pks       An array of row IDs.&lt;br /&gt;
	 * @param   array    $contexts  An array of item contexts.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @return  void.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @since   3.1&lt;br /&gt;
	 */&lt;br /&gt;
	protected function batchTag($value, $pks, $contexts)&lt;br /&gt;
	{&lt;br /&gt;
		$tagsHelper = new JHelperTags();&lt;br /&gt;
		$tagsHelper-&amp;gt;tagItems($value, $pks, $contexts);&lt;br /&gt;
&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And modify your batch method by adding&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		if (!empty($commands[&#039;tag&#039;]))&lt;br /&gt;
		{&lt;br /&gt;
			if (!$this-&amp;gt;batchTag($commands[&#039;tag&#039;], $pks, $contexts))&lt;br /&gt;
			{&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			$done = true;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pay attention to any JSON encoded strings that you need special handling for in batch processing -- remember that you are saving a copy of the core fields and you need save to work as expected.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s it, now create some tags, tag some items and you are set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]][[Category:Content Tags]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117741</id>
		<title>J3.x:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117741"/>
		<updated>2014-04-26T15:35:30Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* rules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{copyedit}}&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Joomla&#039;s tagging system is used in all core content extensions and  is designed to be be easy to integrate into other extensions that use standard Joomla design patterns. &lt;br /&gt;
&lt;br /&gt;
Using tags in an extension is fairly straightforward. It requires these changes to a typical extension:&lt;br /&gt;
&lt;br /&gt;
* Register a &#039;Content type&#039; for the extension view(s)&lt;br /&gt;
* Add &#039;Observer methods&#039; to the extension table class(es)&lt;br /&gt;
* Add &#039;Tag fields&#039; to the extension edit forms&lt;br /&gt;
* Add &#039;Tag display&#039; code to the extension view files &lt;br /&gt;
* Optionally, add a batch tagging method to the extension administration&lt;br /&gt;
&lt;br /&gt;
== Register a content type for each view ==&lt;br /&gt;
&lt;br /&gt;
In Joomla! 3 content types e.g. articles, weblinks, contacts, . . . are registered in the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table with a separate record for each content view e.g. article, weblink, article category, weblink category, . . .&lt;br /&gt;
&lt;br /&gt;
Your extension installer needs to create a new record for each view.&lt;br /&gt;
&lt;br /&gt;
You can create records in the installer in three different ways:&lt;br /&gt;
* in the [[J2.5:Managing_Component_Updates_(Script.php)#postflight|postflight]] method&lt;br /&gt;
* by creating a &amp;lt;tt&amp;gt;JTableContenttype&amp;lt;/tt&amp;gt; instance and adding a row, or&lt;br /&gt;
* by using SQL to create a record directly&lt;br /&gt;
&lt;br /&gt;
The structure of the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table is fairly straightforward:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Column name !! Column type !! Purpose &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_id&amp;lt;/tt&amp;gt; || int(10) || Record key&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt;|| varchar(255)|| Type title e.g. &amp;lt;tt&amp;gt;Article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_alias&amp;lt;/tt&amp;gt; || varchar(255)|| Type alias e.g. &amp;lt;tt&amp;gt;com_content.article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt; || varchar(255)|| Information about the Table class&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt; || text || Not currently used&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt; || text || Maps the table column names to standard Joomla! names &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt; || varchar(255)|| Optional: name of a router method&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Construct the Content Type entry===&lt;br /&gt;
&lt;br /&gt;
For each table/option/view you will need to make an entry in &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table (see above).  &lt;br /&gt;
&lt;br /&gt;
====type_id====&lt;br /&gt;
This is the auto-incremented record key, nothing to be done here.&lt;br /&gt;
&lt;br /&gt;
====type_title====&lt;br /&gt;
&lt;br /&gt;
The title for your Content Type e.g. Article, Contact, Weblink,  &lt;br /&gt;
You can do this either using sql or postflight by creating an instance of JTableContenttype. Don&#039;t forget a category type if you use the Joomla categories API.&lt;br /&gt;
&lt;br /&gt;
====type_alias====&lt;br /&gt;
&lt;br /&gt;
A string identifying the component and view (that would be in the page request, typically matching the model name) e.g. &amp;lt;tt&amp;gt;com_contact.contact&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;com_weblinks.weblink&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====table====&lt;br /&gt;
&lt;br /&gt;
You need to identify which tables used by your component contain records to be tagged. For example the &#039;Contact&#039; view of the Contacts component uses &amp;lt;tt&amp;gt;#__contact_details&amp;lt;/tt&amp;gt; and similarly the &#039;Weblinks&#039; view of the Weblinks component uses &amp;lt;tt&amp;gt;#__weblinks &amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Each of these components uses a class to write to the table: &amp;lt;tt&amp;gt;ContactTableContact&amp;lt;/tt&amp;gt; for the Contacts component and &amp;lt;tt&amp;gt;WeblinksTableWeblink&amp;lt;/tt&amp;gt; for the Weblinks component. You will need the class prefixes e.g. &amp;lt;tt&amp;gt;ContactTable&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;WeblinksTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that Category types all use the &amp;lt;tt&amp;gt;#__categories&amp;lt;/tt&amp;gt; table.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;type_table&amp;lt;/tt&amp;gt; entry gives the complete table class information for the table class as a JSON object with two elements. The first element represents your &amp;quot;special&amp;quot; table and the second an optional common table (otherwise it will default the &amp;lt;tt&amp;gt;JTableCorecontent&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Here is the JSON entry for the Weblink content type:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__weblinks&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Weblink&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;WeblinksTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__ucm_content&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;ucm_id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Corecontent&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;JTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
The values to be included are:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Entry name !! Content &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;dbtable&amp;lt;/tt&amp;gt; || Table name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;key&amp;lt;/tt&amp;gt;|| Primary key name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type&amp;lt;/tt&amp;gt; || Content type&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;prefix&amp;lt;/tt&amp;gt; || Class prefix&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt; || An option array, as used in your component constructor and getInstance() methods, may be empty&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This information enables the tagging system (and other APIs) to access your table easily. &lt;br /&gt;
&lt;br /&gt;
If you are using Joomla categories make sure to create a category type so that they can be tagged. In Joomla 3.1 and 3.1.1 there is an error where the tag field will show even if there is not a type, but this is corrected in 3.1.4.  &lt;br /&gt;
  &lt;br /&gt;
=====Notes=====&lt;br /&gt;
&lt;br /&gt;
* The table name for the common table is &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt;; this is incorrect in 3.1 and 3.1.1 data but is not currently used. The data was updated in 3.1.4.&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt; field would potentially be used for display although this is not implemented currently except in the contenttype field.  Usually it should begin with an upper case letter if it is in English. See note about how to make this translatable. To make your type names translatable add &amp;lt;tt&amp;gt;COM_TAGS_CONTENT_TYPE_+type_title=&amp;quot;Type Title&amp;quot;&amp;lt;/tt&amp;gt; in both the ini and sys.ini files.&lt;br /&gt;
&lt;br /&gt;
====rules====&lt;br /&gt;
&lt;br /&gt;
Rules is currently not used. It will likely be removed in favor of an &amp;lt;tt&amp;gt;asset_id&amp;lt;/tt&amp;gt; for each type, but currently you can ignore this field which will be managed by &amp;lt;tt&amp;gt;JTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
This entry maps the names of specific fields in your table to a set of standard names. This mapping is stored as a JSON array with the first element mapping to the common fields and the second one mapping the other fields from the table. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important note&#039;&#039;&#039;  The JHelperTags and JUcm APIS at 3.1.1 supported arrays for this field, but as of  3.1.4 either arrays or objects with a default of objects are supported.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&#039;{&amp;quot;common&amp;quot;:[{&amp;quot;core_content_item_id&amp;quot;:&amp;quot;id&amp;quot;,&amp;quot;core_title&amp;quot;:&amp;quot;title&amp;quot;,&amp;quot;core_state&amp;quot;:&amp;quot;published&amp;quot;,&amp;quot;core_alias&amp;quot;:&amp;quot;alias&amp;quot;,&amp;quot;core_created_time&amp;quot;:&amp;quot;created_time&amp;quot;,&amp;quot;core_modified_time&amp;quot;:&amp;quot;modified_time&amp;quot;,&amp;quot;core_body&amp;quot;:&amp;quot;description&amp;quot;, &amp;quot;core_hits&amp;quot;:&amp;quot;hits&amp;quot;,&amp;quot;core_publish_up&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_publish_down&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_access&amp;quot;:&amp;quot;access&amp;quot;, &amp;quot;core_params&amp;quot;:&amp;quot;params&amp;quot;, &amp;quot;core_featured&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metadata&amp;quot;:&amp;quot;metadata&amp;quot;, &amp;quot;core_language&amp;quot;:&amp;quot;language&amp;quot;, &amp;quot;core_images&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_urls&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_version&amp;quot;:&amp;quot;version&amp;quot;, &amp;quot;core_ordering&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metakey&amp;quot;:&amp;quot;metakey&amp;quot;, &amp;quot;core_metadesc&amp;quot;:&amp;quot;metadesc&amp;quot;, &amp;quot;core_catid&amp;quot;:&amp;quot;parent_id&amp;quot;, &amp;quot;core_xreference&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;asset_id&amp;quot;:&amp;quot;asset_id&amp;quot;}], &amp;quot;special&amp;quot;: [{&amp;quot;parent_id&amp;quot;:&amp;quot;parent_id&amp;quot;,&amp;quot;lft&amp;quot;:&amp;quot;lft&amp;quot;,&amp;quot;rgt&amp;quot;:&amp;quot;rgt&amp;quot;,&amp;quot;level&amp;quot;:&amp;quot;level&amp;quot;,&amp;quot;path&amp;quot;:&amp;quot;path&amp;quot;,&amp;quot;extension&amp;quot;:&amp;quot;extension&amp;quot;,&amp;quot;note&amp;quot;:&amp;quot;note&amp;quot;}]}&#039;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is the field mapping for the Article type. Note that article uses the name attribs for the core field called params.  If your table does not contain a field, put “null” instead.  Leaving it blank may cause SQL issues. The special fields are optional.  At a minimum for common fields you need to map: content_item_id, alias and title in order to successfully create urls in the tagged items list.  You also will probably want: access, status, and language.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Router&#039;&#039;&#039; is an optional entry to include the name of a static helper router method for this type found in its front end helpers folder. &lt;br /&gt;
&lt;br /&gt;
If you only store data in &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt; you will eventually be able to leave the router field blank although this option is not currently implemented. If you do not have a custom router tags falls back to the rules found in &amp;lt;tt&amp;gt;JHelperRoute&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Modify your component&#039;s table class (or classes if you have multiple tables) ==&lt;br /&gt;
&lt;br /&gt;
Add the following to your JTable constructor:&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;_observers = new JObserverUpdater($this);&lt;br /&gt;
JObserverMapper::attachAllObservers($this);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The addition material formerly located here is no longer needed after 3.1.4&#039;&#039;&#039; Please read the document history for this page if you are required to support 3.1.0 or 3.1.1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;For joomla 3.2 I needed this line:&#039;&#039;&#039; ( Dont forget to replace weblinks by your component name )&lt;br /&gt;
&lt;br /&gt;
JObserverMapper::addObserverClassToClass(&#039;JTableObserverTags&#039;, &#039;WeblinksTableWeblink&#039;, array(&#039;typeAlias&#039; =&amp;gt; &#039;com_weblinks.weblink&#039;));&lt;br /&gt;
&lt;br /&gt;
== Add tags to the getItem() method of the model ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: this was only required in 3.1.0 and 3.1.1. It should not be used in 3.1.4 or later.&#039;&#039;&#039; Please read the history of this page if you need instructions for older versions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add a tag field to edit screens ==&lt;br /&gt;
&lt;br /&gt;
In any edit layouts where you want to allow tagging, you need to add the field to the xml and the appropriate layouts if necessary. The core layouts us a JLayout to manage this and the same layout by any extension.&lt;br /&gt;
&lt;br /&gt;
Update: Note that in &#039;&#039;&#039;3.1.1 only&#039;&#039;&#039; there is special handling of this in the core.  Tags in the edit screen &#039;&#039;&#039;MUST&#039;&#039;&#039; be part of a metadata &amp;lt;fields&amp;gt;&amp;lt;/fields&amp;gt; group.  The core provides two JLayouts to help you manage standard layouts, one (details) for the metadata and one for the sidebar that includes the tabs. In update an extension to 3.1.4 you may need to fix adjust your edit views.&lt;br /&gt;
&lt;br /&gt;
In 3.1.4 or later this special handling is not necessary.  Best practice is to use the standard JLayouts since this provides a consistent experience for users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field loads all the Javascript libraries required. You don&#039;t need to worry about that.&lt;br /&gt;
&lt;br /&gt;
The field supports two modes:  &lt;br /&gt;
* &#039;&#039;&#039;Nested tags mode.&#039;&#039;&#039; Hierarchical tag list. Doesn&#039;t support on the fly tag creation.&lt;br /&gt;
* &#039;&#039;&#039;AJAX mode.&#039;&#039;&#039;  Tags are searched while user types (3 min. chars required to launch the AJAX search). Custom tags are added by pressing ENTER or COMMA keys. Tags show the global route/path. Example: &amp;lt;code&amp;gt;grandpa/parent/tag&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field mode can be forced or use the com_tags setting &amp;lt;code&amp;gt;Tag field mode&amp;lt;/code&amp;gt; to determine its mode. To set/force the field mode we have to add &#039;&#039;&#039;mode=&amp;quot;ajax&amp;quot;&#039;&#039;&#039; or &#039;&#039;&#039;mode=&amp;quot;nested&amp;quot;&#039;&#039;&#039; to the tag field definition.  &lt;br /&gt;
&lt;br /&gt;
Example of forced AJAX mode:  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The field also includes an attribute to allow/deny the user to enter custom values. Currently this only works in AJAX mode. The attribute has to be added to the field definition like **custom=&amp;quot;allow&amp;quot;** or **custom=&amp;quot;deny&amp;quot;**  &lt;br /&gt;
&lt;br /&gt;
Example field definition with custom tags denied:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot; custom=&amp;quot;denied&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
if it is not already there. Usually multiple should be true unless you have a specific reason for it not to be.&lt;br /&gt;
In the core components in administrator, the field is in the group shown on the right, below the language field.&lt;br /&gt;
&lt;br /&gt;
Note: As of 3.1.2 if you wish to use the field to designate parent tags you must add parent=&amp;quot;parent&amp;quot; to the xml definition of the field.&lt;br /&gt;
&lt;br /&gt;
== Prepare the view ==&lt;br /&gt;
&lt;br /&gt;
Add an appropriate version of this to your view.html.php file before loading the layout:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$item-&amp;gt;tags = new JHelperTags;&lt;br /&gt;
$item-&amp;gt;tags-&amp;gt;getItemTags(&#039;com_newsfeeds.newsfeed.&#039; , $this-&amp;gt;item-&amp;gt;id); &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter should match a type in the types table. The second parameter is the primary key under which this record is stored in your table. This would be used in any display in any view where you want the tags associated with the item to display.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Set up the display ==&lt;br /&gt;
&lt;br /&gt;
In any layout where you want to display the tags associated with an item add:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Changing the object and property names as appropriate.&lt;br /&gt;
&lt;br /&gt;
You will most likely want to add the show_tags parameter to the item parameters, the menu item parameters and component configuration as appropriate for your use case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;?php if ($this-&amp;gt;params-&amp;gt;get(&#039;show_tags&#039;, 1) &amp;amp;&amp;amp; !empty($this-&amp;gt;item-&amp;gt;tags)) : ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
	&amp;lt;?php endif; ?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Batch processing ==&lt;br /&gt;
If you want to add the ability to do batch tagging to a backend list view do the following.&lt;br /&gt;
&lt;br /&gt;
Add tag to the default_batch layout&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
        &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&lt;br /&gt;
				&amp;lt;?php echo JHtml::_(&#039;batch.tag&#039;);?&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;/div&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add tag to the button class in the modal footer (Not totally necessary at this point, but good for potential future changes e.g. if unTag is added.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;button class=&amp;quot;btn&amp;quot; type=&amp;quot;button&amp;quot; onclick=&amp;quot;document.id(&#039;batch-category-id&#039;).value=&#039;&#039;;document.id(&#039;batch-access&#039;).value=&#039;&#039;;document.id(&#039;batch-language-id&#039;).value=&#039;&#039;;document.id(&#039;batch-user-id&#039;).value=&#039;&#039;;document.id(&#039;batch-tag-id)&#039;).value=&#039;&#039;&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a batch method to your model if you are not extending JModelAdmin or overriding the batch method. ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Batch tag a list of item.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @param   integer  $value     The value of the new tag.&lt;br /&gt;
	 * @param   array    $pks       An array of row IDs.&lt;br /&gt;
	 * @param   array    $contexts  An array of item contexts.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @return  void.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @since   3.1&lt;br /&gt;
	 */&lt;br /&gt;
	protected function batchTag($value, $pks, $contexts)&lt;br /&gt;
	{&lt;br /&gt;
		$tagsHelper = new JHelperTags();&lt;br /&gt;
		$tagsHelper-&amp;gt;tagItems($value, $pks, $contexts);&lt;br /&gt;
&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And modify your batch method by adding&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		if (!empty($commands[&#039;tag&#039;]))&lt;br /&gt;
		{&lt;br /&gt;
			if (!$this-&amp;gt;batchTag($commands[&#039;tag&#039;], $pks, $contexts))&lt;br /&gt;
			{&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			$done = true;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pay attention to any JSON encoded strings that you need special handling for in batch processing -- remember that you are saving a copy of the core fields and you need save to work as expected.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s it, now create some tags, tag some items and you are set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]][[Category:Content Tags]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117740</id>
		<title>J3.x:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117740"/>
		<updated>2014-04-26T15:34:43Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* table */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{copyedit}}&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Joomla&#039;s tagging system is used in all core content extensions and  is designed to be be easy to integrate into other extensions that use standard Joomla design patterns. &lt;br /&gt;
&lt;br /&gt;
Using tags in an extension is fairly straightforward. It requires these changes to a typical extension:&lt;br /&gt;
&lt;br /&gt;
* Register a &#039;Content type&#039; for the extension view(s)&lt;br /&gt;
* Add &#039;Observer methods&#039; to the extension table class(es)&lt;br /&gt;
* Add &#039;Tag fields&#039; to the extension edit forms&lt;br /&gt;
* Add &#039;Tag display&#039; code to the extension view files &lt;br /&gt;
* Optionally, add a batch tagging method to the extension administration&lt;br /&gt;
&lt;br /&gt;
== Register a content type for each view ==&lt;br /&gt;
&lt;br /&gt;
In Joomla! 3 content types e.g. articles, weblinks, contacts, . . . are registered in the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table with a separate record for each content view e.g. article, weblink, article category, weblink category, . . .&lt;br /&gt;
&lt;br /&gt;
Your extension installer needs to create a new record for each view.&lt;br /&gt;
&lt;br /&gt;
You can create records in the installer in three different ways:&lt;br /&gt;
* in the [[J2.5:Managing_Component_Updates_(Script.php)#postflight|postflight]] method&lt;br /&gt;
* by creating a &amp;lt;tt&amp;gt;JTableContenttype&amp;lt;/tt&amp;gt; instance and adding a row, or&lt;br /&gt;
* by using SQL to create a record directly&lt;br /&gt;
&lt;br /&gt;
The structure of the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table is fairly straightforward:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Column name !! Column type !! Purpose &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_id&amp;lt;/tt&amp;gt; || int(10) || Record key&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt;|| varchar(255)|| Type title e.g. &amp;lt;tt&amp;gt;Article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_alias&amp;lt;/tt&amp;gt; || varchar(255)|| Type alias e.g. &amp;lt;tt&amp;gt;com_content.article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt; || varchar(255)|| Information about the Table class&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt; || text || Not currently used&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt; || text || Maps the table column names to standard Joomla! names &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt; || varchar(255)|| Optional: name of a router method&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Construct the Content Type entry===&lt;br /&gt;
&lt;br /&gt;
For each table/option/view you will need to make an entry in &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table (see above).  &lt;br /&gt;
&lt;br /&gt;
====type_id====&lt;br /&gt;
This is the auto-incremented record key, nothing to be done here.&lt;br /&gt;
&lt;br /&gt;
====type_title====&lt;br /&gt;
&lt;br /&gt;
The title for your Content Type e.g. Article, Contact, Weblink,  &lt;br /&gt;
You can do this either using sql or postflight by creating an instance of JTableContenttype. Don&#039;t forget a category type if you use the Joomla categories API.&lt;br /&gt;
&lt;br /&gt;
====type_alias====&lt;br /&gt;
&lt;br /&gt;
A string identifying the component and view (that would be in the page request, typically matching the model name) e.g. &amp;lt;tt&amp;gt;com_contact.contact&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;com_weblinks.weblink&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====table====&lt;br /&gt;
&lt;br /&gt;
You need to identify which tables used by your component contain records to be tagged. For example the &#039;Contact&#039; view of the Contacts component uses &amp;lt;tt&amp;gt;#__contact_details&amp;lt;/tt&amp;gt; and similarly the &#039;Weblinks&#039; view of the Weblinks component uses &amp;lt;tt&amp;gt;#__weblinks &amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Each of these components uses a class to write to the table: &amp;lt;tt&amp;gt;ContactTableContact&amp;lt;/tt&amp;gt; for the Contacts component and &amp;lt;tt&amp;gt;WeblinksTableWeblink&amp;lt;/tt&amp;gt; for the Weblinks component. You will need the class prefixes e.g. &amp;lt;tt&amp;gt;ContactTable&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;WeblinksTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that Category types all use the &amp;lt;tt&amp;gt;#__categories&amp;lt;/tt&amp;gt; table.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;type_table&amp;lt;/tt&amp;gt; entry gives the complete table class information for the table class as a JSON object with two elements. The first element represents your &amp;quot;special&amp;quot; table and the second an optional common table (otherwise it will default the &amp;lt;tt&amp;gt;JTableCorecontent&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Here is the JSON entry for the Weblink content type:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__weblinks&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Weblink&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;WeblinksTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__ucm_content&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;ucm_id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Corecontent&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;JTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
The values to be included are:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Entry name !! Content &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;dbtable&amp;lt;/tt&amp;gt; || Table name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;key&amp;lt;/tt&amp;gt;|| Primary key name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type&amp;lt;/tt&amp;gt; || Content type&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;prefix&amp;lt;/tt&amp;gt; || Class prefix&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt; || An option array, as used in your component constructor and getInstance() methods, may be empty&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This information enables the tagging system (and other APIs) to access your table easily. &lt;br /&gt;
&lt;br /&gt;
If you are using Joomla categories make sure to create a category type so that they can be tagged. In Joomla 3.1 and 3.1.1 there is an error where the tag field will show even if there is not a type, but this is corrected in 3.1.4.  &lt;br /&gt;
  &lt;br /&gt;
=====Notes=====&lt;br /&gt;
&lt;br /&gt;
* The table name for the common table is &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt;; this is incorrect in 3.1 and 3.1.1 data but is not currently used. The data was updated in 3.1.4.&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt; field would potentially be used for display although this is not implemented currently except in the contenttype field.  Usually it should begin with an upper case letter if it is in English. See note about how to make this translatable. To make your type names translatable add &amp;lt;tt&amp;gt;COM_TAGS_CONTENT_TYPE_+type_title=&amp;quot;Type Title&amp;quot;&amp;lt;/tt&amp;gt; in both the ini and sys.ini files.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
Rules is currently not used. It will likely be removed in favor of an asset_id for each type, but currently you can ignore this field which will be managed by JTable. &lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
This entry maps the names of specific fields in your table to a set of standard names. This mapping is stored as a JSON array with the first element mapping to the common fields and the second one mapping the other fields from the table. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important note&#039;&#039;&#039;  The JHelperTags and JUcm APIS at 3.1.1 supported arrays for this field, but as of  3.1.4 either arrays or objects with a default of objects are supported.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&#039;{&amp;quot;common&amp;quot;:[{&amp;quot;core_content_item_id&amp;quot;:&amp;quot;id&amp;quot;,&amp;quot;core_title&amp;quot;:&amp;quot;title&amp;quot;,&amp;quot;core_state&amp;quot;:&amp;quot;published&amp;quot;,&amp;quot;core_alias&amp;quot;:&amp;quot;alias&amp;quot;,&amp;quot;core_created_time&amp;quot;:&amp;quot;created_time&amp;quot;,&amp;quot;core_modified_time&amp;quot;:&amp;quot;modified_time&amp;quot;,&amp;quot;core_body&amp;quot;:&amp;quot;description&amp;quot;, &amp;quot;core_hits&amp;quot;:&amp;quot;hits&amp;quot;,&amp;quot;core_publish_up&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_publish_down&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_access&amp;quot;:&amp;quot;access&amp;quot;, &amp;quot;core_params&amp;quot;:&amp;quot;params&amp;quot;, &amp;quot;core_featured&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metadata&amp;quot;:&amp;quot;metadata&amp;quot;, &amp;quot;core_language&amp;quot;:&amp;quot;language&amp;quot;, &amp;quot;core_images&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_urls&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_version&amp;quot;:&amp;quot;version&amp;quot;, &amp;quot;core_ordering&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metakey&amp;quot;:&amp;quot;metakey&amp;quot;, &amp;quot;core_metadesc&amp;quot;:&amp;quot;metadesc&amp;quot;, &amp;quot;core_catid&amp;quot;:&amp;quot;parent_id&amp;quot;, &amp;quot;core_xreference&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;asset_id&amp;quot;:&amp;quot;asset_id&amp;quot;}], &amp;quot;special&amp;quot;: [{&amp;quot;parent_id&amp;quot;:&amp;quot;parent_id&amp;quot;,&amp;quot;lft&amp;quot;:&amp;quot;lft&amp;quot;,&amp;quot;rgt&amp;quot;:&amp;quot;rgt&amp;quot;,&amp;quot;level&amp;quot;:&amp;quot;level&amp;quot;,&amp;quot;path&amp;quot;:&amp;quot;path&amp;quot;,&amp;quot;extension&amp;quot;:&amp;quot;extension&amp;quot;,&amp;quot;note&amp;quot;:&amp;quot;note&amp;quot;}]}&#039;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is the field mapping for the Article type. Note that article uses the name attribs for the core field called params.  If your table does not contain a field, put “null” instead.  Leaving it blank may cause SQL issues. The special fields are optional.  At a minimum for common fields you need to map: content_item_id, alias and title in order to successfully create urls in the tagged items list.  You also will probably want: access, status, and language.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Router&#039;&#039;&#039; is an optional entry to include the name of a static helper router method for this type found in its front end helpers folder. &lt;br /&gt;
&lt;br /&gt;
If you only store data in &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt; you will eventually be able to leave the router field blank although this option is not currently implemented. If you do not have a custom router tags falls back to the rules found in &amp;lt;tt&amp;gt;JHelperRoute&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Modify your component&#039;s table class (or classes if you have multiple tables) ==&lt;br /&gt;
&lt;br /&gt;
Add the following to your JTable constructor:&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;_observers = new JObserverUpdater($this);&lt;br /&gt;
JObserverMapper::attachAllObservers($this);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The addition material formerly located here is no longer needed after 3.1.4&#039;&#039;&#039; Please read the document history for this page if you are required to support 3.1.0 or 3.1.1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;For joomla 3.2 I needed this line:&#039;&#039;&#039; ( Dont forget to replace weblinks by your component name )&lt;br /&gt;
&lt;br /&gt;
JObserverMapper::addObserverClassToClass(&#039;JTableObserverTags&#039;, &#039;WeblinksTableWeblink&#039;, array(&#039;typeAlias&#039; =&amp;gt; &#039;com_weblinks.weblink&#039;));&lt;br /&gt;
&lt;br /&gt;
== Add tags to the getItem() method of the model ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: this was only required in 3.1.0 and 3.1.1. It should not be used in 3.1.4 or later.&#039;&#039;&#039; Please read the history of this page if you need instructions for older versions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add a tag field to edit screens ==&lt;br /&gt;
&lt;br /&gt;
In any edit layouts where you want to allow tagging, you need to add the field to the xml and the appropriate layouts if necessary. The core layouts us a JLayout to manage this and the same layout by any extension.&lt;br /&gt;
&lt;br /&gt;
Update: Note that in &#039;&#039;&#039;3.1.1 only&#039;&#039;&#039; there is special handling of this in the core.  Tags in the edit screen &#039;&#039;&#039;MUST&#039;&#039;&#039; be part of a metadata &amp;lt;fields&amp;gt;&amp;lt;/fields&amp;gt; group.  The core provides two JLayouts to help you manage standard layouts, one (details) for the metadata and one for the sidebar that includes the tabs. In update an extension to 3.1.4 you may need to fix adjust your edit views.&lt;br /&gt;
&lt;br /&gt;
In 3.1.4 or later this special handling is not necessary.  Best practice is to use the standard JLayouts since this provides a consistent experience for users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field loads all the Javascript libraries required. You don&#039;t need to worry about that.&lt;br /&gt;
&lt;br /&gt;
The field supports two modes:  &lt;br /&gt;
* &#039;&#039;&#039;Nested tags mode.&#039;&#039;&#039; Hierarchical tag list. Doesn&#039;t support on the fly tag creation.&lt;br /&gt;
* &#039;&#039;&#039;AJAX mode.&#039;&#039;&#039;  Tags are searched while user types (3 min. chars required to launch the AJAX search). Custom tags are added by pressing ENTER or COMMA keys. Tags show the global route/path. Example: &amp;lt;code&amp;gt;grandpa/parent/tag&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field mode can be forced or use the com_tags setting &amp;lt;code&amp;gt;Tag field mode&amp;lt;/code&amp;gt; to determine its mode. To set/force the field mode we have to add &#039;&#039;&#039;mode=&amp;quot;ajax&amp;quot;&#039;&#039;&#039; or &#039;&#039;&#039;mode=&amp;quot;nested&amp;quot;&#039;&#039;&#039; to the tag field definition.  &lt;br /&gt;
&lt;br /&gt;
Example of forced AJAX mode:  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The field also includes an attribute to allow/deny the user to enter custom values. Currently this only works in AJAX mode. The attribute has to be added to the field definition like **custom=&amp;quot;allow&amp;quot;** or **custom=&amp;quot;deny&amp;quot;**  &lt;br /&gt;
&lt;br /&gt;
Example field definition with custom tags denied:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot; custom=&amp;quot;denied&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
if it is not already there. Usually multiple should be true unless you have a specific reason for it not to be.&lt;br /&gt;
In the core components in administrator, the field is in the group shown on the right, below the language field.&lt;br /&gt;
&lt;br /&gt;
Note: As of 3.1.2 if you wish to use the field to designate parent tags you must add parent=&amp;quot;parent&amp;quot; to the xml definition of the field.&lt;br /&gt;
&lt;br /&gt;
== Prepare the view ==&lt;br /&gt;
&lt;br /&gt;
Add an appropriate version of this to your view.html.php file before loading the layout:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$item-&amp;gt;tags = new JHelperTags;&lt;br /&gt;
$item-&amp;gt;tags-&amp;gt;getItemTags(&#039;com_newsfeeds.newsfeed.&#039; , $this-&amp;gt;item-&amp;gt;id); &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter should match a type in the types table. The second parameter is the primary key under which this record is stored in your table. This would be used in any display in any view where you want the tags associated with the item to display.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Set up the display ==&lt;br /&gt;
&lt;br /&gt;
In any layout where you want to display the tags associated with an item add:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Changing the object and property names as appropriate.&lt;br /&gt;
&lt;br /&gt;
You will most likely want to add the show_tags parameter to the item parameters, the menu item parameters and component configuration as appropriate for your use case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;?php if ($this-&amp;gt;params-&amp;gt;get(&#039;show_tags&#039;, 1) &amp;amp;&amp;amp; !empty($this-&amp;gt;item-&amp;gt;tags)) : ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
	&amp;lt;?php endif; ?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Batch processing ==&lt;br /&gt;
If you want to add the ability to do batch tagging to a backend list view do the following.&lt;br /&gt;
&lt;br /&gt;
Add tag to the default_batch layout&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
        &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&lt;br /&gt;
				&amp;lt;?php echo JHtml::_(&#039;batch.tag&#039;);?&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;/div&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add tag to the button class in the modal footer (Not totally necessary at this point, but good for potential future changes e.g. if unTag is added.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;button class=&amp;quot;btn&amp;quot; type=&amp;quot;button&amp;quot; onclick=&amp;quot;document.id(&#039;batch-category-id&#039;).value=&#039;&#039;;document.id(&#039;batch-access&#039;).value=&#039;&#039;;document.id(&#039;batch-language-id&#039;).value=&#039;&#039;;document.id(&#039;batch-user-id&#039;).value=&#039;&#039;;document.id(&#039;batch-tag-id)&#039;).value=&#039;&#039;&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a batch method to your model if you are not extending JModelAdmin or overriding the batch method. ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Batch tag a list of item.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @param   integer  $value     The value of the new tag.&lt;br /&gt;
	 * @param   array    $pks       An array of row IDs.&lt;br /&gt;
	 * @param   array    $contexts  An array of item contexts.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @return  void.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @since   3.1&lt;br /&gt;
	 */&lt;br /&gt;
	protected function batchTag($value, $pks, $contexts)&lt;br /&gt;
	{&lt;br /&gt;
		$tagsHelper = new JHelperTags();&lt;br /&gt;
		$tagsHelper-&amp;gt;tagItems($value, $pks, $contexts);&lt;br /&gt;
&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And modify your batch method by adding&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		if (!empty($commands[&#039;tag&#039;]))&lt;br /&gt;
		{&lt;br /&gt;
			if (!$this-&amp;gt;batchTag($commands[&#039;tag&#039;], $pks, $contexts))&lt;br /&gt;
			{&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			$done = true;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pay attention to any JSON encoded strings that you need special handling for in batch processing -- remember that you are saving a copy of the core fields and you need save to work as expected.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s it, now create some tags, tag some items and you are set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]][[Category:Content Tags]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117739</id>
		<title>J3.x:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117739"/>
		<updated>2014-04-26T15:34:12Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* Notes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{copyedit}}&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Joomla&#039;s tagging system is used in all core content extensions and  is designed to be be easy to integrate into other extensions that use standard Joomla design patterns. &lt;br /&gt;
&lt;br /&gt;
Using tags in an extension is fairly straightforward. It requires these changes to a typical extension:&lt;br /&gt;
&lt;br /&gt;
* Register a &#039;Content type&#039; for the extension view(s)&lt;br /&gt;
* Add &#039;Observer methods&#039; to the extension table class(es)&lt;br /&gt;
* Add &#039;Tag fields&#039; to the extension edit forms&lt;br /&gt;
* Add &#039;Tag display&#039; code to the extension view files &lt;br /&gt;
* Optionally, add a batch tagging method to the extension administration&lt;br /&gt;
&lt;br /&gt;
== Register a content type for each view ==&lt;br /&gt;
&lt;br /&gt;
In Joomla! 3 content types e.g. articles, weblinks, contacts, . . . are registered in the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table with a separate record for each content view e.g. article, weblink, article category, weblink category, . . .&lt;br /&gt;
&lt;br /&gt;
Your extension installer needs to create a new record for each view.&lt;br /&gt;
&lt;br /&gt;
You can create records in the installer in three different ways:&lt;br /&gt;
* in the [[J2.5:Managing_Component_Updates_(Script.php)#postflight|postflight]] method&lt;br /&gt;
* by creating a &amp;lt;tt&amp;gt;JTableContenttype&amp;lt;/tt&amp;gt; instance and adding a row, or&lt;br /&gt;
* by using SQL to create a record directly&lt;br /&gt;
&lt;br /&gt;
The structure of the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table is fairly straightforward:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Column name !! Column type !! Purpose &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_id&amp;lt;/tt&amp;gt; || int(10) || Record key&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt;|| varchar(255)|| Type title e.g. &amp;lt;tt&amp;gt;Article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_alias&amp;lt;/tt&amp;gt; || varchar(255)|| Type alias e.g. &amp;lt;tt&amp;gt;com_content.article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt; || varchar(255)|| Information about the Table class&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt; || text || Not currently used&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt; || text || Maps the table column names to standard Joomla! names &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt; || varchar(255)|| Optional: name of a router method&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Construct the Content Type entry===&lt;br /&gt;
&lt;br /&gt;
For each table/option/view you will need to make an entry in &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table (see above).  &lt;br /&gt;
&lt;br /&gt;
====type_id====&lt;br /&gt;
This is the auto-incremented record key, nothing to be done here.&lt;br /&gt;
&lt;br /&gt;
====type_title====&lt;br /&gt;
&lt;br /&gt;
The title for your Content Type e.g. Article, Contact, Weblink,  &lt;br /&gt;
You can do this either using sql or postflight by creating an instance of JTableContenttype. Don&#039;t forget a category type if you use the Joomla categories API.&lt;br /&gt;
&lt;br /&gt;
====type_alias====&lt;br /&gt;
&lt;br /&gt;
A string identifying the component and view (that would be in the page request, typically matching the model name) e.g. &amp;lt;tt&amp;gt;com_contact.contact&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;com_weblinks.weblink&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
You need to identify which tables used by your component contain records to be tagged. For example the &#039;Contact&#039; view of the Contacts component uses &amp;lt;tt&amp;gt;#__contact_details&amp;lt;/tt&amp;gt; and similarly the &#039;Weblinks&#039; view of the Weblinks component uses &amp;lt;tt&amp;gt;#__weblinks &amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Each of these components uses a class to write to the table: &amp;lt;tt&amp;gt;ContactTableContact&amp;lt;/tt&amp;gt; for the Contacts component and &amp;lt;tt&amp;gt;WeblinksTableWeblink&amp;lt;/tt&amp;gt; for the Weblinks component. You will need the class prefixes e.g. &amp;lt;tt&amp;gt;ContactTable&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;WeblinksTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that Category types all use the &amp;lt;tt&amp;gt;#__categories&amp;lt;/tt&amp;gt; table.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;type_table&amp;lt;/tt&amp;gt; entry gives the complete table class information for the table class as a JSON object with two elements. The first element represents your &amp;quot;special&amp;quot; table and the second an optional common table (otherwise it will default the &amp;lt;tt&amp;gt;JTableCorecontent&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Here is the JSON entry for the Weblink content type:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__weblinks&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Weblink&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;WeblinksTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__ucm_content&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;ucm_id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Corecontent&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;JTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
The values to be included are:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Entry name !! Content &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;dbtable&amp;lt;/tt&amp;gt; || Table name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;key&amp;lt;/tt&amp;gt;|| Primary key name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type&amp;lt;/tt&amp;gt; || Content type&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;prefix&amp;lt;/tt&amp;gt; || Class prefix&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt; || An option array, as used in your component constructor and getInstance() methods, may be empty&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This information enables the tagging system (and other APIs) to access your table easily. &lt;br /&gt;
&lt;br /&gt;
If you are using Joomla categories make sure to create a category type so that they can be tagged. In Joomla 3.1 and 3.1.1 there is an error where the tag field will show even if there is not a type, but this is corrected in 3.1.4.  &lt;br /&gt;
  &lt;br /&gt;
=====Notes=====&lt;br /&gt;
&lt;br /&gt;
* The table name for the common table is &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt;; this is incorrect in 3.1 and 3.1.1 data but is not currently used. The data was updated in 3.1.4.&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt; field would potentially be used for display although this is not implemented currently except in the contenttype field.  Usually it should begin with an upper case letter if it is in English. See note about how to make this translatable. To make your type names translatable add &amp;lt;tt&amp;gt;COM_TAGS_CONTENT_TYPE_+type_title=&amp;quot;Type Title&amp;quot;&amp;lt;/tt&amp;gt; in both the ini and sys.ini files.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
Rules is currently not used. It will likely be removed in favor of an asset_id for each type, but currently you can ignore this field which will be managed by JTable. &lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
This entry maps the names of specific fields in your table to a set of standard names. This mapping is stored as a JSON array with the first element mapping to the common fields and the second one mapping the other fields from the table. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important note&#039;&#039;&#039;  The JHelperTags and JUcm APIS at 3.1.1 supported arrays for this field, but as of  3.1.4 either arrays or objects with a default of objects are supported.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&#039;{&amp;quot;common&amp;quot;:[{&amp;quot;core_content_item_id&amp;quot;:&amp;quot;id&amp;quot;,&amp;quot;core_title&amp;quot;:&amp;quot;title&amp;quot;,&amp;quot;core_state&amp;quot;:&amp;quot;published&amp;quot;,&amp;quot;core_alias&amp;quot;:&amp;quot;alias&amp;quot;,&amp;quot;core_created_time&amp;quot;:&amp;quot;created_time&amp;quot;,&amp;quot;core_modified_time&amp;quot;:&amp;quot;modified_time&amp;quot;,&amp;quot;core_body&amp;quot;:&amp;quot;description&amp;quot;, &amp;quot;core_hits&amp;quot;:&amp;quot;hits&amp;quot;,&amp;quot;core_publish_up&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_publish_down&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_access&amp;quot;:&amp;quot;access&amp;quot;, &amp;quot;core_params&amp;quot;:&amp;quot;params&amp;quot;, &amp;quot;core_featured&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metadata&amp;quot;:&amp;quot;metadata&amp;quot;, &amp;quot;core_language&amp;quot;:&amp;quot;language&amp;quot;, &amp;quot;core_images&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_urls&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_version&amp;quot;:&amp;quot;version&amp;quot;, &amp;quot;core_ordering&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metakey&amp;quot;:&amp;quot;metakey&amp;quot;, &amp;quot;core_metadesc&amp;quot;:&amp;quot;metadesc&amp;quot;, &amp;quot;core_catid&amp;quot;:&amp;quot;parent_id&amp;quot;, &amp;quot;core_xreference&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;asset_id&amp;quot;:&amp;quot;asset_id&amp;quot;}], &amp;quot;special&amp;quot;: [{&amp;quot;parent_id&amp;quot;:&amp;quot;parent_id&amp;quot;,&amp;quot;lft&amp;quot;:&amp;quot;lft&amp;quot;,&amp;quot;rgt&amp;quot;:&amp;quot;rgt&amp;quot;,&amp;quot;level&amp;quot;:&amp;quot;level&amp;quot;,&amp;quot;path&amp;quot;:&amp;quot;path&amp;quot;,&amp;quot;extension&amp;quot;:&amp;quot;extension&amp;quot;,&amp;quot;note&amp;quot;:&amp;quot;note&amp;quot;}]}&#039;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is the field mapping for the Article type. Note that article uses the name attribs for the core field called params.  If your table does not contain a field, put “null” instead.  Leaving it blank may cause SQL issues. The special fields are optional.  At a minimum for common fields you need to map: content_item_id, alias and title in order to successfully create urls in the tagged items list.  You also will probably want: access, status, and language.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Router&#039;&#039;&#039; is an optional entry to include the name of a static helper router method for this type found in its front end helpers folder. &lt;br /&gt;
&lt;br /&gt;
If you only store data in &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt; you will eventually be able to leave the router field blank although this option is not currently implemented. If you do not have a custom router tags falls back to the rules found in &amp;lt;tt&amp;gt;JHelperRoute&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Modify your component&#039;s table class (or classes if you have multiple tables) ==&lt;br /&gt;
&lt;br /&gt;
Add the following to your JTable constructor:&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;_observers = new JObserverUpdater($this);&lt;br /&gt;
JObserverMapper::attachAllObservers($this);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The addition material formerly located here is no longer needed after 3.1.4&#039;&#039;&#039; Please read the document history for this page if you are required to support 3.1.0 or 3.1.1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;For joomla 3.2 I needed this line:&#039;&#039;&#039; ( Dont forget to replace weblinks by your component name )&lt;br /&gt;
&lt;br /&gt;
JObserverMapper::addObserverClassToClass(&#039;JTableObserverTags&#039;, &#039;WeblinksTableWeblink&#039;, array(&#039;typeAlias&#039; =&amp;gt; &#039;com_weblinks.weblink&#039;));&lt;br /&gt;
&lt;br /&gt;
== Add tags to the getItem() method of the model ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: this was only required in 3.1.0 and 3.1.1. It should not be used in 3.1.4 or later.&#039;&#039;&#039; Please read the history of this page if you need instructions for older versions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add a tag field to edit screens ==&lt;br /&gt;
&lt;br /&gt;
In any edit layouts where you want to allow tagging, you need to add the field to the xml and the appropriate layouts if necessary. The core layouts us a JLayout to manage this and the same layout by any extension.&lt;br /&gt;
&lt;br /&gt;
Update: Note that in &#039;&#039;&#039;3.1.1 only&#039;&#039;&#039; there is special handling of this in the core.  Tags in the edit screen &#039;&#039;&#039;MUST&#039;&#039;&#039; be part of a metadata &amp;lt;fields&amp;gt;&amp;lt;/fields&amp;gt; group.  The core provides two JLayouts to help you manage standard layouts, one (details) for the metadata and one for the sidebar that includes the tabs. In update an extension to 3.1.4 you may need to fix adjust your edit views.&lt;br /&gt;
&lt;br /&gt;
In 3.1.4 or later this special handling is not necessary.  Best practice is to use the standard JLayouts since this provides a consistent experience for users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field loads all the Javascript libraries required. You don&#039;t need to worry about that.&lt;br /&gt;
&lt;br /&gt;
The field supports two modes:  &lt;br /&gt;
* &#039;&#039;&#039;Nested tags mode.&#039;&#039;&#039; Hierarchical tag list. Doesn&#039;t support on the fly tag creation.&lt;br /&gt;
* &#039;&#039;&#039;AJAX mode.&#039;&#039;&#039;  Tags are searched while user types (3 min. chars required to launch the AJAX search). Custom tags are added by pressing ENTER or COMMA keys. Tags show the global route/path. Example: &amp;lt;code&amp;gt;grandpa/parent/tag&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field mode can be forced or use the com_tags setting &amp;lt;code&amp;gt;Tag field mode&amp;lt;/code&amp;gt; to determine its mode. To set/force the field mode we have to add &#039;&#039;&#039;mode=&amp;quot;ajax&amp;quot;&#039;&#039;&#039; or &#039;&#039;&#039;mode=&amp;quot;nested&amp;quot;&#039;&#039;&#039; to the tag field definition.  &lt;br /&gt;
&lt;br /&gt;
Example of forced AJAX mode:  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The field also includes an attribute to allow/deny the user to enter custom values. Currently this only works in AJAX mode. The attribute has to be added to the field definition like **custom=&amp;quot;allow&amp;quot;** or **custom=&amp;quot;deny&amp;quot;**  &lt;br /&gt;
&lt;br /&gt;
Example field definition with custom tags denied:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot; custom=&amp;quot;denied&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
if it is not already there. Usually multiple should be true unless you have a specific reason for it not to be.&lt;br /&gt;
In the core components in administrator, the field is in the group shown on the right, below the language field.&lt;br /&gt;
&lt;br /&gt;
Note: As of 3.1.2 if you wish to use the field to designate parent tags you must add parent=&amp;quot;parent&amp;quot; to the xml definition of the field.&lt;br /&gt;
&lt;br /&gt;
== Prepare the view ==&lt;br /&gt;
&lt;br /&gt;
Add an appropriate version of this to your view.html.php file before loading the layout:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$item-&amp;gt;tags = new JHelperTags;&lt;br /&gt;
$item-&amp;gt;tags-&amp;gt;getItemTags(&#039;com_newsfeeds.newsfeed.&#039; , $this-&amp;gt;item-&amp;gt;id); &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter should match a type in the types table. The second parameter is the primary key under which this record is stored in your table. This would be used in any display in any view where you want the tags associated with the item to display.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Set up the display ==&lt;br /&gt;
&lt;br /&gt;
In any layout where you want to display the tags associated with an item add:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Changing the object and property names as appropriate.&lt;br /&gt;
&lt;br /&gt;
You will most likely want to add the show_tags parameter to the item parameters, the menu item parameters and component configuration as appropriate for your use case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;?php if ($this-&amp;gt;params-&amp;gt;get(&#039;show_tags&#039;, 1) &amp;amp;&amp;amp; !empty($this-&amp;gt;item-&amp;gt;tags)) : ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
	&amp;lt;?php endif; ?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Batch processing ==&lt;br /&gt;
If you want to add the ability to do batch tagging to a backend list view do the following.&lt;br /&gt;
&lt;br /&gt;
Add tag to the default_batch layout&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
        &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&lt;br /&gt;
				&amp;lt;?php echo JHtml::_(&#039;batch.tag&#039;);?&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;/div&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add tag to the button class in the modal footer (Not totally necessary at this point, but good for potential future changes e.g. if unTag is added.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;button class=&amp;quot;btn&amp;quot; type=&amp;quot;button&amp;quot; onclick=&amp;quot;document.id(&#039;batch-category-id&#039;).value=&#039;&#039;;document.id(&#039;batch-access&#039;).value=&#039;&#039;;document.id(&#039;batch-language-id&#039;).value=&#039;&#039;;document.id(&#039;batch-user-id&#039;).value=&#039;&#039;;document.id(&#039;batch-tag-id)&#039;).value=&#039;&#039;&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a batch method to your model if you are not extending JModelAdmin or overriding the batch method. ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Batch tag a list of item.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @param   integer  $value     The value of the new tag.&lt;br /&gt;
	 * @param   array    $pks       An array of row IDs.&lt;br /&gt;
	 * @param   array    $contexts  An array of item contexts.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @return  void.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @since   3.1&lt;br /&gt;
	 */&lt;br /&gt;
	protected function batchTag($value, $pks, $contexts)&lt;br /&gt;
	{&lt;br /&gt;
		$tagsHelper = new JHelperTags();&lt;br /&gt;
		$tagsHelper-&amp;gt;tagItems($value, $pks, $contexts);&lt;br /&gt;
&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And modify your batch method by adding&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		if (!empty($commands[&#039;tag&#039;]))&lt;br /&gt;
		{&lt;br /&gt;
			if (!$this-&amp;gt;batchTag($commands[&#039;tag&#039;], $pks, $contexts))&lt;br /&gt;
			{&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			$done = true;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pay attention to any JSON encoded strings that you need special handling for in batch processing -- remember that you are saving a copy of the core fields and you need save to work as expected.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s it, now create some tags, tag some items and you are set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]][[Category:Content Tags]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117738</id>
		<title>J3.x:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117738"/>
		<updated>2014-04-26T15:33:41Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* type_alias */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{copyedit}}&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Joomla&#039;s tagging system is used in all core content extensions and  is designed to be be easy to integrate into other extensions that use standard Joomla design patterns. &lt;br /&gt;
&lt;br /&gt;
Using tags in an extension is fairly straightforward. It requires these changes to a typical extension:&lt;br /&gt;
&lt;br /&gt;
* Register a &#039;Content type&#039; for the extension view(s)&lt;br /&gt;
* Add &#039;Observer methods&#039; to the extension table class(es)&lt;br /&gt;
* Add &#039;Tag fields&#039; to the extension edit forms&lt;br /&gt;
* Add &#039;Tag display&#039; code to the extension view files &lt;br /&gt;
* Optionally, add a batch tagging method to the extension administration&lt;br /&gt;
&lt;br /&gt;
== Register a content type for each view ==&lt;br /&gt;
&lt;br /&gt;
In Joomla! 3 content types e.g. articles, weblinks, contacts, . . . are registered in the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table with a separate record for each content view e.g. article, weblink, article category, weblink category, . . .&lt;br /&gt;
&lt;br /&gt;
Your extension installer needs to create a new record for each view.&lt;br /&gt;
&lt;br /&gt;
You can create records in the installer in three different ways:&lt;br /&gt;
* in the [[J2.5:Managing_Component_Updates_(Script.php)#postflight|postflight]] method&lt;br /&gt;
* by creating a &amp;lt;tt&amp;gt;JTableContenttype&amp;lt;/tt&amp;gt; instance and adding a row, or&lt;br /&gt;
* by using SQL to create a record directly&lt;br /&gt;
&lt;br /&gt;
The structure of the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table is fairly straightforward:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Column name !! Column type !! Purpose &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_id&amp;lt;/tt&amp;gt; || int(10) || Record key&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt;|| varchar(255)|| Type title e.g. &amp;lt;tt&amp;gt;Article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_alias&amp;lt;/tt&amp;gt; || varchar(255)|| Type alias e.g. &amp;lt;tt&amp;gt;com_content.article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt; || varchar(255)|| Information about the Table class&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt; || text || Not currently used&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt; || text || Maps the table column names to standard Joomla! names &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt; || varchar(255)|| Optional: name of a router method&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Construct the Content Type entry===&lt;br /&gt;
&lt;br /&gt;
For each table/option/view you will need to make an entry in &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table (see above).  &lt;br /&gt;
&lt;br /&gt;
====type_id====&lt;br /&gt;
This is the auto-incremented record key, nothing to be done here.&lt;br /&gt;
&lt;br /&gt;
====type_title====&lt;br /&gt;
&lt;br /&gt;
The title for your Content Type e.g. Article, Contact, Weblink,  &lt;br /&gt;
You can do this either using sql or postflight by creating an instance of JTableContenttype. Don&#039;t forget a category type if you use the Joomla categories API.&lt;br /&gt;
&lt;br /&gt;
====type_alias====&lt;br /&gt;
&lt;br /&gt;
A string identifying the component and view (that would be in the page request, typically matching the model name) e.g. &amp;lt;tt&amp;gt;com_contact.contact&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;com_weblinks.weblink&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
You need to identify which tables used by your component contain records to be tagged. For example the &#039;Contact&#039; view of the Contacts component uses &amp;lt;tt&amp;gt;#__contact_details&amp;lt;/tt&amp;gt; and similarly the &#039;Weblinks&#039; view of the Weblinks component uses &amp;lt;tt&amp;gt;#__weblinks &amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Each of these components uses a class to write to the table: &amp;lt;tt&amp;gt;ContactTableContact&amp;lt;/tt&amp;gt; for the Contacts component and &amp;lt;tt&amp;gt;WeblinksTableWeblink&amp;lt;/tt&amp;gt; for the Weblinks component. You will need the class prefixes e.g. &amp;lt;tt&amp;gt;ContactTable&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;WeblinksTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that Category types all use the &amp;lt;tt&amp;gt;#__categories&amp;lt;/tt&amp;gt; table.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;type_table&amp;lt;/tt&amp;gt; entry gives the complete table class information for the table class as a JSON object with two elements. The first element represents your &amp;quot;special&amp;quot; table and the second an optional common table (otherwise it will default the &amp;lt;tt&amp;gt;JTableCorecontent&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Here is the JSON entry for the Weblink content type:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__weblinks&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Weblink&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;WeblinksTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__ucm_content&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;ucm_id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Corecontent&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;JTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
The values to be included are:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Entry name !! Content &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;dbtable&amp;lt;/tt&amp;gt; || Table name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;key&amp;lt;/tt&amp;gt;|| Primary key name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type&amp;lt;/tt&amp;gt; || Content type&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;prefix&amp;lt;/tt&amp;gt; || Class prefix&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt; || An option array, as used in your component constructor and getInstance() methods, may be empty&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This information enables the tagging system (and other APIs) to access your table easily. &lt;br /&gt;
&lt;br /&gt;
If you are using Joomla categories make sure to create a category type so that they can be tagged. In Joomla 3.1 and 3.1.1 there is an error where the tag field will show even if there is not a type, but this is corrected in 3.1.4.  &lt;br /&gt;
  &lt;br /&gt;
=====Notes=====&lt;br /&gt;
&lt;br /&gt;
* The table name for the common table is &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt;; this is INCORRECT in 3.1 and 3.1.1 data but is not currently used. The data was updated in 3.1.4.&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt; field would potentially be used for display although this is not implemented currently except in the contenttype field.  Usually it should begin with an upper case letter if it is in English. See note about how to make this translatable. To make your type names translatable add &amp;lt;tt&amp;gt;COM_TAGS_CONTENT_TYPE_+type_title=&amp;quot;Type Title&amp;quot;&amp;lt;/tt&amp;gt; in both the ini and sys.ini files. &lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
Rules is currently not used. It will likely be removed in favor of an asset_id for each type, but currently you can ignore this field which will be managed by JTable. &lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
This entry maps the names of specific fields in your table to a set of standard names. This mapping is stored as a JSON array with the first element mapping to the common fields and the second one mapping the other fields from the table. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important note&#039;&#039;&#039;  The JHelperTags and JUcm APIS at 3.1.1 supported arrays for this field, but as of  3.1.4 either arrays or objects with a default of objects are supported.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&#039;{&amp;quot;common&amp;quot;:[{&amp;quot;core_content_item_id&amp;quot;:&amp;quot;id&amp;quot;,&amp;quot;core_title&amp;quot;:&amp;quot;title&amp;quot;,&amp;quot;core_state&amp;quot;:&amp;quot;published&amp;quot;,&amp;quot;core_alias&amp;quot;:&amp;quot;alias&amp;quot;,&amp;quot;core_created_time&amp;quot;:&amp;quot;created_time&amp;quot;,&amp;quot;core_modified_time&amp;quot;:&amp;quot;modified_time&amp;quot;,&amp;quot;core_body&amp;quot;:&amp;quot;description&amp;quot;, &amp;quot;core_hits&amp;quot;:&amp;quot;hits&amp;quot;,&amp;quot;core_publish_up&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_publish_down&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_access&amp;quot;:&amp;quot;access&amp;quot;, &amp;quot;core_params&amp;quot;:&amp;quot;params&amp;quot;, &amp;quot;core_featured&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metadata&amp;quot;:&amp;quot;metadata&amp;quot;, &amp;quot;core_language&amp;quot;:&amp;quot;language&amp;quot;, &amp;quot;core_images&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_urls&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_version&amp;quot;:&amp;quot;version&amp;quot;, &amp;quot;core_ordering&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metakey&amp;quot;:&amp;quot;metakey&amp;quot;, &amp;quot;core_metadesc&amp;quot;:&amp;quot;metadesc&amp;quot;, &amp;quot;core_catid&amp;quot;:&amp;quot;parent_id&amp;quot;, &amp;quot;core_xreference&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;asset_id&amp;quot;:&amp;quot;asset_id&amp;quot;}], &amp;quot;special&amp;quot;: [{&amp;quot;parent_id&amp;quot;:&amp;quot;parent_id&amp;quot;,&amp;quot;lft&amp;quot;:&amp;quot;lft&amp;quot;,&amp;quot;rgt&amp;quot;:&amp;quot;rgt&amp;quot;,&amp;quot;level&amp;quot;:&amp;quot;level&amp;quot;,&amp;quot;path&amp;quot;:&amp;quot;path&amp;quot;,&amp;quot;extension&amp;quot;:&amp;quot;extension&amp;quot;,&amp;quot;note&amp;quot;:&amp;quot;note&amp;quot;}]}&#039;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is the field mapping for the Article type. Note that article uses the name attribs for the core field called params.  If your table does not contain a field, put “null” instead.  Leaving it blank may cause SQL issues. The special fields are optional.  At a minimum for common fields you need to map: content_item_id, alias and title in order to successfully create urls in the tagged items list.  You also will probably want: access, status, and language.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Router&#039;&#039;&#039; is an optional entry to include the name of a static helper router method for this type found in its front end helpers folder. &lt;br /&gt;
&lt;br /&gt;
If you only store data in &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt; you will eventually be able to leave the router field blank although this option is not currently implemented. If you do not have a custom router tags falls back to the rules found in &amp;lt;tt&amp;gt;JHelperRoute&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Modify your component&#039;s table class (or classes if you have multiple tables) ==&lt;br /&gt;
&lt;br /&gt;
Add the following to your JTable constructor:&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;_observers = new JObserverUpdater($this);&lt;br /&gt;
JObserverMapper::attachAllObservers($this);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The addition material formerly located here is no longer needed after 3.1.4&#039;&#039;&#039; Please read the document history for this page if you are required to support 3.1.0 or 3.1.1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;For joomla 3.2 I needed this line:&#039;&#039;&#039; ( Dont forget to replace weblinks by your component name )&lt;br /&gt;
&lt;br /&gt;
JObserverMapper::addObserverClassToClass(&#039;JTableObserverTags&#039;, &#039;WeblinksTableWeblink&#039;, array(&#039;typeAlias&#039; =&amp;gt; &#039;com_weblinks.weblink&#039;));&lt;br /&gt;
&lt;br /&gt;
== Add tags to the getItem() method of the model ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: this was only required in 3.1.0 and 3.1.1. It should not be used in 3.1.4 or later.&#039;&#039;&#039; Please read the history of this page if you need instructions for older versions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add a tag field to edit screens ==&lt;br /&gt;
&lt;br /&gt;
In any edit layouts where you want to allow tagging, you need to add the field to the xml and the appropriate layouts if necessary. The core layouts us a JLayout to manage this and the same layout by any extension.&lt;br /&gt;
&lt;br /&gt;
Update: Note that in &#039;&#039;&#039;3.1.1 only&#039;&#039;&#039; there is special handling of this in the core.  Tags in the edit screen &#039;&#039;&#039;MUST&#039;&#039;&#039; be part of a metadata &amp;lt;fields&amp;gt;&amp;lt;/fields&amp;gt; group.  The core provides two JLayouts to help you manage standard layouts, one (details) for the metadata and one for the sidebar that includes the tabs. In update an extension to 3.1.4 you may need to fix adjust your edit views.&lt;br /&gt;
&lt;br /&gt;
In 3.1.4 or later this special handling is not necessary.  Best practice is to use the standard JLayouts since this provides a consistent experience for users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field loads all the Javascript libraries required. You don&#039;t need to worry about that.&lt;br /&gt;
&lt;br /&gt;
The field supports two modes:  &lt;br /&gt;
* &#039;&#039;&#039;Nested tags mode.&#039;&#039;&#039; Hierarchical tag list. Doesn&#039;t support on the fly tag creation.&lt;br /&gt;
* &#039;&#039;&#039;AJAX mode.&#039;&#039;&#039;  Tags are searched while user types (3 min. chars required to launch the AJAX search). Custom tags are added by pressing ENTER or COMMA keys. Tags show the global route/path. Example: &amp;lt;code&amp;gt;grandpa/parent/tag&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field mode can be forced or use the com_tags setting &amp;lt;code&amp;gt;Tag field mode&amp;lt;/code&amp;gt; to determine its mode. To set/force the field mode we have to add &#039;&#039;&#039;mode=&amp;quot;ajax&amp;quot;&#039;&#039;&#039; or &#039;&#039;&#039;mode=&amp;quot;nested&amp;quot;&#039;&#039;&#039; to the tag field definition.  &lt;br /&gt;
&lt;br /&gt;
Example of forced AJAX mode:  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The field also includes an attribute to allow/deny the user to enter custom values. Currently this only works in AJAX mode. The attribute has to be added to the field definition like **custom=&amp;quot;allow&amp;quot;** or **custom=&amp;quot;deny&amp;quot;**  &lt;br /&gt;
&lt;br /&gt;
Example field definition with custom tags denied:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot; custom=&amp;quot;denied&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
if it is not already there. Usually multiple should be true unless you have a specific reason for it not to be.&lt;br /&gt;
In the core components in administrator, the field is in the group shown on the right, below the language field.&lt;br /&gt;
&lt;br /&gt;
Note: As of 3.1.2 if you wish to use the field to designate parent tags you must add parent=&amp;quot;parent&amp;quot; to the xml definition of the field.&lt;br /&gt;
&lt;br /&gt;
== Prepare the view ==&lt;br /&gt;
&lt;br /&gt;
Add an appropriate version of this to your view.html.php file before loading the layout:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$item-&amp;gt;tags = new JHelperTags;&lt;br /&gt;
$item-&amp;gt;tags-&amp;gt;getItemTags(&#039;com_newsfeeds.newsfeed.&#039; , $this-&amp;gt;item-&amp;gt;id); &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter should match a type in the types table. The second parameter is the primary key under which this record is stored in your table. This would be used in any display in any view where you want the tags associated with the item to display.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Set up the display ==&lt;br /&gt;
&lt;br /&gt;
In any layout where you want to display the tags associated with an item add:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Changing the object and property names as appropriate.&lt;br /&gt;
&lt;br /&gt;
You will most likely want to add the show_tags parameter to the item parameters, the menu item parameters and component configuration as appropriate for your use case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;?php if ($this-&amp;gt;params-&amp;gt;get(&#039;show_tags&#039;, 1) &amp;amp;&amp;amp; !empty($this-&amp;gt;item-&amp;gt;tags)) : ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
	&amp;lt;?php endif; ?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Batch processing ==&lt;br /&gt;
If you want to add the ability to do batch tagging to a backend list view do the following.&lt;br /&gt;
&lt;br /&gt;
Add tag to the default_batch layout&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
        &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&lt;br /&gt;
				&amp;lt;?php echo JHtml::_(&#039;batch.tag&#039;);?&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;/div&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add tag to the button class in the modal footer (Not totally necessary at this point, but good for potential future changes e.g. if unTag is added.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;button class=&amp;quot;btn&amp;quot; type=&amp;quot;button&amp;quot; onclick=&amp;quot;document.id(&#039;batch-category-id&#039;).value=&#039;&#039;;document.id(&#039;batch-access&#039;).value=&#039;&#039;;document.id(&#039;batch-language-id&#039;).value=&#039;&#039;;document.id(&#039;batch-user-id&#039;).value=&#039;&#039;;document.id(&#039;batch-tag-id)&#039;).value=&#039;&#039;&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a batch method to your model if you are not extending JModelAdmin or overriding the batch method. ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Batch tag a list of item.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @param   integer  $value     The value of the new tag.&lt;br /&gt;
	 * @param   array    $pks       An array of row IDs.&lt;br /&gt;
	 * @param   array    $contexts  An array of item contexts.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @return  void.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @since   3.1&lt;br /&gt;
	 */&lt;br /&gt;
	protected function batchTag($value, $pks, $contexts)&lt;br /&gt;
	{&lt;br /&gt;
		$tagsHelper = new JHelperTags();&lt;br /&gt;
		$tagsHelper-&amp;gt;tagItems($value, $pks, $contexts);&lt;br /&gt;
&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And modify your batch method by adding&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		if (!empty($commands[&#039;tag&#039;]))&lt;br /&gt;
		{&lt;br /&gt;
			if (!$this-&amp;gt;batchTag($commands[&#039;tag&#039;], $pks, $contexts))&lt;br /&gt;
			{&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			$done = true;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pay attention to any JSON encoded strings that you need special handling for in batch processing -- remember that you are saving a copy of the core fields and you need save to work as expected.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s it, now create some tags, tag some items and you are set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]][[Category:Content Tags]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117737</id>
		<title>J3.x:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117737"/>
		<updated>2014-04-26T15:33:20Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* type_title */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{copyedit}}&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Joomla&#039;s tagging system is used in all core content extensions and  is designed to be be easy to integrate into other extensions that use standard Joomla design patterns. &lt;br /&gt;
&lt;br /&gt;
Using tags in an extension is fairly straightforward. It requires these changes to a typical extension:&lt;br /&gt;
&lt;br /&gt;
* Register a &#039;Content type&#039; for the extension view(s)&lt;br /&gt;
* Add &#039;Observer methods&#039; to the extension table class(es)&lt;br /&gt;
* Add &#039;Tag fields&#039; to the extension edit forms&lt;br /&gt;
* Add &#039;Tag display&#039; code to the extension view files &lt;br /&gt;
* Optionally, add a batch tagging method to the extension administration&lt;br /&gt;
&lt;br /&gt;
== Register a content type for each view ==&lt;br /&gt;
&lt;br /&gt;
In Joomla! 3 content types e.g. articles, weblinks, contacts, . . . are registered in the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table with a separate record for each content view e.g. article, weblink, article category, weblink category, . . .&lt;br /&gt;
&lt;br /&gt;
Your extension installer needs to create a new record for each view.&lt;br /&gt;
&lt;br /&gt;
You can create records in the installer in three different ways:&lt;br /&gt;
* in the [[J2.5:Managing_Component_Updates_(Script.php)#postflight|postflight]] method&lt;br /&gt;
* by creating a &amp;lt;tt&amp;gt;JTableContenttype&amp;lt;/tt&amp;gt; instance and adding a row, or&lt;br /&gt;
* by using SQL to create a record directly&lt;br /&gt;
&lt;br /&gt;
The structure of the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table is fairly straightforward:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Column name !! Column type !! Purpose &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_id&amp;lt;/tt&amp;gt; || int(10) || Record key&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt;|| varchar(255)|| Type title e.g. &amp;lt;tt&amp;gt;Article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_alias&amp;lt;/tt&amp;gt; || varchar(255)|| Type alias e.g. &amp;lt;tt&amp;gt;com_content.article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt; || varchar(255)|| Information about the Table class&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt; || text || Not currently used&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt; || text || Maps the table column names to standard Joomla! names &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt; || varchar(255)|| Optional: name of a router method&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Construct the Content Type entry===&lt;br /&gt;
&lt;br /&gt;
For each table/option/view you will need to make an entry in &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table (see above).  &lt;br /&gt;
&lt;br /&gt;
====type_id====&lt;br /&gt;
This is the auto-incremented record key, nothing to be done here.&lt;br /&gt;
&lt;br /&gt;
====type_title====&lt;br /&gt;
&lt;br /&gt;
The title for your Content Type e.g. Article, Contact, Weblink,  &lt;br /&gt;
You can do this either using sql or postflight by creating an instance of JTableContenttype. Don&#039;t forget a category type if you use the Joomla categories API.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;type_alias&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
A string identifying the component and view (that would be in the page request, typically matching the model name) e.g. &amp;lt;tt&amp;gt;com_contact.contact&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;com_weblinks.weblink&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
You need to identify which tables used by your component contain records to be tagged. For example the &#039;Contact&#039; view of the Contacts component uses &amp;lt;tt&amp;gt;#__contact_details&amp;lt;/tt&amp;gt; and similarly the &#039;Weblinks&#039; view of the Weblinks component uses &amp;lt;tt&amp;gt;#__weblinks &amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Each of these components uses a class to write to the table: &amp;lt;tt&amp;gt;ContactTableContact&amp;lt;/tt&amp;gt; for the Contacts component and &amp;lt;tt&amp;gt;WeblinksTableWeblink&amp;lt;/tt&amp;gt; for the Weblinks component. You will need the class prefixes e.g. &amp;lt;tt&amp;gt;ContactTable&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;WeblinksTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that Category types all use the &amp;lt;tt&amp;gt;#__categories&amp;lt;/tt&amp;gt; table.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;type_table&amp;lt;/tt&amp;gt; entry gives the complete table class information for the table class as a JSON object with two elements. The first element represents your &amp;quot;special&amp;quot; table and the second an optional common table (otherwise it will default the &amp;lt;tt&amp;gt;JTableCorecontent&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Here is the JSON entry for the Weblink content type:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__weblinks&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Weblink&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;WeblinksTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__ucm_content&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;ucm_id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Corecontent&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;JTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
The values to be included are:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Entry name !! Content &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;dbtable&amp;lt;/tt&amp;gt; || Table name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;key&amp;lt;/tt&amp;gt;|| Primary key name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type&amp;lt;/tt&amp;gt; || Content type&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;prefix&amp;lt;/tt&amp;gt; || Class prefix&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt; || An option array, as used in your component constructor and getInstance() methods, may be empty&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This information enables the tagging system (and other APIs) to access your table easily. &lt;br /&gt;
&lt;br /&gt;
If you are using Joomla categories make sure to create a category type so that they can be tagged. In Joomla 3.1 and 3.1.1 there is an error where the tag field will show even if there is not a type, but this is corrected in 3.1.4.  &lt;br /&gt;
  &lt;br /&gt;
=====Notes=====&lt;br /&gt;
&lt;br /&gt;
* The table name for the common table is &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt;; this is INCORRECT in 3.1 and 3.1.1 data but is not currently used. The data was updated in 3.1.4.&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt; field would potentially be used for display although this is not implemented currently except in the contenttype field.  Usually it should begin with an upper case letter if it is in English. See note about how to make this translatable. To make your type names translatable add &amp;lt;tt&amp;gt;COM_TAGS_CONTENT_TYPE_+type_title=&amp;quot;Type Title&amp;quot;&amp;lt;/tt&amp;gt; in both the ini and sys.ini files. &lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
Rules is currently not used. It will likely be removed in favor of an asset_id for each type, but currently you can ignore this field which will be managed by JTable. &lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
This entry maps the names of specific fields in your table to a set of standard names. This mapping is stored as a JSON array with the first element mapping to the common fields and the second one mapping the other fields from the table. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important note&#039;&#039;&#039;  The JHelperTags and JUcm APIS at 3.1.1 supported arrays for this field, but as of  3.1.4 either arrays or objects with a default of objects are supported.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&#039;{&amp;quot;common&amp;quot;:[{&amp;quot;core_content_item_id&amp;quot;:&amp;quot;id&amp;quot;,&amp;quot;core_title&amp;quot;:&amp;quot;title&amp;quot;,&amp;quot;core_state&amp;quot;:&amp;quot;published&amp;quot;,&amp;quot;core_alias&amp;quot;:&amp;quot;alias&amp;quot;,&amp;quot;core_created_time&amp;quot;:&amp;quot;created_time&amp;quot;,&amp;quot;core_modified_time&amp;quot;:&amp;quot;modified_time&amp;quot;,&amp;quot;core_body&amp;quot;:&amp;quot;description&amp;quot;, &amp;quot;core_hits&amp;quot;:&amp;quot;hits&amp;quot;,&amp;quot;core_publish_up&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_publish_down&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_access&amp;quot;:&amp;quot;access&amp;quot;, &amp;quot;core_params&amp;quot;:&amp;quot;params&amp;quot;, &amp;quot;core_featured&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metadata&amp;quot;:&amp;quot;metadata&amp;quot;, &amp;quot;core_language&amp;quot;:&amp;quot;language&amp;quot;, &amp;quot;core_images&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_urls&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_version&amp;quot;:&amp;quot;version&amp;quot;, &amp;quot;core_ordering&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metakey&amp;quot;:&amp;quot;metakey&amp;quot;, &amp;quot;core_metadesc&amp;quot;:&amp;quot;metadesc&amp;quot;, &amp;quot;core_catid&amp;quot;:&amp;quot;parent_id&amp;quot;, &amp;quot;core_xreference&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;asset_id&amp;quot;:&amp;quot;asset_id&amp;quot;}], &amp;quot;special&amp;quot;: [{&amp;quot;parent_id&amp;quot;:&amp;quot;parent_id&amp;quot;,&amp;quot;lft&amp;quot;:&amp;quot;lft&amp;quot;,&amp;quot;rgt&amp;quot;:&amp;quot;rgt&amp;quot;,&amp;quot;level&amp;quot;:&amp;quot;level&amp;quot;,&amp;quot;path&amp;quot;:&amp;quot;path&amp;quot;,&amp;quot;extension&amp;quot;:&amp;quot;extension&amp;quot;,&amp;quot;note&amp;quot;:&amp;quot;note&amp;quot;}]}&#039;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is the field mapping for the Article type. Note that article uses the name attribs for the core field called params.  If your table does not contain a field, put “null” instead.  Leaving it blank may cause SQL issues. The special fields are optional.  At a minimum for common fields you need to map: content_item_id, alias and title in order to successfully create urls in the tagged items list.  You also will probably want: access, status, and language.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Router&#039;&#039;&#039; is an optional entry to include the name of a static helper router method for this type found in its front end helpers folder. &lt;br /&gt;
&lt;br /&gt;
If you only store data in &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt; you will eventually be able to leave the router field blank although this option is not currently implemented. If you do not have a custom router tags falls back to the rules found in &amp;lt;tt&amp;gt;JHelperRoute&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Modify your component&#039;s table class (or classes if you have multiple tables) ==&lt;br /&gt;
&lt;br /&gt;
Add the following to your JTable constructor:&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;_observers = new JObserverUpdater($this);&lt;br /&gt;
JObserverMapper::attachAllObservers($this);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The addition material formerly located here is no longer needed after 3.1.4&#039;&#039;&#039; Please read the document history for this page if you are required to support 3.1.0 or 3.1.1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;For joomla 3.2 I needed this line:&#039;&#039;&#039; ( Dont forget to replace weblinks by your component name )&lt;br /&gt;
&lt;br /&gt;
JObserverMapper::addObserverClassToClass(&#039;JTableObserverTags&#039;, &#039;WeblinksTableWeblink&#039;, array(&#039;typeAlias&#039; =&amp;gt; &#039;com_weblinks.weblink&#039;));&lt;br /&gt;
&lt;br /&gt;
== Add tags to the getItem() method of the model ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: this was only required in 3.1.0 and 3.1.1. It should not be used in 3.1.4 or later.&#039;&#039;&#039; Please read the history of this page if you need instructions for older versions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add a tag field to edit screens ==&lt;br /&gt;
&lt;br /&gt;
In any edit layouts where you want to allow tagging, you need to add the field to the xml and the appropriate layouts if necessary. The core layouts us a JLayout to manage this and the same layout by any extension.&lt;br /&gt;
&lt;br /&gt;
Update: Note that in &#039;&#039;&#039;3.1.1 only&#039;&#039;&#039; there is special handling of this in the core.  Tags in the edit screen &#039;&#039;&#039;MUST&#039;&#039;&#039; be part of a metadata &amp;lt;fields&amp;gt;&amp;lt;/fields&amp;gt; group.  The core provides two JLayouts to help you manage standard layouts, one (details) for the metadata and one for the sidebar that includes the tabs. In update an extension to 3.1.4 you may need to fix adjust your edit views.&lt;br /&gt;
&lt;br /&gt;
In 3.1.4 or later this special handling is not necessary.  Best practice is to use the standard JLayouts since this provides a consistent experience for users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field loads all the Javascript libraries required. You don&#039;t need to worry about that.&lt;br /&gt;
&lt;br /&gt;
The field supports two modes:  &lt;br /&gt;
* &#039;&#039;&#039;Nested tags mode.&#039;&#039;&#039; Hierarchical tag list. Doesn&#039;t support on the fly tag creation.&lt;br /&gt;
* &#039;&#039;&#039;AJAX mode.&#039;&#039;&#039;  Tags are searched while user types (3 min. chars required to launch the AJAX search). Custom tags are added by pressing ENTER or COMMA keys. Tags show the global route/path. Example: &amp;lt;code&amp;gt;grandpa/parent/tag&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field mode can be forced or use the com_tags setting &amp;lt;code&amp;gt;Tag field mode&amp;lt;/code&amp;gt; to determine its mode. To set/force the field mode we have to add &#039;&#039;&#039;mode=&amp;quot;ajax&amp;quot;&#039;&#039;&#039; or &#039;&#039;&#039;mode=&amp;quot;nested&amp;quot;&#039;&#039;&#039; to the tag field definition.  &lt;br /&gt;
&lt;br /&gt;
Example of forced AJAX mode:  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The field also includes an attribute to allow/deny the user to enter custom values. Currently this only works in AJAX mode. The attribute has to be added to the field definition like **custom=&amp;quot;allow&amp;quot;** or **custom=&amp;quot;deny&amp;quot;**  &lt;br /&gt;
&lt;br /&gt;
Example field definition with custom tags denied:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot; custom=&amp;quot;denied&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
if it is not already there. Usually multiple should be true unless you have a specific reason for it not to be.&lt;br /&gt;
In the core components in administrator, the field is in the group shown on the right, below the language field.&lt;br /&gt;
&lt;br /&gt;
Note: As of 3.1.2 if you wish to use the field to designate parent tags you must add parent=&amp;quot;parent&amp;quot; to the xml definition of the field.&lt;br /&gt;
&lt;br /&gt;
== Prepare the view ==&lt;br /&gt;
&lt;br /&gt;
Add an appropriate version of this to your view.html.php file before loading the layout:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$item-&amp;gt;tags = new JHelperTags;&lt;br /&gt;
$item-&amp;gt;tags-&amp;gt;getItemTags(&#039;com_newsfeeds.newsfeed.&#039; , $this-&amp;gt;item-&amp;gt;id); &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter should match a type in the types table. The second parameter is the primary key under which this record is stored in your table. This would be used in any display in any view where you want the tags associated with the item to display.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Set up the display ==&lt;br /&gt;
&lt;br /&gt;
In any layout where you want to display the tags associated with an item add:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Changing the object and property names as appropriate.&lt;br /&gt;
&lt;br /&gt;
You will most likely want to add the show_tags parameter to the item parameters, the menu item parameters and component configuration as appropriate for your use case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;?php if ($this-&amp;gt;params-&amp;gt;get(&#039;show_tags&#039;, 1) &amp;amp;&amp;amp; !empty($this-&amp;gt;item-&amp;gt;tags)) : ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
	&amp;lt;?php endif; ?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Batch processing ==&lt;br /&gt;
If you want to add the ability to do batch tagging to a backend list view do the following.&lt;br /&gt;
&lt;br /&gt;
Add tag to the default_batch layout&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
        &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&lt;br /&gt;
				&amp;lt;?php echo JHtml::_(&#039;batch.tag&#039;);?&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;/div&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add tag to the button class in the modal footer (Not totally necessary at this point, but good for potential future changes e.g. if unTag is added.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;button class=&amp;quot;btn&amp;quot; type=&amp;quot;button&amp;quot; onclick=&amp;quot;document.id(&#039;batch-category-id&#039;).value=&#039;&#039;;document.id(&#039;batch-access&#039;).value=&#039;&#039;;document.id(&#039;batch-language-id&#039;).value=&#039;&#039;;document.id(&#039;batch-user-id&#039;).value=&#039;&#039;;document.id(&#039;batch-tag-id)&#039;).value=&#039;&#039;&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a batch method to your model if you are not extending JModelAdmin or overriding the batch method. ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Batch tag a list of item.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @param   integer  $value     The value of the new tag.&lt;br /&gt;
	 * @param   array    $pks       An array of row IDs.&lt;br /&gt;
	 * @param   array    $contexts  An array of item contexts.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @return  void.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @since   3.1&lt;br /&gt;
	 */&lt;br /&gt;
	protected function batchTag($value, $pks, $contexts)&lt;br /&gt;
	{&lt;br /&gt;
		$tagsHelper = new JHelperTags();&lt;br /&gt;
		$tagsHelper-&amp;gt;tagItems($value, $pks, $contexts);&lt;br /&gt;
&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And modify your batch method by adding&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		if (!empty($commands[&#039;tag&#039;]))&lt;br /&gt;
		{&lt;br /&gt;
			if (!$this-&amp;gt;batchTag($commands[&#039;tag&#039;], $pks, $contexts))&lt;br /&gt;
			{&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			$done = true;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pay attention to any JSON encoded strings that you need special handling for in batch processing -- remember that you are saving a copy of the core fields and you need save to work as expected.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s it, now create some tags, tag some items and you are set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]][[Category:Content Tags]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117736</id>
		<title>J3.x:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117736"/>
		<updated>2014-04-26T15:33:00Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* type_id */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{copyedit}}&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Joomla&#039;s tagging system is used in all core content extensions and  is designed to be be easy to integrate into other extensions that use standard Joomla design patterns. &lt;br /&gt;
&lt;br /&gt;
Using tags in an extension is fairly straightforward. It requires these changes to a typical extension:&lt;br /&gt;
&lt;br /&gt;
* Register a &#039;Content type&#039; for the extension view(s)&lt;br /&gt;
* Add &#039;Observer methods&#039; to the extension table class(es)&lt;br /&gt;
* Add &#039;Tag fields&#039; to the extension edit forms&lt;br /&gt;
* Add &#039;Tag display&#039; code to the extension view files &lt;br /&gt;
* Optionally, add a batch tagging method to the extension administration&lt;br /&gt;
&lt;br /&gt;
== Register a content type for each view ==&lt;br /&gt;
&lt;br /&gt;
In Joomla! 3 content types e.g. articles, weblinks, contacts, . . . are registered in the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table with a separate record for each content view e.g. article, weblink, article category, weblink category, . . .&lt;br /&gt;
&lt;br /&gt;
Your extension installer needs to create a new record for each view.&lt;br /&gt;
&lt;br /&gt;
You can create records in the installer in three different ways:&lt;br /&gt;
* in the [[J2.5:Managing_Component_Updates_(Script.php)#postflight|postflight]] method&lt;br /&gt;
* by creating a &amp;lt;tt&amp;gt;JTableContenttype&amp;lt;/tt&amp;gt; instance and adding a row, or&lt;br /&gt;
* by using SQL to create a record directly&lt;br /&gt;
&lt;br /&gt;
The structure of the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table is fairly straightforward:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Column name !! Column type !! Purpose &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_id&amp;lt;/tt&amp;gt; || int(10) || Record key&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt;|| varchar(255)|| Type title e.g. &amp;lt;tt&amp;gt;Article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_alias&amp;lt;/tt&amp;gt; || varchar(255)|| Type alias e.g. &amp;lt;tt&amp;gt;com_content.article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt; || varchar(255)|| Information about the Table class&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt; || text || Not currently used&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt; || text || Maps the table column names to standard Joomla! names &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt; || varchar(255)|| Optional: name of a router method&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Construct the Content Type entry===&lt;br /&gt;
&lt;br /&gt;
For each table/option/view you will need to make an entry in &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table (see above).  &lt;br /&gt;
&lt;br /&gt;
====type_id====&lt;br /&gt;
This is the auto-incremented record key, nothing to be done here.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
The title for your Content Type e.g. Article, Contact, Weblink,  &lt;br /&gt;
You can do this either using sql or postflight by creating an instance of JTableContenttype. Don&#039;t forget a category type if you use the Joomla categories API.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;type_alias&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
A string identifying the component and view (that would be in the page request, typically matching the model name) e.g. &amp;lt;tt&amp;gt;com_contact.contact&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;com_weblinks.weblink&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
You need to identify which tables used by your component contain records to be tagged. For example the &#039;Contact&#039; view of the Contacts component uses &amp;lt;tt&amp;gt;#__contact_details&amp;lt;/tt&amp;gt; and similarly the &#039;Weblinks&#039; view of the Weblinks component uses &amp;lt;tt&amp;gt;#__weblinks &amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Each of these components uses a class to write to the table: &amp;lt;tt&amp;gt;ContactTableContact&amp;lt;/tt&amp;gt; for the Contacts component and &amp;lt;tt&amp;gt;WeblinksTableWeblink&amp;lt;/tt&amp;gt; for the Weblinks component. You will need the class prefixes e.g. &amp;lt;tt&amp;gt;ContactTable&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;WeblinksTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that Category types all use the &amp;lt;tt&amp;gt;#__categories&amp;lt;/tt&amp;gt; table.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;type_table&amp;lt;/tt&amp;gt; entry gives the complete table class information for the table class as a JSON object with two elements. The first element represents your &amp;quot;special&amp;quot; table and the second an optional common table (otherwise it will default the &amp;lt;tt&amp;gt;JTableCorecontent&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Here is the JSON entry for the Weblink content type:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__weblinks&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Weblink&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;WeblinksTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__ucm_content&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;ucm_id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Corecontent&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;JTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
The values to be included are:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Entry name !! Content &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;dbtable&amp;lt;/tt&amp;gt; || Table name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;key&amp;lt;/tt&amp;gt;|| Primary key name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type&amp;lt;/tt&amp;gt; || Content type&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;prefix&amp;lt;/tt&amp;gt; || Class prefix&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt; || An option array, as used in your component constructor and getInstance() methods, may be empty&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This information enables the tagging system (and other APIs) to access your table easily. &lt;br /&gt;
&lt;br /&gt;
If you are using Joomla categories make sure to create a category type so that they can be tagged. In Joomla 3.1 and 3.1.1 there is an error where the tag field will show even if there is not a type, but this is corrected in 3.1.4.  &lt;br /&gt;
  &lt;br /&gt;
=====Notes=====&lt;br /&gt;
&lt;br /&gt;
* The table name for the common table is &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt;; this is INCORRECT in 3.1 and 3.1.1 data but is not currently used. The data was updated in 3.1.4.&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt; field would potentially be used for display although this is not implemented currently except in the contenttype field.  Usually it should begin with an upper case letter if it is in English. See note about how to make this translatable. To make your type names translatable add &amp;lt;tt&amp;gt;COM_TAGS_CONTENT_TYPE_+type_title=&amp;quot;Type Title&amp;quot;&amp;lt;/tt&amp;gt; in both the ini and sys.ini files. &lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
Rules is currently not used. It will likely be removed in favor of an asset_id for each type, but currently you can ignore this field which will be managed by JTable. &lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
This entry maps the names of specific fields in your table to a set of standard names. This mapping is stored as a JSON array with the first element mapping to the common fields and the second one mapping the other fields from the table. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important note&#039;&#039;&#039;  The JHelperTags and JUcm APIS at 3.1.1 supported arrays for this field, but as of  3.1.4 either arrays or objects with a default of objects are supported.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&#039;{&amp;quot;common&amp;quot;:[{&amp;quot;core_content_item_id&amp;quot;:&amp;quot;id&amp;quot;,&amp;quot;core_title&amp;quot;:&amp;quot;title&amp;quot;,&amp;quot;core_state&amp;quot;:&amp;quot;published&amp;quot;,&amp;quot;core_alias&amp;quot;:&amp;quot;alias&amp;quot;,&amp;quot;core_created_time&amp;quot;:&amp;quot;created_time&amp;quot;,&amp;quot;core_modified_time&amp;quot;:&amp;quot;modified_time&amp;quot;,&amp;quot;core_body&amp;quot;:&amp;quot;description&amp;quot;, &amp;quot;core_hits&amp;quot;:&amp;quot;hits&amp;quot;,&amp;quot;core_publish_up&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_publish_down&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_access&amp;quot;:&amp;quot;access&amp;quot;, &amp;quot;core_params&amp;quot;:&amp;quot;params&amp;quot;, &amp;quot;core_featured&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metadata&amp;quot;:&amp;quot;metadata&amp;quot;, &amp;quot;core_language&amp;quot;:&amp;quot;language&amp;quot;, &amp;quot;core_images&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_urls&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_version&amp;quot;:&amp;quot;version&amp;quot;, &amp;quot;core_ordering&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metakey&amp;quot;:&amp;quot;metakey&amp;quot;, &amp;quot;core_metadesc&amp;quot;:&amp;quot;metadesc&amp;quot;, &amp;quot;core_catid&amp;quot;:&amp;quot;parent_id&amp;quot;, &amp;quot;core_xreference&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;asset_id&amp;quot;:&amp;quot;asset_id&amp;quot;}], &amp;quot;special&amp;quot;: [{&amp;quot;parent_id&amp;quot;:&amp;quot;parent_id&amp;quot;,&amp;quot;lft&amp;quot;:&amp;quot;lft&amp;quot;,&amp;quot;rgt&amp;quot;:&amp;quot;rgt&amp;quot;,&amp;quot;level&amp;quot;:&amp;quot;level&amp;quot;,&amp;quot;path&amp;quot;:&amp;quot;path&amp;quot;,&amp;quot;extension&amp;quot;:&amp;quot;extension&amp;quot;,&amp;quot;note&amp;quot;:&amp;quot;note&amp;quot;}]}&#039;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is the field mapping for the Article type. Note that article uses the name attribs for the core field called params.  If your table does not contain a field, put “null” instead.  Leaving it blank may cause SQL issues. The special fields are optional.  At a minimum for common fields you need to map: content_item_id, alias and title in order to successfully create urls in the tagged items list.  You also will probably want: access, status, and language.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Router&#039;&#039;&#039; is an optional entry to include the name of a static helper router method for this type found in its front end helpers folder. &lt;br /&gt;
&lt;br /&gt;
If you only store data in &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt; you will eventually be able to leave the router field blank although this option is not currently implemented. If you do not have a custom router tags falls back to the rules found in &amp;lt;tt&amp;gt;JHelperRoute&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Modify your component&#039;s table class (or classes if you have multiple tables) ==&lt;br /&gt;
&lt;br /&gt;
Add the following to your JTable constructor:&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;_observers = new JObserverUpdater($this);&lt;br /&gt;
JObserverMapper::attachAllObservers($this);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The addition material formerly located here is no longer needed after 3.1.4&#039;&#039;&#039; Please read the document history for this page if you are required to support 3.1.0 or 3.1.1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;For joomla 3.2 I needed this line:&#039;&#039;&#039; ( Dont forget to replace weblinks by your component name )&lt;br /&gt;
&lt;br /&gt;
JObserverMapper::addObserverClassToClass(&#039;JTableObserverTags&#039;, &#039;WeblinksTableWeblink&#039;, array(&#039;typeAlias&#039; =&amp;gt; &#039;com_weblinks.weblink&#039;));&lt;br /&gt;
&lt;br /&gt;
== Add tags to the getItem() method of the model ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: this was only required in 3.1.0 and 3.1.1. It should not be used in 3.1.4 or later.&#039;&#039;&#039; Please read the history of this page if you need instructions for older versions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add a tag field to edit screens ==&lt;br /&gt;
&lt;br /&gt;
In any edit layouts where you want to allow tagging, you need to add the field to the xml and the appropriate layouts if necessary. The core layouts us a JLayout to manage this and the same layout by any extension.&lt;br /&gt;
&lt;br /&gt;
Update: Note that in &#039;&#039;&#039;3.1.1 only&#039;&#039;&#039; there is special handling of this in the core.  Tags in the edit screen &#039;&#039;&#039;MUST&#039;&#039;&#039; be part of a metadata &amp;lt;fields&amp;gt;&amp;lt;/fields&amp;gt; group.  The core provides two JLayouts to help you manage standard layouts, one (details) for the metadata and one for the sidebar that includes the tabs. In update an extension to 3.1.4 you may need to fix adjust your edit views.&lt;br /&gt;
&lt;br /&gt;
In 3.1.4 or later this special handling is not necessary.  Best practice is to use the standard JLayouts since this provides a consistent experience for users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field loads all the Javascript libraries required. You don&#039;t need to worry about that.&lt;br /&gt;
&lt;br /&gt;
The field supports two modes:  &lt;br /&gt;
* &#039;&#039;&#039;Nested tags mode.&#039;&#039;&#039; Hierarchical tag list. Doesn&#039;t support on the fly tag creation.&lt;br /&gt;
* &#039;&#039;&#039;AJAX mode.&#039;&#039;&#039;  Tags are searched while user types (3 min. chars required to launch the AJAX search). Custom tags are added by pressing ENTER or COMMA keys. Tags show the global route/path. Example: &amp;lt;code&amp;gt;grandpa/parent/tag&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field mode can be forced or use the com_tags setting &amp;lt;code&amp;gt;Tag field mode&amp;lt;/code&amp;gt; to determine its mode. To set/force the field mode we have to add &#039;&#039;&#039;mode=&amp;quot;ajax&amp;quot;&#039;&#039;&#039; or &#039;&#039;&#039;mode=&amp;quot;nested&amp;quot;&#039;&#039;&#039; to the tag field definition.  &lt;br /&gt;
&lt;br /&gt;
Example of forced AJAX mode:  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The field also includes an attribute to allow/deny the user to enter custom values. Currently this only works in AJAX mode. The attribute has to be added to the field definition like **custom=&amp;quot;allow&amp;quot;** or **custom=&amp;quot;deny&amp;quot;**  &lt;br /&gt;
&lt;br /&gt;
Example field definition with custom tags denied:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot; custom=&amp;quot;denied&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
if it is not already there. Usually multiple should be true unless you have a specific reason for it not to be.&lt;br /&gt;
In the core components in administrator, the field is in the group shown on the right, below the language field.&lt;br /&gt;
&lt;br /&gt;
Note: As of 3.1.2 if you wish to use the field to designate parent tags you must add parent=&amp;quot;parent&amp;quot; to the xml definition of the field.&lt;br /&gt;
&lt;br /&gt;
== Prepare the view ==&lt;br /&gt;
&lt;br /&gt;
Add an appropriate version of this to your view.html.php file before loading the layout:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$item-&amp;gt;tags = new JHelperTags;&lt;br /&gt;
$item-&amp;gt;tags-&amp;gt;getItemTags(&#039;com_newsfeeds.newsfeed.&#039; , $this-&amp;gt;item-&amp;gt;id); &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter should match a type in the types table. The second parameter is the primary key under which this record is stored in your table. This would be used in any display in any view where you want the tags associated with the item to display.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Set up the display ==&lt;br /&gt;
&lt;br /&gt;
In any layout where you want to display the tags associated with an item add:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Changing the object and property names as appropriate.&lt;br /&gt;
&lt;br /&gt;
You will most likely want to add the show_tags parameter to the item parameters, the menu item parameters and component configuration as appropriate for your use case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;?php if ($this-&amp;gt;params-&amp;gt;get(&#039;show_tags&#039;, 1) &amp;amp;&amp;amp; !empty($this-&amp;gt;item-&amp;gt;tags)) : ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
	&amp;lt;?php endif; ?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Batch processing ==&lt;br /&gt;
If you want to add the ability to do batch tagging to a backend list view do the following.&lt;br /&gt;
&lt;br /&gt;
Add tag to the default_batch layout&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
        &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&lt;br /&gt;
				&amp;lt;?php echo JHtml::_(&#039;batch.tag&#039;);?&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;/div&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add tag to the button class in the modal footer (Not totally necessary at this point, but good for potential future changes e.g. if unTag is added.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;button class=&amp;quot;btn&amp;quot; type=&amp;quot;button&amp;quot; onclick=&amp;quot;document.id(&#039;batch-category-id&#039;).value=&#039;&#039;;document.id(&#039;batch-access&#039;).value=&#039;&#039;;document.id(&#039;batch-language-id&#039;).value=&#039;&#039;;document.id(&#039;batch-user-id&#039;).value=&#039;&#039;;document.id(&#039;batch-tag-id)&#039;).value=&#039;&#039;&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a batch method to your model if you are not extending JModelAdmin or overriding the batch method. ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Batch tag a list of item.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @param   integer  $value     The value of the new tag.&lt;br /&gt;
	 * @param   array    $pks       An array of row IDs.&lt;br /&gt;
	 * @param   array    $contexts  An array of item contexts.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @return  void.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @since   3.1&lt;br /&gt;
	 */&lt;br /&gt;
	protected function batchTag($value, $pks, $contexts)&lt;br /&gt;
	{&lt;br /&gt;
		$tagsHelper = new JHelperTags();&lt;br /&gt;
		$tagsHelper-&amp;gt;tagItems($value, $pks, $contexts);&lt;br /&gt;
&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And modify your batch method by adding&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		if (!empty($commands[&#039;tag&#039;]))&lt;br /&gt;
		{&lt;br /&gt;
			if (!$this-&amp;gt;batchTag($commands[&#039;tag&#039;], $pks, $contexts))&lt;br /&gt;
			{&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			$done = true;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pay attention to any JSON encoded strings that you need special handling for in batch processing -- remember that you are saving a copy of the core fields and you need save to work as expected.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s it, now create some tags, tag some items and you are set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]][[Category:Content Tags]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117735</id>
		<title>J3.x:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117735"/>
		<updated>2014-04-26T15:31:42Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* Register a content type for each view */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{copyedit}}&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Joomla&#039;s tagging system is used in all core content extensions and  is designed to be be easy to integrate into other extensions that use standard Joomla design patterns. &lt;br /&gt;
&lt;br /&gt;
Using tags in an extension is fairly straightforward. It requires these changes to a typical extension:&lt;br /&gt;
&lt;br /&gt;
* Register a &#039;Content type&#039; for the extension view(s)&lt;br /&gt;
* Add &#039;Observer methods&#039; to the extension table class(es)&lt;br /&gt;
* Add &#039;Tag fields&#039; to the extension edit forms&lt;br /&gt;
* Add &#039;Tag display&#039; code to the extension view files &lt;br /&gt;
* Optionally, add a batch tagging method to the extension administration&lt;br /&gt;
&lt;br /&gt;
== Register a content type for each view ==&lt;br /&gt;
&lt;br /&gt;
In Joomla! 3 content types e.g. articles, weblinks, contacts, . . . are registered in the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table with a separate record for each content view e.g. article, weblink, article category, weblink category, . . .&lt;br /&gt;
&lt;br /&gt;
Your extension installer needs to create a new record for each view.&lt;br /&gt;
&lt;br /&gt;
You can create records in the installer in three different ways:&lt;br /&gt;
* in the [[J2.5:Managing_Component_Updates_(Script.php)#postflight|postflight]] method&lt;br /&gt;
* by creating a &amp;lt;tt&amp;gt;JTableContenttype&amp;lt;/tt&amp;gt; instance and adding a row, or&lt;br /&gt;
* by using SQL to create a record directly&lt;br /&gt;
&lt;br /&gt;
The structure of the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table is fairly straightforward:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Column name !! Column type !! Purpose &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_id&amp;lt;/tt&amp;gt; || int(10) || Record key&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt;|| varchar(255)|| Type title e.g. &amp;lt;tt&amp;gt;Article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_alias&amp;lt;/tt&amp;gt; || varchar(255)|| Type alias e.g. &amp;lt;tt&amp;gt;com_content.article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt; || varchar(255)|| Information about the Table class&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt; || text || Not currently used&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt; || text || Maps the table column names to standard Joomla! names &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt; || varchar(255)|| Optional: name of a router method&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Construct the Content Type entry===&lt;br /&gt;
&lt;br /&gt;
For each table/option/view you will need to make an entry in &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table (see above).  &lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;type_id&amp;lt;/tt&amp;gt;====&lt;br /&gt;
This is the auto-incremented record key, nothing to be done here.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
The title for your Content Type e.g. Article, Contact, Weblink,  &lt;br /&gt;
You can do this either using sql or postflight by creating an instance of JTableContenttype. Don&#039;t forget a category type if you use the Joomla categories API.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;type_alias&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
A string identifying the component and view (that would be in the page request, typically matching the model name) e.g. &amp;lt;tt&amp;gt;com_contact.contact&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;com_weblinks.weblink&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
You need to identify which tables used by your component contain records to be tagged. For example the &#039;Contact&#039; view of the Contacts component uses &amp;lt;tt&amp;gt;#__contact_details&amp;lt;/tt&amp;gt; and similarly the &#039;Weblinks&#039; view of the Weblinks component uses &amp;lt;tt&amp;gt;#__weblinks &amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Each of these components uses a class to write to the table: &amp;lt;tt&amp;gt;ContactTableContact&amp;lt;/tt&amp;gt; for the Contacts component and &amp;lt;tt&amp;gt;WeblinksTableWeblink&amp;lt;/tt&amp;gt; for the Weblinks component. You will need the class prefixes e.g. &amp;lt;tt&amp;gt;ContactTable&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;WeblinksTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that Category types all use the &amp;lt;tt&amp;gt;#__categories&amp;lt;/tt&amp;gt; table.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;type_table&amp;lt;/tt&amp;gt; entry gives the complete table class information for the table class as a JSON object with two elements. The first element represents your &amp;quot;special&amp;quot; table and the second an optional common table (otherwise it will default the &amp;lt;tt&amp;gt;JTableCorecontent&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Here is the JSON entry for the Weblink content type:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__weblinks&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Weblink&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;WeblinksTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__ucm_content&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;ucm_id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Corecontent&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;JTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
The values to be included are:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Entry name !! Content &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;dbtable&amp;lt;/tt&amp;gt; || Table name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;key&amp;lt;/tt&amp;gt;|| Primary key name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type&amp;lt;/tt&amp;gt; || Content type&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;prefix&amp;lt;/tt&amp;gt; || Class prefix&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt; || An option array, as used in your component constructor and getInstance() methods, may be empty&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This information enables the tagging system (and other APIs) to access your table easily. &lt;br /&gt;
&lt;br /&gt;
If you are using Joomla categories make sure to create a category type so that they can be tagged. In Joomla 3.1 and 3.1.1 there is an error where the tag field will show even if there is not a type, but this is corrected in 3.1.4.  &lt;br /&gt;
  &lt;br /&gt;
=====Notes=====&lt;br /&gt;
&lt;br /&gt;
* The table name for the common table is &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt;; this is INCORRECT in 3.1 and 3.1.1 data but is not currently used. The data was updated in 3.1.4.&lt;br /&gt;
&lt;br /&gt;
* The &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt; field would potentially be used for display although this is not implemented currently except in the contenttype field.  Usually it should begin with an upper case letter if it is in English. See note about how to make this translatable. To make your type names translatable add &amp;lt;tt&amp;gt;COM_TAGS_CONTENT_TYPE_+type_title=&amp;quot;Type Title&amp;quot;&amp;lt;/tt&amp;gt; in both the ini and sys.ini files. &lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
Rules is currently not used. It will likely be removed in favor of an asset_id for each type, but currently you can ignore this field which will be managed by JTable. &lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
This entry maps the names of specific fields in your table to a set of standard names. This mapping is stored as a JSON array with the first element mapping to the common fields and the second one mapping the other fields from the table. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important note&#039;&#039;&#039;  The JHelperTags and JUcm APIS at 3.1.1 supported arrays for this field, but as of  3.1.4 either arrays or objects with a default of objects are supported.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&#039;{&amp;quot;common&amp;quot;:[{&amp;quot;core_content_item_id&amp;quot;:&amp;quot;id&amp;quot;,&amp;quot;core_title&amp;quot;:&amp;quot;title&amp;quot;,&amp;quot;core_state&amp;quot;:&amp;quot;published&amp;quot;,&amp;quot;core_alias&amp;quot;:&amp;quot;alias&amp;quot;,&amp;quot;core_created_time&amp;quot;:&amp;quot;created_time&amp;quot;,&amp;quot;core_modified_time&amp;quot;:&amp;quot;modified_time&amp;quot;,&amp;quot;core_body&amp;quot;:&amp;quot;description&amp;quot;, &amp;quot;core_hits&amp;quot;:&amp;quot;hits&amp;quot;,&amp;quot;core_publish_up&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_publish_down&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_access&amp;quot;:&amp;quot;access&amp;quot;, &amp;quot;core_params&amp;quot;:&amp;quot;params&amp;quot;, &amp;quot;core_featured&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metadata&amp;quot;:&amp;quot;metadata&amp;quot;, &amp;quot;core_language&amp;quot;:&amp;quot;language&amp;quot;, &amp;quot;core_images&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_urls&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_version&amp;quot;:&amp;quot;version&amp;quot;, &amp;quot;core_ordering&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metakey&amp;quot;:&amp;quot;metakey&amp;quot;, &amp;quot;core_metadesc&amp;quot;:&amp;quot;metadesc&amp;quot;, &amp;quot;core_catid&amp;quot;:&amp;quot;parent_id&amp;quot;, &amp;quot;core_xreference&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;asset_id&amp;quot;:&amp;quot;asset_id&amp;quot;}], &amp;quot;special&amp;quot;: [{&amp;quot;parent_id&amp;quot;:&amp;quot;parent_id&amp;quot;,&amp;quot;lft&amp;quot;:&amp;quot;lft&amp;quot;,&amp;quot;rgt&amp;quot;:&amp;quot;rgt&amp;quot;,&amp;quot;level&amp;quot;:&amp;quot;level&amp;quot;,&amp;quot;path&amp;quot;:&amp;quot;path&amp;quot;,&amp;quot;extension&amp;quot;:&amp;quot;extension&amp;quot;,&amp;quot;note&amp;quot;:&amp;quot;note&amp;quot;}]}&#039;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is the field mapping for the Article type. Note that article uses the name attribs for the core field called params.  If your table does not contain a field, put “null” instead.  Leaving it blank may cause SQL issues. The special fields are optional.  At a minimum for common fields you need to map: content_item_id, alias and title in order to successfully create urls in the tagged items list.  You also will probably want: access, status, and language.&lt;br /&gt;
&lt;br /&gt;
====&amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt;====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Router&#039;&#039;&#039; is an optional entry to include the name of a static helper router method for this type found in its front end helpers folder. &lt;br /&gt;
&lt;br /&gt;
If you only store data in &amp;lt;tt&amp;gt;#__ucm_content&amp;lt;/tt&amp;gt; you will eventually be able to leave the router field blank although this option is not currently implemented. If you do not have a custom router tags falls back to the rules found in &amp;lt;tt&amp;gt;JHelperRoute&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Modify your component&#039;s table class (or classes if you have multiple tables) ==&lt;br /&gt;
&lt;br /&gt;
Add the following to your JTable constructor:&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;_observers = new JObserverUpdater($this);&lt;br /&gt;
JObserverMapper::attachAllObservers($this);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The addition material formerly located here is no longer needed after 3.1.4&#039;&#039;&#039; Please read the document history for this page if you are required to support 3.1.0 or 3.1.1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;For joomla 3.2 I needed this line:&#039;&#039;&#039; ( Dont forget to replace weblinks by your component name )&lt;br /&gt;
&lt;br /&gt;
JObserverMapper::addObserverClassToClass(&#039;JTableObserverTags&#039;, &#039;WeblinksTableWeblink&#039;, array(&#039;typeAlias&#039; =&amp;gt; &#039;com_weblinks.weblink&#039;));&lt;br /&gt;
&lt;br /&gt;
== Add tags to the getItem() method of the model ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: this was only required in 3.1.0 and 3.1.1. It should not be used in 3.1.4 or later.&#039;&#039;&#039; Please read the history of this page if you need instructions for older versions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add a tag field to edit screens ==&lt;br /&gt;
&lt;br /&gt;
In any edit layouts where you want to allow tagging, you need to add the field to the xml and the appropriate layouts if necessary. The core layouts us a JLayout to manage this and the same layout by any extension.&lt;br /&gt;
&lt;br /&gt;
Update: Note that in &#039;&#039;&#039;3.1.1 only&#039;&#039;&#039; there is special handling of this in the core.  Tags in the edit screen &#039;&#039;&#039;MUST&#039;&#039;&#039; be part of a metadata &amp;lt;fields&amp;gt;&amp;lt;/fields&amp;gt; group.  The core provides two JLayouts to help you manage standard layouts, one (details) for the metadata and one for the sidebar that includes the tabs. In update an extension to 3.1.4 you may need to fix adjust your edit views.&lt;br /&gt;
&lt;br /&gt;
In 3.1.4 or later this special handling is not necessary.  Best practice is to use the standard JLayouts since this provides a consistent experience for users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field loads all the Javascript libraries required. You don&#039;t need to worry about that.&lt;br /&gt;
&lt;br /&gt;
The field supports two modes:  &lt;br /&gt;
* &#039;&#039;&#039;Nested tags mode.&#039;&#039;&#039; Hierarchical tag list. Doesn&#039;t support on the fly tag creation.&lt;br /&gt;
* &#039;&#039;&#039;AJAX mode.&#039;&#039;&#039;  Tags are searched while user types (3 min. chars required to launch the AJAX search). Custom tags are added by pressing ENTER or COMMA keys. Tags show the global route/path. Example: &amp;lt;code&amp;gt;grandpa/parent/tag&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field mode can be forced or use the com_tags setting &amp;lt;code&amp;gt;Tag field mode&amp;lt;/code&amp;gt; to determine its mode. To set/force the field mode we have to add &#039;&#039;&#039;mode=&amp;quot;ajax&amp;quot;&#039;&#039;&#039; or &#039;&#039;&#039;mode=&amp;quot;nested&amp;quot;&#039;&#039;&#039; to the tag field definition.  &lt;br /&gt;
&lt;br /&gt;
Example of forced AJAX mode:  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The field also includes an attribute to allow/deny the user to enter custom values. Currently this only works in AJAX mode. The attribute has to be added to the field definition like **custom=&amp;quot;allow&amp;quot;** or **custom=&amp;quot;deny&amp;quot;**  &lt;br /&gt;
&lt;br /&gt;
Example field definition with custom tags denied:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot; custom=&amp;quot;denied&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
if it is not already there. Usually multiple should be true unless you have a specific reason for it not to be.&lt;br /&gt;
In the core components in administrator, the field is in the group shown on the right, below the language field.&lt;br /&gt;
&lt;br /&gt;
Note: As of 3.1.2 if you wish to use the field to designate parent tags you must add parent=&amp;quot;parent&amp;quot; to the xml definition of the field.&lt;br /&gt;
&lt;br /&gt;
== Prepare the view ==&lt;br /&gt;
&lt;br /&gt;
Add an appropriate version of this to your view.html.php file before loading the layout:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$item-&amp;gt;tags = new JHelperTags;&lt;br /&gt;
$item-&amp;gt;tags-&amp;gt;getItemTags(&#039;com_newsfeeds.newsfeed.&#039; , $this-&amp;gt;item-&amp;gt;id); &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter should match a type in the types table. The second parameter is the primary key under which this record is stored in your table. This would be used in any display in any view where you want the tags associated with the item to display.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Set up the display ==&lt;br /&gt;
&lt;br /&gt;
In any layout where you want to display the tags associated with an item add:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Changing the object and property names as appropriate.&lt;br /&gt;
&lt;br /&gt;
You will most likely want to add the show_tags parameter to the item parameters, the menu item parameters and component configuration as appropriate for your use case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;?php if ($this-&amp;gt;params-&amp;gt;get(&#039;show_tags&#039;, 1) &amp;amp;&amp;amp; !empty($this-&amp;gt;item-&amp;gt;tags)) : ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
	&amp;lt;?php endif; ?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Batch processing ==&lt;br /&gt;
If you want to add the ability to do batch tagging to a backend list view do the following.&lt;br /&gt;
&lt;br /&gt;
Add tag to the default_batch layout&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
        &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&lt;br /&gt;
				&amp;lt;?php echo JHtml::_(&#039;batch.tag&#039;);?&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;/div&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add tag to the button class in the modal footer (Not totally necessary at this point, but good for potential future changes e.g. if unTag is added.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;button class=&amp;quot;btn&amp;quot; type=&amp;quot;button&amp;quot; onclick=&amp;quot;document.id(&#039;batch-category-id&#039;).value=&#039;&#039;;document.id(&#039;batch-access&#039;).value=&#039;&#039;;document.id(&#039;batch-language-id&#039;).value=&#039;&#039;;document.id(&#039;batch-user-id&#039;).value=&#039;&#039;;document.id(&#039;batch-tag-id)&#039;).value=&#039;&#039;&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a batch method to your model if you are not extending JModelAdmin or overriding the batch method. ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Batch tag a list of item.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @param   integer  $value     The value of the new tag.&lt;br /&gt;
	 * @param   array    $pks       An array of row IDs.&lt;br /&gt;
	 * @param   array    $contexts  An array of item contexts.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @return  void.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @since   3.1&lt;br /&gt;
	 */&lt;br /&gt;
	protected function batchTag($value, $pks, $contexts)&lt;br /&gt;
	{&lt;br /&gt;
		$tagsHelper = new JHelperTags();&lt;br /&gt;
		$tagsHelper-&amp;gt;tagItems($value, $pks, $contexts);&lt;br /&gt;
&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And modify your batch method by adding&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		if (!empty($commands[&#039;tag&#039;]))&lt;br /&gt;
		{&lt;br /&gt;
			if (!$this-&amp;gt;batchTag($commands[&#039;tag&#039;], $pks, $contexts))&lt;br /&gt;
			{&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			$done = true;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pay attention to any JSON encoded strings that you need special handling for in batch processing -- remember that you are saving a copy of the core fields and you need save to work as expected.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s it, now create some tags, tag some items and you are set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]][[Category:Content Tags]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117734</id>
		<title>J3.x:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117734"/>
		<updated>2014-04-26T15:13:13Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* Register a content type for each view */ Amplify table section&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{copyedit}}&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Joomla&#039;s tagging system is used in all core content extensions and  is designed to be be easy to integrate into other extensions that use standard Joomla design patterns. &lt;br /&gt;
&lt;br /&gt;
Using tags in an extension is fairly straightforward. It requires these changes to a typical extension:&lt;br /&gt;
&lt;br /&gt;
* Register a &#039;Content type&#039; for the extension view(s)&lt;br /&gt;
* Add &#039;Observer methods&#039; to the extension table class(es)&lt;br /&gt;
* Add &#039;Tag fields&#039; to the extension edit forms&lt;br /&gt;
* Add &#039;Tag display&#039; code to the extension view files &lt;br /&gt;
* Optionally, add a batch tagging method to the extension administration&lt;br /&gt;
&lt;br /&gt;
== Register a content type for each view ==&lt;br /&gt;
&lt;br /&gt;
In Joomla! 3 content types e.g. articles, weblinks, contacts, . . . are registered in the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table with a separate record for each content view e.g. article, weblink, article category, weblink category, . . .&lt;br /&gt;
&lt;br /&gt;
Your extension installer needs to create a new record for each view.&lt;br /&gt;
&lt;br /&gt;
You can create records in the installer in three different ways:&lt;br /&gt;
* in the [[J2.5:Managing_Component_Updates_(Script.php)#postflight|postflight]] method&lt;br /&gt;
* by creating a &amp;lt;tt&amp;gt;JTableContenttype&amp;lt;/tt&amp;gt; instance and adding a row, or&lt;br /&gt;
* by using SQL to create a record directly&lt;br /&gt;
&lt;br /&gt;
The structure of the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table is fairly straightforward:&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
! Column name !! Column type !! Purpose &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_id&amp;lt;/tt&amp;gt; || int(10) || Record key&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt;|| varchar(255)|| Type title e.g. &amp;lt;tt&amp;gt;Article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_alias&amp;lt;/tt&amp;gt; || varchar(255)|| Type alias e.g. &amp;lt;tt&amp;gt;com_content.article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt; || varchar(255)|| Information about the Table class&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt; || text || Not currently used&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt; || text || Maps the table column names to standard Joomla! names &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt; || varchar(255)|| Optional: name of a router method&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
===Construct the Content Type entry===&lt;br /&gt;
&lt;br /&gt;
For each table/option/view you will need to make an entry in &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table (see above).  &lt;br /&gt;
&lt;br /&gt;
====type_id====&lt;br /&gt;
This is the auto-incremented record key, nothing to be done here.&lt;br /&gt;
&lt;br /&gt;
====type_title====&lt;br /&gt;
&lt;br /&gt;
The title for your Content Type e.g. Article, Contact, Weblink,  &lt;br /&gt;
You can do this either using sql or postflight by creating an instance of JTableContenttype. Don&#039;t forget a category type if you use the Joomla categories API.&lt;br /&gt;
&lt;br /&gt;
====type_alias====&lt;br /&gt;
&lt;br /&gt;
A string identifying the component and view (that would be in the page request, typically matching the model name) e.g. &amp;lt;tt&amp;gt;com_contact.contact&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;com_weblinks.weblink&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
====table====&lt;br /&gt;
&lt;br /&gt;
You need to identify which tables used by your component contain records to be tagged. For example the &#039;Contact&#039; view of the Contacts component uses &amp;lt;tt&amp;gt;#__contact_details&amp;lt;/tt&amp;gt; and similarly the &#039;Weblinks&#039; view of the Weblinks component uses &amp;lt;tt&amp;gt;#__weblinks &amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Each of these components uses a class to write to the table: &amp;lt;tt&amp;gt;ContactTableContact&amp;lt;/tt&amp;gt; for the Contacts component and &amp;lt;tt&amp;gt;WeblinksTableWeblink&amp;lt;/tt&amp;gt; for the Weblinks component. You will need the class prefixes e.g. &amp;lt;tt&amp;gt;ContactTable&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;WeblinksTable&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that Category types all use the &amp;lt;tt&amp;gt;#__categories&amp;lt;/tt&amp;gt; table.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;type_table&amp;lt;/tt&amp;gt; entry gives the complete table class information for the table class as a JSON object with two elements. The first element represents your &amp;quot;special&amp;quot; table and the second an optional common table (otherwise it will default the &amp;lt;tt&amp;gt;JTableCorecontent&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Here is the JSON entry for the Weblink content type:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;{&lt;br /&gt;
  &amp;quot;special&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__weblinks&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Weblink&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;WeblinksTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  },&lt;br /&gt;
  &amp;quot;common&amp;quot;: {&lt;br /&gt;
    &amp;quot;dbtable&amp;quot;: &amp;quot;#__ucm_content&amp;quot;,&lt;br /&gt;
    &amp;quot;key&amp;quot;: &amp;quot;ucm_id&amp;quot;,&lt;br /&gt;
    &amp;quot;type&amp;quot;: &amp;quot;Corecontent&amp;quot;,&lt;br /&gt;
    &amp;quot;prefix&amp;quot;: &amp;quot;JTable&amp;quot;,&lt;br /&gt;
    &amp;quot;config&amp;quot;: &amp;quot;array()&amp;quot;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
! Entry name !! Content &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;dbtable&amp;lt;/tt&amp;gt; || Table name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;key&amp;lt;/tt&amp;gt;|| Primary key name&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type&amp;lt;/tt&amp;gt; || Content type&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;prefix&amp;lt;/tt&amp;gt; || Class prefix&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;config&amp;lt;/tt&amp;gt; || An option array, may be empty&lt;br /&gt;
|}&lt;br /&gt;
The option array is as used in your component constructor and getInstance() methods. &lt;br /&gt;
&lt;br /&gt;
This information enables the tagging system (and other APIs) to access your table easily. &lt;br /&gt;
&lt;br /&gt;
If you are using Joomla categories make sure to create a category type so that they can be tagged. In Joomla 3.1 and 3.1.1 there is an error where the tag field will show even if there is not a type, but this is corrected in 3.1.4.  &lt;br /&gt;
  &lt;br /&gt;
&#039;&#039;&#039;Important note&#039;&#039;&#039; : Please note that the table name for the common table is #__ucm_content. This is INCORRECT in 3.1 and 3.1.1 data but is not currently used.  The data was updated in 3.1.4.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;type_title&#039;&#039;&#039; field would potentially be used for display although this is not implemented currently except in the contentttype field.  Usually it should begin with an upper case letter if it is in English. See note about how to make this translatable. &lt;br /&gt;
&lt;br /&gt;
 note:&lt;br /&gt;
 To make your type names translatable add &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
COM_TAGS_CONTENT_TYPE_ + the type_title&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
in both the ini and sys.ini files. &lt;br /&gt;
&lt;br /&gt;
====table====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rules&#039;&#039;&#039;  is currently not used. It will likely be removed in favor of an asset_id for each type, but currently you can ignore this field which will be managed by JTable. &lt;br /&gt;
&lt;br /&gt;
====field_mappings====&lt;br /&gt;
&lt;br /&gt;
 maps the names of specific fields in your table to a set of standard names. This mapping is stored as a JSON array with the first element mapping to the common fields and the second one mapping the other fields from the table. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important note&#039;&#039;&#039;  The JHelperTags and JUcm APIS at 3.1.1 supported arrays for this field, but as of  3.1.4 either arrays or objects with a default of objects are supported.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&#039;{&amp;quot;common&amp;quot;:[{&amp;quot;core_content_item_id&amp;quot;:&amp;quot;id&amp;quot;,&amp;quot;core_title&amp;quot;:&amp;quot;title&amp;quot;,&amp;quot;core_state&amp;quot;:&amp;quot;published&amp;quot;,&amp;quot;core_alias&amp;quot;:&amp;quot;alias&amp;quot;,&amp;quot;core_created_time&amp;quot;:&amp;quot;created_time&amp;quot;,&amp;quot;core_modified_time&amp;quot;:&amp;quot;modified_time&amp;quot;,&amp;quot;core_body&amp;quot;:&amp;quot;description&amp;quot;, &amp;quot;core_hits&amp;quot;:&amp;quot;hits&amp;quot;,&amp;quot;core_publish_up&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_publish_down&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_access&amp;quot;:&amp;quot;access&amp;quot;, &amp;quot;core_params&amp;quot;:&amp;quot;params&amp;quot;, &amp;quot;core_featured&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metadata&amp;quot;:&amp;quot;metadata&amp;quot;, &amp;quot;core_language&amp;quot;:&amp;quot;language&amp;quot;, &amp;quot;core_images&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_urls&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_version&amp;quot;:&amp;quot;version&amp;quot;, &amp;quot;core_ordering&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metakey&amp;quot;:&amp;quot;metakey&amp;quot;, &amp;quot;core_metadesc&amp;quot;:&amp;quot;metadesc&amp;quot;, &amp;quot;core_catid&amp;quot;:&amp;quot;parent_id&amp;quot;, &amp;quot;core_xreference&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;asset_id&amp;quot;:&amp;quot;asset_id&amp;quot;}], &amp;quot;special&amp;quot;: [{&amp;quot;parent_id&amp;quot;:&amp;quot;parent_id&amp;quot;,&amp;quot;lft&amp;quot;:&amp;quot;lft&amp;quot;,&amp;quot;rgt&amp;quot;:&amp;quot;rgt&amp;quot;,&amp;quot;level&amp;quot;:&amp;quot;level&amp;quot;,&amp;quot;path&amp;quot;:&amp;quot;path&amp;quot;,&amp;quot;extension&amp;quot;:&amp;quot;extension&amp;quot;,&amp;quot;note&amp;quot;:&amp;quot;note&amp;quot;}]}&#039;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is the field mapping for the Article type. Note that article uses the name attribs for the core field called params.  If your table does not contain a field, put “null” instead.  Leaving it blank may cause SQL issues. The special fields are optional.  At a minimum for common fields you need to map: content_item_id, alias and title in order to successfully create urls in the tagged items list.  You also will probably want: access, status, and language.&lt;br /&gt;
&lt;br /&gt;
====router====&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Router&#039;&#039;&#039; is an optional name of a static helper router method for this type found in its front end helpers folder. &lt;br /&gt;
If you only store data in #__ucm_content you will eventually be able to leave the router field blank although this option is not currently implemented. If you do not have a custom router tags falls back to the rules found in JHelperRoute.&lt;br /&gt;
&lt;br /&gt;
== Modify your component&#039;s table class (or classes if you have multiple tables) ==&lt;br /&gt;
&lt;br /&gt;
Add the following to your JTable constructor:&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;_observers = new JObserverUpdater($this);&lt;br /&gt;
JObserverMapper::attachAllObservers($this);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The addition material formerly located here is no longer needed after 3.1.4&#039;&#039;&#039; Please read the document history for this page if you are required to support 3.1.0 or 3.1.1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;For joomla 3.2 I needed this line:&#039;&#039;&#039; ( Dont forget to replace weblinks by your component name )&lt;br /&gt;
&lt;br /&gt;
JObserverMapper::addObserverClassToClass(&#039;JTableObserverTags&#039;, &#039;WeblinksTableWeblink&#039;, array(&#039;typeAlias&#039; =&amp;gt; &#039;com_weblinks.weblink&#039;));&lt;br /&gt;
&lt;br /&gt;
== Add tags to the getItem() method of the model ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: this was only required in 3.1.0 and 3.1.1. It should not be used in 3.1.4 or later.&#039;&#039;&#039; Please read the history of this page if you need instructions for older versions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add a tag field to edit screens ==&lt;br /&gt;
&lt;br /&gt;
In any edit layouts where you want to allow tagging, you need to add the field to the xml and the appropriate layouts if necessary. The core layouts us a JLayout to manage this and the same layout by any extension.&lt;br /&gt;
&lt;br /&gt;
Update: Note that in &#039;&#039;&#039;3.1.1 only&#039;&#039;&#039; there is special handling of this in the core.  Tags in the edit screen &#039;&#039;&#039;MUST&#039;&#039;&#039; be part of a metadata &amp;lt;fields&amp;gt;&amp;lt;/fields&amp;gt; group.  The core provides two JLayouts to help you manage standard layouts, one (details) for the metadata and one for the sidebar that includes the tabs. In update an extension to 3.1.4 you may need to fix adjust your edit views.&lt;br /&gt;
&lt;br /&gt;
In 3.1.4 or later this special handling is not necessary.  Best practice is to use the standard JLayouts since this provides a consistent experience for users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field loads all the Javascript libraries required. You don&#039;t need to worry about that.&lt;br /&gt;
&lt;br /&gt;
The field supports two modes:  &lt;br /&gt;
* &#039;&#039;&#039;Nested tags mode.&#039;&#039;&#039; Hierarchical tag list. Doesn&#039;t support on the fly tag creation.&lt;br /&gt;
* &#039;&#039;&#039;AJAX mode.&#039;&#039;&#039;  Tags are searched while user types (3 min. chars required to launch the AJAX search). Custom tags are added by pressing ENTER or COMMA keys. Tags show the global route/path. Example: &amp;lt;code&amp;gt;grandpa/parent/tag&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field mode can be forced or use the com_tags setting &amp;lt;code&amp;gt;Tag field mode&amp;lt;/code&amp;gt; to determine its mode. To set/force the field mode we have to add &#039;&#039;&#039;mode=&amp;quot;ajax&amp;quot;&#039;&#039;&#039; or &#039;&#039;&#039;mode=&amp;quot;nested&amp;quot;&#039;&#039;&#039; to the tag field definition.  &lt;br /&gt;
&lt;br /&gt;
Example of forced AJAX mode:  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The field also includes an attribute to allow/deny the user to enter custom values. Currently this only works in AJAX mode. The attribute has to be added to the field definition like **custom=&amp;quot;allow&amp;quot;** or **custom=&amp;quot;deny&amp;quot;**  &lt;br /&gt;
&lt;br /&gt;
Example field definition with custom tags denied:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot; custom=&amp;quot;denied&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
if it is not already there. Usually multiple should be true unless you have a specific reason for it not to be.&lt;br /&gt;
In the core components in administrator, the field is in the group shown on the right, below the language field.&lt;br /&gt;
&lt;br /&gt;
Note: As of 3.1.2 if you wish to use the field to designate parent tags you must add parent=&amp;quot;parent&amp;quot; to the xml definition of the field.&lt;br /&gt;
&lt;br /&gt;
== Prepare the view ==&lt;br /&gt;
&lt;br /&gt;
Add an appropriate version of this to your view.html.php file before loading the layout:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$item-&amp;gt;tags = new JHelperTags;&lt;br /&gt;
$item-&amp;gt;tags-&amp;gt;getItemTags(&#039;com_newsfeeds.newsfeed.&#039; , $this-&amp;gt;item-&amp;gt;id); &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter should match a type in the types table. The second parameter is the primary key under which this record is stored in your table. This would be used in any display in any view where you want the tags associated with the item to display.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Set up the display ==&lt;br /&gt;
&lt;br /&gt;
In any layout where you want to display the tags associated with an item add:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Changing the object and property names as appropriate.&lt;br /&gt;
&lt;br /&gt;
You will most likely want to add the show_tags parameter to the item parameters, the menu item parameters and component configuration as appropriate for your use case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;?php if ($this-&amp;gt;params-&amp;gt;get(&#039;show_tags&#039;, 1) &amp;amp;&amp;amp; !empty($this-&amp;gt;item-&amp;gt;tags)) : ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
	&amp;lt;?php endif; ?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Batch processing ==&lt;br /&gt;
If you want to add the ability to do batch tagging to a backend list view do the following.&lt;br /&gt;
&lt;br /&gt;
Add tag to the default_batch layout&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
        &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&lt;br /&gt;
				&amp;lt;?php echo JHtml::_(&#039;batch.tag&#039;);?&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;/div&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add tag to the button class in the modal footer (Not totally necessary at this point, but good for potential future changes e.g. if unTag is added.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;button class=&amp;quot;btn&amp;quot; type=&amp;quot;button&amp;quot; onclick=&amp;quot;document.id(&#039;batch-category-id&#039;).value=&#039;&#039;;document.id(&#039;batch-access&#039;).value=&#039;&#039;;document.id(&#039;batch-language-id&#039;).value=&#039;&#039;;document.id(&#039;batch-user-id&#039;).value=&#039;&#039;;document.id(&#039;batch-tag-id)&#039;).value=&#039;&#039;&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a batch method to your model if you are not extending JModelAdmin or overriding the batch method. ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Batch tag a list of item.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @param   integer  $value     The value of the new tag.&lt;br /&gt;
	 * @param   array    $pks       An array of row IDs.&lt;br /&gt;
	 * @param   array    $contexts  An array of item contexts.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @return  void.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @since   3.1&lt;br /&gt;
	 */&lt;br /&gt;
	protected function batchTag($value, $pks, $contexts)&lt;br /&gt;
	{&lt;br /&gt;
		$tagsHelper = new JHelperTags();&lt;br /&gt;
		$tagsHelper-&amp;gt;tagItems($value, $pks, $contexts);&lt;br /&gt;
&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And modify your batch method by adding&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		if (!empty($commands[&#039;tag&#039;]))&lt;br /&gt;
		{&lt;br /&gt;
			if (!$this-&amp;gt;batchTag($commands[&#039;tag&#039;], $pks, $contexts))&lt;br /&gt;
			{&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			$done = true;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pay attention to any JSON encoded strings that you need special handling for in batch processing -- remember that you are saving a copy of the core fields and you need save to work as expected.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s it, now create some tags, tag some items and you are set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]][[Category:Content Tags]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117719</id>
		<title>J3.x:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117719"/>
		<updated>2014-04-24T17:19:14Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* Create a content type for each view */ clarified table struture&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{copyedit}}&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Joomla&#039;s tagging system is used in all core content extensions and  is designed to be be easy to integrate into other extensions that use standard Joomla design patterns. &lt;br /&gt;
&lt;br /&gt;
Using tags in an extension is fairly straightforward. It requires these changes to a typical extension:&lt;br /&gt;
&lt;br /&gt;
* Register a &#039;Content type&#039; for the extension view(s)&lt;br /&gt;
* Add &#039;Observer methods&#039; to the extension table class(es)&lt;br /&gt;
* Add &#039;Tag fields&#039; to the extension edit forms&lt;br /&gt;
* Add &#039;Tag display&#039; code to the extension view files &lt;br /&gt;
* Optionally, add a batch tagging method to the extension administration&lt;br /&gt;
&lt;br /&gt;
== Register a content type for each view ==&lt;br /&gt;
&lt;br /&gt;
In Joomla! 3 content types e.g. articles, weblinks, contacts, . . . are registered in the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table with a separate record for each content view e.g. article, weblink, article category, weblink category, . . .&lt;br /&gt;
&lt;br /&gt;
Your extension installer needs to create a new record for each view.&lt;br /&gt;
&lt;br /&gt;
You can create records in the installer in three different ways:&lt;br /&gt;
* in the [[J2.5:Managing_Component_Updates_(Script.php)#postflight|postflight]] method&lt;br /&gt;
* by creating a &amp;lt;tt&amp;gt;JTableContenttype&amp;lt;/tt&amp;gt; instance and adding a row, or&lt;br /&gt;
* by using SQL to create a record directly&lt;br /&gt;
&lt;br /&gt;
The structure of the &amp;lt;tt&amp;gt;#__content_types&amp;lt;/tt&amp;gt; table is fairly straightforward:&lt;br /&gt;
{|&lt;br /&gt;
|-&lt;br /&gt;
! Column name !! Column type !! Purpose &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_id&amp;lt;/tt&amp;gt; || int(10) || Record key&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_title&amp;lt;/tt&amp;gt;|| varchar(255)|| Type title e.g. &amp;lt;tt&amp;gt;Article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;type_alias&amp;lt;/tt&amp;gt; || varchar(255)|| Type alias e.g. &amp;lt;tt&amp;gt;com_content.article&amp;lt;/tt&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;table&amp;lt;/tt&amp;gt; || varchar(255)|| Information about the Table class&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;rules&amp;lt;/tt&amp;gt; || text || Not currently used&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;field_mappings&amp;lt;/tt&amp;gt; || text || Maps the table column names to standard Joomla! names &lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;tt&amp;gt;router&amp;lt;/tt&amp;gt; || varchar(255)|| Optional: name of a router method&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
First identify which tables contain data that will be tagged and what the name of the single item view displaying each of these tables. For example:&lt;br /&gt;
* #__contact_details  is the table displayed in the view contact of component com_contact with table class ContactTableContact.&lt;br /&gt;
* #__weblinks is the table displayed in the view weblink of com_weblinks so WeblinksTableWeblink. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For each table/option/view you will need to make an entry in #__content_types.  You can do this either using sql or postflight by creating an instance of JTableContenttype. Don&#039;t forget a category type if you use the Joomla categories API.&lt;br /&gt;
&lt;br /&gt;
A string giving component.view (that would be in the page request, typically matching the model name) goes in the **type_alias** field.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;table&#039;&#039;&#039; gives the complete table class information for the table class stored as a JSON object in which the first element represents your &amp;quot;special&amp;quot; table and the second an optional common table (otherwise it will default the JTableCorecontent. This includes the name of the database table, the name of the primary key, the prefix, the name, an option array as used in your constructor and getInstance() methods. This enables the tagging system (and other APIs) to access your table easily. &lt;br /&gt;
&lt;br /&gt;
If you are using Joomla categories make sure to create a category type so that they can be tagged. In Joomla 3.1 and 3.1.1 there is an error where the tag field will show even if there is not a type, but this is corrected in 3.1.4.  &lt;br /&gt;
  &lt;br /&gt;
&#039;&#039;&#039;Important note&#039;&#039;&#039; : Please note that the table name for the common table is #__ucm_content. This is INCORRECT in 3.1 and 3.1.1 data but is not currently used.  The data was updated in 3.1.4.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
{&amp;quot;special&amp;quot;:{&amp;quot;dbtable&amp;quot;:&amp;quot;#__content&amp;quot;,&amp;quot;key&amp;quot;:&amp;quot;id&amp;quot;,&amp;quot;type&amp;quot;:&amp;quot;Content&amp;quot;,&amp;quot;prefix&amp;quot;:&amp;quot;JTable&amp;quot;,&amp;quot;config&amp;quot;:&amp;quot;array()&amp;quot;},&amp;quot;common&amp;quot;:{&amp;quot;dbtable&amp;quot;:&amp;quot;#__ucm_content&amp;quot;,&amp;quot;key&amp;quot;:&amp;quot;ucm_id&amp;quot;,&amp;quot;type&amp;quot;:&amp;quot;Corecontent&amp;quot;,&amp;quot;prefix&amp;quot;:&amp;quot;JTable&amp;quot;,&amp;quot;config&amp;quot;:&amp;quot;array()&amp;quot;}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;type_title&#039;&#039;&#039; field would potentially be used for display although this is not implemented currently except in the contentttype field.  Usually it should begin with an upper case letter if it is in English. See note about how to make this translatable. &lt;br /&gt;
&lt;br /&gt;
 note:&lt;br /&gt;
 To make your type names translatable add &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
COM_TAGS_CONTENT_TYPE_ + the type_title&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
in both the ini and sys.ini files. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rules&#039;&#039;&#039;  is currently not used. It will likely be removed in favor of an asset_id for each type, but currently you can ignore this field which will be managed by JTable. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;field_mappings&#039;&#039;&#039; maps the names of specific fields in your table to a set of standard names. This mapping is stored as a JSON array with the first element mapping to the common fields and the second one mapping the other fields from the table. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important note&#039;&#039;&#039;  The JHelperTags and JUcm APIS at 3.1.1 supported arrays for this field, but as of  3.1.4 either arrays or objects with a default of objects are supported.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&#039;{&amp;quot;common&amp;quot;:[{&amp;quot;core_content_item_id&amp;quot;:&amp;quot;id&amp;quot;,&amp;quot;core_title&amp;quot;:&amp;quot;title&amp;quot;,&amp;quot;core_state&amp;quot;:&amp;quot;published&amp;quot;,&amp;quot;core_alias&amp;quot;:&amp;quot;alias&amp;quot;,&amp;quot;core_created_time&amp;quot;:&amp;quot;created_time&amp;quot;,&amp;quot;core_modified_time&amp;quot;:&amp;quot;modified_time&amp;quot;,&amp;quot;core_body&amp;quot;:&amp;quot;description&amp;quot;, &amp;quot;core_hits&amp;quot;:&amp;quot;hits&amp;quot;,&amp;quot;core_publish_up&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_publish_down&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_access&amp;quot;:&amp;quot;access&amp;quot;, &amp;quot;core_params&amp;quot;:&amp;quot;params&amp;quot;, &amp;quot;core_featured&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metadata&amp;quot;:&amp;quot;metadata&amp;quot;, &amp;quot;core_language&amp;quot;:&amp;quot;language&amp;quot;, &amp;quot;core_images&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_urls&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_version&amp;quot;:&amp;quot;version&amp;quot;, &amp;quot;core_ordering&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metakey&amp;quot;:&amp;quot;metakey&amp;quot;, &amp;quot;core_metadesc&amp;quot;:&amp;quot;metadesc&amp;quot;, &amp;quot;core_catid&amp;quot;:&amp;quot;parent_id&amp;quot;, &amp;quot;core_xreference&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;asset_id&amp;quot;:&amp;quot;asset_id&amp;quot;}], &amp;quot;special&amp;quot;: [{&amp;quot;parent_id&amp;quot;:&amp;quot;parent_id&amp;quot;,&amp;quot;lft&amp;quot;:&amp;quot;lft&amp;quot;,&amp;quot;rgt&amp;quot;:&amp;quot;rgt&amp;quot;,&amp;quot;level&amp;quot;:&amp;quot;level&amp;quot;,&amp;quot;path&amp;quot;:&amp;quot;path&amp;quot;,&amp;quot;extension&amp;quot;:&amp;quot;extension&amp;quot;,&amp;quot;note&amp;quot;:&amp;quot;note&amp;quot;}]}&#039;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is the field mapping for the Article type. Note that article uses the name attribs for the core field called params.  If your table does not contain a field, put “null” instead.  Leaving it blank may cause SQL issues. The special fields are optional.  At a minimum for common fields you need to map: content_item_id, alias and title in order to successfully create urls in the tagged items list.  You also will probably want: access, status, and language.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Router&#039;&#039;&#039; is an optional name of a static helper router method for this type found in its front end helpers folder. &lt;br /&gt;
If you only store data in #__ucm_content you will eventually be able to leave the router field blank although this option is not currently implemented. If you do not have a custom router tags falls back to the rules found in JHelperRoute.&lt;br /&gt;
&lt;br /&gt;
== Modify your component&#039;s table class (or classes if you have multiple tables) ==&lt;br /&gt;
&lt;br /&gt;
Add the following to your JTable constructor:&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;_observers = new JObserverUpdater($this);&lt;br /&gt;
JObserverMapper::attachAllObservers($this);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The addition material formerly located here is no longer needed after 3.1.4&#039;&#039;&#039; Please read the document history for this page if you are required to support 3.1.0 or 3.1.1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;For joomla 3.2 I needed this line:&#039;&#039;&#039; ( Dont forget to replace weblinks by your component name )&lt;br /&gt;
&lt;br /&gt;
JObserverMapper::addObserverClassToClass(&#039;JTableObserverTags&#039;, &#039;WeblinksTableWeblink&#039;, array(&#039;typeAlias&#039; =&amp;gt; &#039;com_weblinks.weblink&#039;));&lt;br /&gt;
&lt;br /&gt;
== Add tags to the getItem() method of the model ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: this was only required in 3.1.0 and 3.1.1. It should not be used in 3.1.4 or later.&#039;&#039;&#039; Please read the history of this page if you need instructions for older versions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add a tag field to edit screens ==&lt;br /&gt;
&lt;br /&gt;
In any edit layouts where you want to allow tagging, you need to add the field to the xml and the appropriate layouts if necessary. The core layouts us a JLayout to manage this and the same layout by any extension.&lt;br /&gt;
&lt;br /&gt;
Update: Note that in &#039;&#039;&#039;3.1.1 only&#039;&#039;&#039; there is special handling of this in the core.  Tags in the edit screen &#039;&#039;&#039;MUST&#039;&#039;&#039; be part of a metadata &amp;lt;fields&amp;gt;&amp;lt;/fields&amp;gt; group.  The core provides two JLayouts to help you manage standard layouts, one (details) for the metadata and one for the sidebar that includes the tabs. In update an extension to 3.1.4 you may need to fix adjust your edit views.&lt;br /&gt;
&lt;br /&gt;
In 3.1.4 or later this special handling is not necessary.  Best practice is to use the standard JLayouts since this provides a consistent experience for users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field loads all the Javascript libraries required. You don&#039;t need to worry about that.&lt;br /&gt;
&lt;br /&gt;
The field supports two modes:  &lt;br /&gt;
* &#039;&#039;&#039;Nested tags mode.&#039;&#039;&#039; Hierarchical tag list. Doesn&#039;t support on the fly tag creation.&lt;br /&gt;
* &#039;&#039;&#039;AJAX mode.&#039;&#039;&#039;  Tags are searched while user types (3 min. chars required to launch the AJAX search). Custom tags are added by pressing ENTER or COMMA keys. Tags show the global route/path. Example: &amp;lt;code&amp;gt;grandpa/parent/tag&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field mode can be forced or use the com_tags setting &amp;lt;code&amp;gt;Tag field mode&amp;lt;/code&amp;gt; to determine its mode. To set/force the field mode we have to add &#039;&#039;&#039;mode=&amp;quot;ajax&amp;quot;&#039;&#039;&#039; or &#039;&#039;&#039;mode=&amp;quot;nested&amp;quot;&#039;&#039;&#039; to the tag field definition.  &lt;br /&gt;
&lt;br /&gt;
Example of forced AJAX mode:  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The field also includes an attribute to allow/deny the user to enter custom values. Currently this only works in AJAX mode. The attribute has to be added to the field definition like **custom=&amp;quot;allow&amp;quot;** or **custom=&amp;quot;deny&amp;quot;**  &lt;br /&gt;
&lt;br /&gt;
Example field definition with custom tags denied:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot; custom=&amp;quot;denied&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
if it is not already there. Usually multiple should be true unless you have a specific reason for it not to be.&lt;br /&gt;
In the core components in administrator, the field is in the group shown on the right, below the language field.&lt;br /&gt;
&lt;br /&gt;
Note: As of 3.1.2 if you wish to use the field to designate parent tags you must add parent=&amp;quot;parent&amp;quot; to the xml definition of the field.&lt;br /&gt;
&lt;br /&gt;
== Prepare the view ==&lt;br /&gt;
&lt;br /&gt;
Add an appropriate version of this to your view.html.php file before loading the layout:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$item-&amp;gt;tags = new JHelperTags;&lt;br /&gt;
$item-&amp;gt;tags-&amp;gt;getItemTags(&#039;com_newsfeeds.newsfeed.&#039; , $this-&amp;gt;item-&amp;gt;id); &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter should match a type in the types table. The second parameter is the primary key under which this record is stored in your table. This would be used in any display in any view where you want the tags associated with the item to display.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Set up the display ==&lt;br /&gt;
&lt;br /&gt;
In any layout where you want to display the tags associated with an item add:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Changing the object and property names as appropriate.&lt;br /&gt;
&lt;br /&gt;
You will most likely want to add the show_tags parameter to the item parameters, the menu item parameters and component configuration as appropriate for your use case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;?php if ($this-&amp;gt;params-&amp;gt;get(&#039;show_tags&#039;, 1) &amp;amp;&amp;amp; !empty($this-&amp;gt;item-&amp;gt;tags)) : ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
	&amp;lt;?php endif; ?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Batch processing ==&lt;br /&gt;
If you want to add the ability to do batch tagging to a backend list view do the following.&lt;br /&gt;
&lt;br /&gt;
Add tag to the default_batch layout&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
        &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&lt;br /&gt;
				&amp;lt;?php echo JHtml::_(&#039;batch.tag&#039;);?&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;/div&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add tag to the button class in the modal footer (Not totally necessary at this point, but good for potential future changes e.g. if unTag is added.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;button class=&amp;quot;btn&amp;quot; type=&amp;quot;button&amp;quot; onclick=&amp;quot;document.id(&#039;batch-category-id&#039;).value=&#039;&#039;;document.id(&#039;batch-access&#039;).value=&#039;&#039;;document.id(&#039;batch-language-id&#039;).value=&#039;&#039;;document.id(&#039;batch-user-id&#039;).value=&#039;&#039;;document.id(&#039;batch-tag-id)&#039;).value=&#039;&#039;&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a batch method to your model if you are not extending JModelAdmin or overriding the batch method. ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Batch tag a list of item.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @param   integer  $value     The value of the new tag.&lt;br /&gt;
	 * @param   array    $pks       An array of row IDs.&lt;br /&gt;
	 * @param   array    $contexts  An array of item contexts.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @return  void.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @since   3.1&lt;br /&gt;
	 */&lt;br /&gt;
	protected function batchTag($value, $pks, $contexts)&lt;br /&gt;
	{&lt;br /&gt;
		$tagsHelper = new JHelperTags();&lt;br /&gt;
		$tagsHelper-&amp;gt;tagItems($value, $pks, $contexts);&lt;br /&gt;
&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And modify your batch method by adding&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		if (!empty($commands[&#039;tag&#039;]))&lt;br /&gt;
		{&lt;br /&gt;
			if (!$this-&amp;gt;batchTag($commands[&#039;tag&#039;], $pks, $contexts))&lt;br /&gt;
			{&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			$done = true;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pay attention to any JSON encoded strings that you need special handling for in batch processing -- remember that you are saving a copy of the core fields and you need save to work as expected.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s it, now create some tags, tag some items and you are set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]][[Category:Content Tags]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117718</id>
		<title>J3.x:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117718"/>
		<updated>2014-04-24T16:56:36Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: Added and edited introduction section&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{copyedit}}&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Joomla&#039;s tagging system is used in all core content extensions and  is designed to be be easy to integrate into other extensions that use standard Joomla design patterns. &lt;br /&gt;
&lt;br /&gt;
Using tags in an extension is fairly straightforward. It requires these changes to a typical extension:&lt;br /&gt;
&lt;br /&gt;
* Register a &#039;Content type&#039; for the extension view(s)&lt;br /&gt;
* Add &#039;Observer methods&#039; to the extension table class(es)&lt;br /&gt;
* Add &#039;Tag fields&#039; to the extension edit forms&lt;br /&gt;
* Add &#039;Tag display&#039; code to the extension view files &lt;br /&gt;
* Optionally, add a batch tagging method to the extension administration&lt;br /&gt;
&lt;br /&gt;
== Create a content type for each view ==&lt;br /&gt;
&lt;br /&gt;
Note: Types can be added using either sql or [[J2.5:Managing_Component_Updates_(Script.php)#postflight|postflight]] by creating a JTableContenttype instance and adding a row.&lt;br /&gt;
&lt;br /&gt;
First identify which tables contain data that will be tagged and what the name of the single item view displaying each of these tables. For example:&lt;br /&gt;
* #__contact_details  is the table displayed in the view contact of component com_contact with table class ContactTableContact.&lt;br /&gt;
* #__weblinks is the table displayed in the view weblink of com_weblinks so WeblinksTableWeblink. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
 CREATE TABLE IF NOT EXISTS `#__content_types` (&lt;br /&gt;
  `type_id` int(10) unsigned NOT NULL AUTO_INCREMENT,&lt;br /&gt;
  `type_title` varchar(255) NOT NULL DEFAULT &#039;&#039;,&lt;br /&gt;
  `type_alias` varchar(255) NOT NULL DEFAULT &#039;&#039;,&lt;br /&gt;
  `table` varchar(255) NOT NULL DEFAULT &#039;&#039;,&lt;br /&gt;
  `rules` text NOT NULL,&lt;br /&gt;
  `field_mappings` text NOT NULL,&lt;br /&gt;
  `router` varchar(255) NOT NULL DEFAULT &#039;&#039;,&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For each table/option/view you will need to make an entry in #__content_types.  You can do this either using sql or postflight by creating an instance of JTableContenttype. Don&#039;t forget a category type if you use the Joomla categories API.&lt;br /&gt;
&lt;br /&gt;
A string giving component.view (that would be in the page request, typically matching the model name) goes in the **type_alias** field.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;table&#039;&#039;&#039; gives the complete table class information for the table class stored as a JSON object in which the first element represents your &amp;quot;special&amp;quot; table and the second an optional common table (otherwise it will default the JTableCorecontent. This includes the name of the database table, the name of the primary key, the prefix, the name, an option array as used in your constructor and getInstance() methods. This enables the tagging system (and other APIs) to access your table easily. &lt;br /&gt;
&lt;br /&gt;
If you are using Joomla categories make sure to create a category type so that they can be tagged. In Joomla 3.1 and 3.1.1 there is an error where the tag field will show even if there is not a type, but this is corrected in 3.1.4.  &lt;br /&gt;
  &lt;br /&gt;
&#039;&#039;&#039;Important note&#039;&#039;&#039; : Please note that the table name for the common table is #__ucm_content. This is INCORRECT in 3.1 and 3.1.1 data but is not currently used.  The data was updated in 3.1.4.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
{&amp;quot;special&amp;quot;:{&amp;quot;dbtable&amp;quot;:&amp;quot;#__content&amp;quot;,&amp;quot;key&amp;quot;:&amp;quot;id&amp;quot;,&amp;quot;type&amp;quot;:&amp;quot;Content&amp;quot;,&amp;quot;prefix&amp;quot;:&amp;quot;JTable&amp;quot;,&amp;quot;config&amp;quot;:&amp;quot;array()&amp;quot;},&amp;quot;common&amp;quot;:{&amp;quot;dbtable&amp;quot;:&amp;quot;#__ucm_content&amp;quot;,&amp;quot;key&amp;quot;:&amp;quot;ucm_id&amp;quot;,&amp;quot;type&amp;quot;:&amp;quot;Corecontent&amp;quot;,&amp;quot;prefix&amp;quot;:&amp;quot;JTable&amp;quot;,&amp;quot;config&amp;quot;:&amp;quot;array()&amp;quot;}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;type_title&#039;&#039;&#039; field would potentially be used for display although this is not implemented currently except in the contentttype field.  Usually it should begin with an upper case letter if it is in English. See note about how to make this translatable. &lt;br /&gt;
&lt;br /&gt;
 note:&lt;br /&gt;
 To make your type names translatable add &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
COM_TAGS_CONTENT_TYPE_ + the type_title&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
in both the ini and sys.ini files. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rules&#039;&#039;&#039;  is currently not used. It will likely be removed in favor of an asset_id for each type, but currently you can ignore this field which will be managed by JTable. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;field_mappings&#039;&#039;&#039; maps the names of specific fields in your table to a set of standard names. This mapping is stored as a JSON array with the first element mapping to the common fields and the second one mapping the other fields from the table. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important note&#039;&#039;&#039;  The JHelperTags and JUcm APIS at 3.1.1 supported arrays for this field, but as of  3.1.4 either arrays or objects with a default of objects are supported.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&#039;{&amp;quot;common&amp;quot;:[{&amp;quot;core_content_item_id&amp;quot;:&amp;quot;id&amp;quot;,&amp;quot;core_title&amp;quot;:&amp;quot;title&amp;quot;,&amp;quot;core_state&amp;quot;:&amp;quot;published&amp;quot;,&amp;quot;core_alias&amp;quot;:&amp;quot;alias&amp;quot;,&amp;quot;core_created_time&amp;quot;:&amp;quot;created_time&amp;quot;,&amp;quot;core_modified_time&amp;quot;:&amp;quot;modified_time&amp;quot;,&amp;quot;core_body&amp;quot;:&amp;quot;description&amp;quot;, &amp;quot;core_hits&amp;quot;:&amp;quot;hits&amp;quot;,&amp;quot;core_publish_up&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_publish_down&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_access&amp;quot;:&amp;quot;access&amp;quot;, &amp;quot;core_params&amp;quot;:&amp;quot;params&amp;quot;, &amp;quot;core_featured&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metadata&amp;quot;:&amp;quot;metadata&amp;quot;, &amp;quot;core_language&amp;quot;:&amp;quot;language&amp;quot;, &amp;quot;core_images&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_urls&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_version&amp;quot;:&amp;quot;version&amp;quot;, &amp;quot;core_ordering&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metakey&amp;quot;:&amp;quot;metakey&amp;quot;, &amp;quot;core_metadesc&amp;quot;:&amp;quot;metadesc&amp;quot;, &amp;quot;core_catid&amp;quot;:&amp;quot;parent_id&amp;quot;, &amp;quot;core_xreference&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;asset_id&amp;quot;:&amp;quot;asset_id&amp;quot;}], &amp;quot;special&amp;quot;: [{&amp;quot;parent_id&amp;quot;:&amp;quot;parent_id&amp;quot;,&amp;quot;lft&amp;quot;:&amp;quot;lft&amp;quot;,&amp;quot;rgt&amp;quot;:&amp;quot;rgt&amp;quot;,&amp;quot;level&amp;quot;:&amp;quot;level&amp;quot;,&amp;quot;path&amp;quot;:&amp;quot;path&amp;quot;,&amp;quot;extension&amp;quot;:&amp;quot;extension&amp;quot;,&amp;quot;note&amp;quot;:&amp;quot;note&amp;quot;}]}&#039;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is the field mapping for the Article type. Note that article uses the name attribs for the core field called params.  If your table does not contain a field, put “null” instead.  Leaving it blank may cause SQL issues. The special fields are optional.  At a minimum for common fields you need to map: content_item_id, alias and title in order to successfully create urls in the tagged items list.  You also will probably want: access, status, and language.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Router&#039;&#039;&#039; is an optional name of a static helper router method for this type found in its front end helpers folder. &lt;br /&gt;
If you only store data in #__ucm_content you will eventually be able to leave the router field blank although this option is not currently implemented. If you do not have a custom router tags falls back to the rules found in JHelperRoute.&lt;br /&gt;
&lt;br /&gt;
== Modify your component&#039;s table class (or classes if you have multiple tables) ==&lt;br /&gt;
&lt;br /&gt;
Add the following to your JTable constructor:&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;_observers = new JObserverUpdater($this);&lt;br /&gt;
JObserverMapper::attachAllObservers($this);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The addition material formerly located here is no longer needed after 3.1.4&#039;&#039;&#039; Please read the document history for this page if you are required to support 3.1.0 or 3.1.1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;For joomla 3.2 I needed this line:&#039;&#039;&#039; ( Dont forget to replace weblinks by your component name )&lt;br /&gt;
&lt;br /&gt;
JObserverMapper::addObserverClassToClass(&#039;JTableObserverTags&#039;, &#039;WeblinksTableWeblink&#039;, array(&#039;typeAlias&#039; =&amp;gt; &#039;com_weblinks.weblink&#039;));&lt;br /&gt;
&lt;br /&gt;
== Add tags to the getItem() method of the model ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: this was only required in 3.1.0 and 3.1.1. It should not be used in 3.1.4 or later.&#039;&#039;&#039; Please read the history of this page if you need instructions for older versions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add a tag field to edit screens ==&lt;br /&gt;
&lt;br /&gt;
In any edit layouts where you want to allow tagging, you need to add the field to the xml and the appropriate layouts if necessary. The core layouts us a JLayout to manage this and the same layout by any extension.&lt;br /&gt;
&lt;br /&gt;
Update: Note that in &#039;&#039;&#039;3.1.1 only&#039;&#039;&#039; there is special handling of this in the core.  Tags in the edit screen &#039;&#039;&#039;MUST&#039;&#039;&#039; be part of a metadata &amp;lt;fields&amp;gt;&amp;lt;/fields&amp;gt; group.  The core provides two JLayouts to help you manage standard layouts, one (details) for the metadata and one for the sidebar that includes the tabs. In update an extension to 3.1.4 you may need to fix adjust your edit views.&lt;br /&gt;
&lt;br /&gt;
In 3.1.4 or later this special handling is not necessary.  Best practice is to use the standard JLayouts since this provides a consistent experience for users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field loads all the Javascript libraries required. You don&#039;t need to worry about that.&lt;br /&gt;
&lt;br /&gt;
The field supports two modes:  &lt;br /&gt;
* &#039;&#039;&#039;Nested tags mode.&#039;&#039;&#039; Hierarchical tag list. Doesn&#039;t support on the fly tag creation.&lt;br /&gt;
* &#039;&#039;&#039;AJAX mode.&#039;&#039;&#039;  Tags are searched while user types (3 min. chars required to launch the AJAX search). Custom tags are added by pressing ENTER or COMMA keys. Tags show the global route/path. Example: &amp;lt;code&amp;gt;grandpa/parent/tag&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field mode can be forced or use the com_tags setting &amp;lt;code&amp;gt;Tag field mode&amp;lt;/code&amp;gt; to determine its mode. To set/force the field mode we have to add &#039;&#039;&#039;mode=&amp;quot;ajax&amp;quot;&#039;&#039;&#039; or &#039;&#039;&#039;mode=&amp;quot;nested&amp;quot;&#039;&#039;&#039; to the tag field definition.  &lt;br /&gt;
&lt;br /&gt;
Example of forced AJAX mode:  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The field also includes an attribute to allow/deny the user to enter custom values. Currently this only works in AJAX mode. The attribute has to be added to the field definition like **custom=&amp;quot;allow&amp;quot;** or **custom=&amp;quot;deny&amp;quot;**  &lt;br /&gt;
&lt;br /&gt;
Example field definition with custom tags denied:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot; custom=&amp;quot;denied&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
if it is not already there. Usually multiple should be true unless you have a specific reason for it not to be.&lt;br /&gt;
In the core components in administrator, the field is in the group shown on the right, below the language field.&lt;br /&gt;
&lt;br /&gt;
Note: As of 3.1.2 if you wish to use the field to designate parent tags you must add parent=&amp;quot;parent&amp;quot; to the xml definition of the field.&lt;br /&gt;
&lt;br /&gt;
== Prepare the view ==&lt;br /&gt;
&lt;br /&gt;
Add an appropriate version of this to your view.html.php file before loading the layout:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$item-&amp;gt;tags = new JHelperTags;&lt;br /&gt;
$item-&amp;gt;tags-&amp;gt;getItemTags(&#039;com_newsfeeds.newsfeed.&#039; , $this-&amp;gt;item-&amp;gt;id); &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter should match a type in the types table. The second parameter is the primary key under which this record is stored in your table. This would be used in any display in any view where you want the tags associated with the item to display.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Set up the display ==&lt;br /&gt;
&lt;br /&gt;
In any layout where you want to display the tags associated with an item add:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Changing the object and property names as appropriate.&lt;br /&gt;
&lt;br /&gt;
You will most likely want to add the show_tags parameter to the item parameters, the menu item parameters and component configuration as appropriate for your use case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;?php if ($this-&amp;gt;params-&amp;gt;get(&#039;show_tags&#039;, 1) &amp;amp;&amp;amp; !empty($this-&amp;gt;item-&amp;gt;tags)) : ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
	&amp;lt;?php endif; ?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Batch processing ==&lt;br /&gt;
If you want to add the ability to do batch tagging to a backend list view do the following.&lt;br /&gt;
&lt;br /&gt;
Add tag to the default_batch layout&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
        &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&lt;br /&gt;
				&amp;lt;?php echo JHtml::_(&#039;batch.tag&#039;);?&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;/div&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add tag to the button class in the modal footer (Not totally necessary at this point, but good for potential future changes e.g. if unTag is added.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;button class=&amp;quot;btn&amp;quot; type=&amp;quot;button&amp;quot; onclick=&amp;quot;document.id(&#039;batch-category-id&#039;).value=&#039;&#039;;document.id(&#039;batch-access&#039;).value=&#039;&#039;;document.id(&#039;batch-language-id&#039;).value=&#039;&#039;;document.id(&#039;batch-user-id&#039;).value=&#039;&#039;;document.id(&#039;batch-tag-id)&#039;).value=&#039;&#039;&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a batch method to your model if you are not extending JModelAdmin or overriding the batch method. ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Batch tag a list of item.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @param   integer  $value     The value of the new tag.&lt;br /&gt;
	 * @param   array    $pks       An array of row IDs.&lt;br /&gt;
	 * @param   array    $contexts  An array of item contexts.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @return  void.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @since   3.1&lt;br /&gt;
	 */&lt;br /&gt;
	protected function batchTag($value, $pks, $contexts)&lt;br /&gt;
	{&lt;br /&gt;
		$tagsHelper = new JHelperTags();&lt;br /&gt;
		$tagsHelper-&amp;gt;tagItems($value, $pks, $contexts);&lt;br /&gt;
&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And modify your batch method by adding&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		if (!empty($commands[&#039;tag&#039;]))&lt;br /&gt;
		{&lt;br /&gt;
			if (!$this-&amp;gt;batchTag($commands[&#039;tag&#039;], $pks, $contexts))&lt;br /&gt;
			{&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			$done = true;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pay attention to any JSON encoded strings that you need special handling for in batch processing -- remember that you are saving a copy of the core fields and you need save to work as expected.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s it, now create some tags, tag some items and you are set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]][[Category:Content Tags]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117717</id>
		<title>J3.x:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117717"/>
		<updated>2014-04-24T16:48:09Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: // added link to postflight&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{copyedit}}&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s tagging system is used in all core content extensions and  is designed to be be easy to integrate into other extensions that use standard Joomla design patterns. &lt;br /&gt;
&lt;br /&gt;
Using tags in an extension is relatively straightforward but it requires small changes in a number of specific places.&lt;br /&gt;
&lt;br /&gt;
== Create a content type for each view ==&lt;br /&gt;
&lt;br /&gt;
Note: Types can be added using either sql or [[J2.5:Managing_Component_Updates_(Script.php)#postflight|postflight]] by creating a JTableContenttype instance and adding a row.&lt;br /&gt;
&lt;br /&gt;
First identify which tables contain data that will be tagged and what the name of the single item view displaying each of these tables. For example:&lt;br /&gt;
* #__contact_details  is the table displayed in the view contact of component com_contact with table class ContactTableContact.&lt;br /&gt;
* #__weblinks is the table displayed in the view weblink of com_weblinks so WeblinksTableWeblink. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
 CREATE TABLE IF NOT EXISTS `#__content_types` (&lt;br /&gt;
  `type_id` int(10) unsigned NOT NULL AUTO_INCREMENT,&lt;br /&gt;
  `type_title` varchar(255) NOT NULL DEFAULT &#039;&#039;,&lt;br /&gt;
  `type_alias` varchar(255) NOT NULL DEFAULT &#039;&#039;,&lt;br /&gt;
  `table` varchar(255) NOT NULL DEFAULT &#039;&#039;,&lt;br /&gt;
  `rules` text NOT NULL,&lt;br /&gt;
  `field_mappings` text NOT NULL,&lt;br /&gt;
  `router` varchar(255) NOT NULL DEFAULT &#039;&#039;,&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For each table/option/view you will need to make an entry in #__content_types.  You can do this either using sql or postflight by creating an instance of JTableContenttype. Don&#039;t forget a category type if you use the Joomla categories API.&lt;br /&gt;
&lt;br /&gt;
A string giving component.view (that would be in the page request, typically matching the model name) goes in the **type_alias** field.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;table&#039;&#039;&#039; gives the complete table class information for the table class stored as a JSON object in which the first element represents your &amp;quot;special&amp;quot; table and the second an optional common table (otherwise it will default the JTableCorecontent. This includes the name of the database table, the name of the primary key, the prefix, the name, an option array as used in your constructor and getInstance() methods. This enables the tagging system (and other APIs) to access your table easily. &lt;br /&gt;
&lt;br /&gt;
If you are using Joomla categories make sure to create a category type so that they can be tagged. In Joomla 3.1 and 3.1.1 there is an error where the tag field will show even if there is not a type, but this is corrected in 3.1.4.  &lt;br /&gt;
  &lt;br /&gt;
&#039;&#039;&#039;Important note&#039;&#039;&#039; : Please note that the table name for the common table is #__ucm_content. This is INCORRECT in 3.1 and 3.1.1 data but is not currently used.  The data was updated in 3.1.4.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
{&amp;quot;special&amp;quot;:{&amp;quot;dbtable&amp;quot;:&amp;quot;#__content&amp;quot;,&amp;quot;key&amp;quot;:&amp;quot;id&amp;quot;,&amp;quot;type&amp;quot;:&amp;quot;Content&amp;quot;,&amp;quot;prefix&amp;quot;:&amp;quot;JTable&amp;quot;,&amp;quot;config&amp;quot;:&amp;quot;array()&amp;quot;},&amp;quot;common&amp;quot;:{&amp;quot;dbtable&amp;quot;:&amp;quot;#__ucm_content&amp;quot;,&amp;quot;key&amp;quot;:&amp;quot;ucm_id&amp;quot;,&amp;quot;type&amp;quot;:&amp;quot;Corecontent&amp;quot;,&amp;quot;prefix&amp;quot;:&amp;quot;JTable&amp;quot;,&amp;quot;config&amp;quot;:&amp;quot;array()&amp;quot;}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;type_title&#039;&#039;&#039; field would potentially be used for display although this is not implemented currently except in the contentttype field.  Usually it should begin with an upper case letter if it is in English. See note about how to make this translatable. &lt;br /&gt;
&lt;br /&gt;
 note:&lt;br /&gt;
 To make your type names translatable add &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
COM_TAGS_CONTENT_TYPE_ + the type_title&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
in both the ini and sys.ini files. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rules&#039;&#039;&#039;  is currently not used. It will likely be removed in favor of an asset_id for each type, but currently you can ignore this field which will be managed by JTable. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;field_mappings&#039;&#039;&#039; maps the names of specific fields in your table to a set of standard names. This mapping is stored as a JSON array with the first element mapping to the common fields and the second one mapping the other fields from the table. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important note&#039;&#039;&#039;  The JHelperTags and JUcm APIS at 3.1.1 supported arrays for this field, but as of  3.1.4 either arrays or objects with a default of objects are supported.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&#039;{&amp;quot;common&amp;quot;:[{&amp;quot;core_content_item_id&amp;quot;:&amp;quot;id&amp;quot;,&amp;quot;core_title&amp;quot;:&amp;quot;title&amp;quot;,&amp;quot;core_state&amp;quot;:&amp;quot;published&amp;quot;,&amp;quot;core_alias&amp;quot;:&amp;quot;alias&amp;quot;,&amp;quot;core_created_time&amp;quot;:&amp;quot;created_time&amp;quot;,&amp;quot;core_modified_time&amp;quot;:&amp;quot;modified_time&amp;quot;,&amp;quot;core_body&amp;quot;:&amp;quot;description&amp;quot;, &amp;quot;core_hits&amp;quot;:&amp;quot;hits&amp;quot;,&amp;quot;core_publish_up&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_publish_down&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_access&amp;quot;:&amp;quot;access&amp;quot;, &amp;quot;core_params&amp;quot;:&amp;quot;params&amp;quot;, &amp;quot;core_featured&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metadata&amp;quot;:&amp;quot;metadata&amp;quot;, &amp;quot;core_language&amp;quot;:&amp;quot;language&amp;quot;, &amp;quot;core_images&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_urls&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_version&amp;quot;:&amp;quot;version&amp;quot;, &amp;quot;core_ordering&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metakey&amp;quot;:&amp;quot;metakey&amp;quot;, &amp;quot;core_metadesc&amp;quot;:&amp;quot;metadesc&amp;quot;, &amp;quot;core_catid&amp;quot;:&amp;quot;parent_id&amp;quot;, &amp;quot;core_xreference&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;asset_id&amp;quot;:&amp;quot;asset_id&amp;quot;}], &amp;quot;special&amp;quot;: [{&amp;quot;parent_id&amp;quot;:&amp;quot;parent_id&amp;quot;,&amp;quot;lft&amp;quot;:&amp;quot;lft&amp;quot;,&amp;quot;rgt&amp;quot;:&amp;quot;rgt&amp;quot;,&amp;quot;level&amp;quot;:&amp;quot;level&amp;quot;,&amp;quot;path&amp;quot;:&amp;quot;path&amp;quot;,&amp;quot;extension&amp;quot;:&amp;quot;extension&amp;quot;,&amp;quot;note&amp;quot;:&amp;quot;note&amp;quot;}]}&#039;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is the field mapping for the Article type. Note that article uses the name attribs for the core field called params.  If your table does not contain a field, put “null” instead.  Leaving it blank may cause SQL issues. The special fields are optional.  At a minimum for common fields you need to map: content_item_id, alias and title in order to successfully create urls in the tagged items list.  You also will probably want: access, status, and language.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Router&#039;&#039;&#039; is an optional name of a static helper router method for this type found in its front end helpers folder. &lt;br /&gt;
If you only store data in #__ucm_content you will eventually be able to leave the router field blank although this option is not currently implemented. If you do not have a custom router tags falls back to the rules found in JHelperRoute.&lt;br /&gt;
&lt;br /&gt;
== Modify your component&#039;s table class (or classes if you have multiple tables) ==&lt;br /&gt;
&lt;br /&gt;
Add the following to your JTable constructor:&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;_observers = new JObserverUpdater($this);&lt;br /&gt;
JObserverMapper::attachAllObservers($this);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The addition material formerly located here is no longer needed after 3.1.4&#039;&#039;&#039; Please read the document history for this page if you are required to support 3.1.0 or 3.1.1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;For joomla 3.2 I needed this line:&#039;&#039;&#039; ( Dont forget to replace weblinks by your component name )&lt;br /&gt;
&lt;br /&gt;
JObserverMapper::addObserverClassToClass(&#039;JTableObserverTags&#039;, &#039;WeblinksTableWeblink&#039;, array(&#039;typeAlias&#039; =&amp;gt; &#039;com_weblinks.weblink&#039;));&lt;br /&gt;
&lt;br /&gt;
== Add tags to the getItem() method of the model ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: this was only required in 3.1.0 and 3.1.1. It should not be used in 3.1.4 or later.&#039;&#039;&#039; Please read the history of this page if you need instructions for older versions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add a tag field to edit screens ==&lt;br /&gt;
&lt;br /&gt;
In any edit layouts where you want to allow tagging, you need to add the field to the xml and the appropriate layouts if necessary. The core layouts us a JLayout to manage this and the same layout by any extension.&lt;br /&gt;
&lt;br /&gt;
Update: Note that in &#039;&#039;&#039;3.1.1 only&#039;&#039;&#039; there is special handling of this in the core.  Tags in the edit screen &#039;&#039;&#039;MUST&#039;&#039;&#039; be part of a metadata &amp;lt;fields&amp;gt;&amp;lt;/fields&amp;gt; group.  The core provides two JLayouts to help you manage standard layouts, one (details) for the metadata and one for the sidebar that includes the tabs. In update an extension to 3.1.4 you may need to fix adjust your edit views.&lt;br /&gt;
&lt;br /&gt;
In 3.1.4 or later this special handling is not necessary.  Best practice is to use the standard JLayouts since this provides a consistent experience for users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field loads all the Javascript libraries required. You don&#039;t need to worry about that.&lt;br /&gt;
&lt;br /&gt;
The field supports two modes:  &lt;br /&gt;
* &#039;&#039;&#039;Nested tags mode.&#039;&#039;&#039; Hierarchical tag list. Doesn&#039;t support on the fly tag creation.&lt;br /&gt;
* &#039;&#039;&#039;AJAX mode.&#039;&#039;&#039;  Tags are searched while user types (3 min. chars required to launch the AJAX search). Custom tags are added by pressing ENTER or COMMA keys. Tags show the global route/path. Example: &amp;lt;code&amp;gt;grandpa/parent/tag&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field mode can be forced or use the com_tags setting &amp;lt;code&amp;gt;Tag field mode&amp;lt;/code&amp;gt; to determine its mode. To set/force the field mode we have to add &#039;&#039;&#039;mode=&amp;quot;ajax&amp;quot;&#039;&#039;&#039; or &#039;&#039;&#039;mode=&amp;quot;nested&amp;quot;&#039;&#039;&#039; to the tag field definition.  &lt;br /&gt;
&lt;br /&gt;
Example of forced AJAX mode:  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The field also includes an attribute to allow/deny the user to enter custom values. Currently this only works in AJAX mode. The attribute has to be added to the field definition like **custom=&amp;quot;allow&amp;quot;** or **custom=&amp;quot;deny&amp;quot;**  &lt;br /&gt;
&lt;br /&gt;
Example field definition with custom tags denied:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot; custom=&amp;quot;denied&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
if it is not already there. Usually multiple should be true unless you have a specific reason for it not to be.&lt;br /&gt;
In the core components in administrator, the field is in the group shown on the right, below the language field.&lt;br /&gt;
&lt;br /&gt;
Note: As of 3.1.2 if you wish to use the field to designate parent tags you must add parent=&amp;quot;parent&amp;quot; to the xml definition of the field.&lt;br /&gt;
&lt;br /&gt;
== Prepare the view ==&lt;br /&gt;
&lt;br /&gt;
Add an appropriate version of this to your view.html.php file before loading the layout:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$item-&amp;gt;tags = new JHelperTags;&lt;br /&gt;
$item-&amp;gt;tags-&amp;gt;getItemTags(&#039;com_newsfeeds.newsfeed.&#039; , $this-&amp;gt;item-&amp;gt;id); &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter should match a type in the types table. The second parameter is the primary key under which this record is stored in your table. This would be used in any display in any view where you want the tags associated with the item to display.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Set up the display ==&lt;br /&gt;
&lt;br /&gt;
In any layout where you want to display the tags associated with an item add:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Changing the object and property names as appropriate.&lt;br /&gt;
&lt;br /&gt;
You will most likely want to add the show_tags parameter to the item parameters, the menu item parameters and component configuration as appropriate for your use case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;?php if ($this-&amp;gt;params-&amp;gt;get(&#039;show_tags&#039;, 1) &amp;amp;&amp;amp; !empty($this-&amp;gt;item-&amp;gt;tags)) : ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
	&amp;lt;?php endif; ?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Batch processing ==&lt;br /&gt;
If you want to add the ability to do batch tagging to a backend list view do the following.&lt;br /&gt;
&lt;br /&gt;
Add tag to the default_batch layout&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
        &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&lt;br /&gt;
				&amp;lt;?php echo JHtml::_(&#039;batch.tag&#039;);?&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;/div&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add tag to the button class in the modal footer (Not totally necessary at this point, but good for potential future changes e.g. if unTag is added.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;button class=&amp;quot;btn&amp;quot; type=&amp;quot;button&amp;quot; onclick=&amp;quot;document.id(&#039;batch-category-id&#039;).value=&#039;&#039;;document.id(&#039;batch-access&#039;).value=&#039;&#039;;document.id(&#039;batch-language-id&#039;).value=&#039;&#039;;document.id(&#039;batch-user-id&#039;).value=&#039;&#039;;document.id(&#039;batch-tag-id)&#039;).value=&#039;&#039;&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a batch method to your model if you are not extending JModelAdmin or overriding the batch method. ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Batch tag a list of item.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @param   integer  $value     The value of the new tag.&lt;br /&gt;
	 * @param   array    $pks       An array of row IDs.&lt;br /&gt;
	 * @param   array    $contexts  An array of item contexts.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @return  void.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @since   3.1&lt;br /&gt;
	 */&lt;br /&gt;
	protected function batchTag($value, $pks, $contexts)&lt;br /&gt;
	{&lt;br /&gt;
		$tagsHelper = new JHelperTags();&lt;br /&gt;
		$tagsHelper-&amp;gt;tagItems($value, $pks, $contexts);&lt;br /&gt;
&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And modify your batch method by adding&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		if (!empty($commands[&#039;tag&#039;]))&lt;br /&gt;
		{&lt;br /&gt;
			if (!$this-&amp;gt;batchTag($commands[&#039;tag&#039;], $pks, $contexts))&lt;br /&gt;
			{&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			$done = true;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pay attention to any JSON encoded strings that you need special handling for in batch processing -- remember that you are saving a copy of the core fields and you need save to work as expected.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s it, now create some tags, tag some items and you are set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]][[Category:Content Tags]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117716</id>
		<title>J3.x:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117716"/>
		<updated>2014-04-24T15:33:08Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: // copyedit&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{copyedit}}&lt;br /&gt;
&lt;br /&gt;
Joomla&#039;s tagging system is used in all core content extensions and  is designed to be be easy to integrate into other extensions that use standard Joomla design patterns. &lt;br /&gt;
&lt;br /&gt;
Using tags in an extension is relatively straightforward but it requires small changes in a number of specific places.&lt;br /&gt;
&lt;br /&gt;
== Create a content type for each view ==&lt;br /&gt;
&lt;br /&gt;
Note: Types can be added using either sql or postflight by creating a JTableContenttype instance and adding a row.&lt;br /&gt;
&lt;br /&gt;
First identify which tables contain data that will be tagged and what the name of the single item view displaying each of these tables. For example:&lt;br /&gt;
* #__contact_details  is the table displayed in the view contact of component com_contact with table class ContactTableContact.&lt;br /&gt;
* #__weblinks is the table displayed in the view weblink of com_weblinks so WeblinksTableWeblink. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
 CREATE TABLE IF NOT EXISTS `#__content_types` (&lt;br /&gt;
  `type_id` int(10) unsigned NOT NULL AUTO_INCREMENT,&lt;br /&gt;
  `type_title` varchar(255) NOT NULL DEFAULT &#039;&#039;,&lt;br /&gt;
  `type_alias` varchar(255) NOT NULL DEFAULT &#039;&#039;,&lt;br /&gt;
  `table` varchar(255) NOT NULL DEFAULT &#039;&#039;,&lt;br /&gt;
  `rules` text NOT NULL,&lt;br /&gt;
  `field_mappings` text NOT NULL,&lt;br /&gt;
  `router` varchar(255) NOT NULL DEFAULT &#039;&#039;,&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For each table/option/view you will need to make an entry in #__content_types.  You can do this either using sql or postflight by creating an instance of JTableContenttype. Don&#039;t forget a category type if you use the Joomla categories API.&lt;br /&gt;
&lt;br /&gt;
A string giving component.view (that would be in the page request, typically matching the model name) goes in the **type_alias** field.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;table&#039;&#039;&#039; gives the complete table class information for the table class stored as a JSON object in which the first element represents your &amp;quot;special&amp;quot; table and the second an optional common table (otherwise it will default the JTableCorecontent. This includes the name of the database table, the name of the primary key, the prefix, the name, an option array as used in your constructor and getInstance() methods. This enables the tagging system (and other APIs) to access your table easily. &lt;br /&gt;
&lt;br /&gt;
If you are using Joomla categories make sure to create a category type so that they can be tagged. In Joomla 3.1 and 3.1.1 there is an error where the tag field will show even if there is not a type, but this is corrected in 3.1.4.  &lt;br /&gt;
  &lt;br /&gt;
&#039;&#039;&#039;Important note&#039;&#039;&#039; : Please note that the table name for the common table is #__ucm_content. This is INCORRECT in 3.1 and 3.1.1 data but is not currently used.  The data was updated in 3.1.4.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
{&amp;quot;special&amp;quot;:{&amp;quot;dbtable&amp;quot;:&amp;quot;#__content&amp;quot;,&amp;quot;key&amp;quot;:&amp;quot;id&amp;quot;,&amp;quot;type&amp;quot;:&amp;quot;Content&amp;quot;,&amp;quot;prefix&amp;quot;:&amp;quot;JTable&amp;quot;,&amp;quot;config&amp;quot;:&amp;quot;array()&amp;quot;},&amp;quot;common&amp;quot;:{&amp;quot;dbtable&amp;quot;:&amp;quot;#__ucm_content&amp;quot;,&amp;quot;key&amp;quot;:&amp;quot;ucm_id&amp;quot;,&amp;quot;type&amp;quot;:&amp;quot;Corecontent&amp;quot;,&amp;quot;prefix&amp;quot;:&amp;quot;JTable&amp;quot;,&amp;quot;config&amp;quot;:&amp;quot;array()&amp;quot;}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;type_title&#039;&#039;&#039; field would potentially be used for display although this is not implemented currently except in the contentttype field.  Usually it should begin with an upper case letter if it is in English. See note about how to make this translatable. &lt;br /&gt;
&lt;br /&gt;
 note:&lt;br /&gt;
 To make your type names translatable add &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
COM_TAGS_CONTENT_TYPE_ + the type_title&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
in both the ini and sys.ini files. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rules&#039;&#039;&#039;  is currently not used. It will likely be removed in favor of an asset_id for each type, but currently you can ignore this field which will be managed by JTable. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;field_mappings&#039;&#039;&#039; maps the names of specific fields in your table to a set of standard names. This mapping is stored as a JSON array with the first element mapping to the common fields and the second one mapping the other fields from the table. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important note&#039;&#039;&#039;  The JHelperTags and JUcm APIS at 3.1.1 supported arrays for this field, but as of  3.1.4 either arrays or objects with a default of objects are supported.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&#039;{&amp;quot;common&amp;quot;:[{&amp;quot;core_content_item_id&amp;quot;:&amp;quot;id&amp;quot;,&amp;quot;core_title&amp;quot;:&amp;quot;title&amp;quot;,&amp;quot;core_state&amp;quot;:&amp;quot;published&amp;quot;,&amp;quot;core_alias&amp;quot;:&amp;quot;alias&amp;quot;,&amp;quot;core_created_time&amp;quot;:&amp;quot;created_time&amp;quot;,&amp;quot;core_modified_time&amp;quot;:&amp;quot;modified_time&amp;quot;,&amp;quot;core_body&amp;quot;:&amp;quot;description&amp;quot;, &amp;quot;core_hits&amp;quot;:&amp;quot;hits&amp;quot;,&amp;quot;core_publish_up&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_publish_down&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_access&amp;quot;:&amp;quot;access&amp;quot;, &amp;quot;core_params&amp;quot;:&amp;quot;params&amp;quot;, &amp;quot;core_featured&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metadata&amp;quot;:&amp;quot;metadata&amp;quot;, &amp;quot;core_language&amp;quot;:&amp;quot;language&amp;quot;, &amp;quot;core_images&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_urls&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_version&amp;quot;:&amp;quot;version&amp;quot;, &amp;quot;core_ordering&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metakey&amp;quot;:&amp;quot;metakey&amp;quot;, &amp;quot;core_metadesc&amp;quot;:&amp;quot;metadesc&amp;quot;, &amp;quot;core_catid&amp;quot;:&amp;quot;parent_id&amp;quot;, &amp;quot;core_xreference&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;asset_id&amp;quot;:&amp;quot;asset_id&amp;quot;}], &amp;quot;special&amp;quot;: [{&amp;quot;parent_id&amp;quot;:&amp;quot;parent_id&amp;quot;,&amp;quot;lft&amp;quot;:&amp;quot;lft&amp;quot;,&amp;quot;rgt&amp;quot;:&amp;quot;rgt&amp;quot;,&amp;quot;level&amp;quot;:&amp;quot;level&amp;quot;,&amp;quot;path&amp;quot;:&amp;quot;path&amp;quot;,&amp;quot;extension&amp;quot;:&amp;quot;extension&amp;quot;,&amp;quot;note&amp;quot;:&amp;quot;note&amp;quot;}]}&#039;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is the field mapping for the Article type. Note that article uses the name attribs for the core field called params.  If your table does not contain a field, put “null” instead.  Leaving it blank may cause SQL issues. The special fields are optional.  At a minimum for common fields you need to map: content_item_id, alias and title in order to successfully create urls in the tagged items list.  You also will probably want: access, status, and language.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Router&#039;&#039;&#039; is an optional name of a static helper router method for this type found in its front end helpers folder. &lt;br /&gt;
If you only store data in #__ucm_content you will eventually be able to leave the router field blank although this option is not currently implemented. If you do not have a custom router tags falls back to the rules found in JHelperRoute.&lt;br /&gt;
&lt;br /&gt;
== Modify your component&#039;s table class (or classes if you have multiple tables) ==&lt;br /&gt;
&lt;br /&gt;
Add the following to your JTable constructor:&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;_observers = new JObserverUpdater($this);&lt;br /&gt;
JObserverMapper::attachAllObservers($this);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The addition material formerly located here is no longer needed after 3.1.4&#039;&#039;&#039; Please read the document history for this page if you are required to support 3.1.0 or 3.1.1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;For joomla 3.2 I needed this line:&#039;&#039;&#039; ( Dont forget to replace weblinks by your component name )&lt;br /&gt;
&lt;br /&gt;
JObserverMapper::addObserverClassToClass(&#039;JTableObserverTags&#039;, &#039;WeblinksTableWeblink&#039;, array(&#039;typeAlias&#039; =&amp;gt; &#039;com_weblinks.weblink&#039;));&lt;br /&gt;
&lt;br /&gt;
== Add tags to the getItem() method of the model ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: this was only required in 3.1.0 and 3.1.1. It should not be used in 3.1.4 or later.&#039;&#039;&#039; Please read the history of this page if you need instructions for older versions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add a tag field to edit screens ==&lt;br /&gt;
&lt;br /&gt;
In any edit layouts where you want to allow tagging, you need to add the field to the xml and the appropriate layouts if necessary. The core layouts us a JLayout to manage this and the same layout by any extension.&lt;br /&gt;
&lt;br /&gt;
Update: Note that in &#039;&#039;&#039;3.1.1 only&#039;&#039;&#039; there is special handling of this in the core.  Tags in the edit screen &#039;&#039;&#039;MUST&#039;&#039;&#039; be part of a metadata &amp;lt;fields&amp;gt;&amp;lt;/fields&amp;gt; group.  The core provides two JLayouts to help you manage standard layouts, one (details) for the metadata and one for the sidebar that includes the tabs. In update an extension to 3.1.4 you may need to fix adjust your edit views.&lt;br /&gt;
&lt;br /&gt;
In 3.1.4 or later this special handling is not necessary.  Best practice is to use the standard JLayouts since this provides a consistent experience for users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field loads all the Javascript libraries required. You don&#039;t need to worry about that.&lt;br /&gt;
&lt;br /&gt;
The field supports two modes:  &lt;br /&gt;
* &#039;&#039;&#039;Nested tags mode.&#039;&#039;&#039; Hierarchical tag list. Doesn&#039;t support on the fly tag creation.&lt;br /&gt;
* &#039;&#039;&#039;AJAX mode.&#039;&#039;&#039;  Tags are searched while user types (3 min. chars required to launch the AJAX search). Custom tags are added by pressing ENTER or COMMA keys. Tags show the global route/path. Example: &amp;lt;code&amp;gt;grandpa/parent/tag&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field mode can be forced or use the com_tags setting &amp;lt;code&amp;gt;Tag field mode&amp;lt;/code&amp;gt; to determine its mode. To set/force the field mode we have to add &#039;&#039;&#039;mode=&amp;quot;ajax&amp;quot;&#039;&#039;&#039; or &#039;&#039;&#039;mode=&amp;quot;nested&amp;quot;&#039;&#039;&#039; to the tag field definition.  &lt;br /&gt;
&lt;br /&gt;
Example of forced AJAX mode:  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The field also includes an attribute to allow/deny the user to enter custom values. Currently this only works in AJAX mode. The attribute has to be added to the field definition like **custom=&amp;quot;allow&amp;quot;** or **custom=&amp;quot;deny&amp;quot;**  &lt;br /&gt;
&lt;br /&gt;
Example field definition with custom tags denied:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot; custom=&amp;quot;denied&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
if it is not already there. Usually multiple should be true unless you have a specific reason for it not to be.&lt;br /&gt;
In the core components in administrator, the field is in the group shown on the right, below the language field.&lt;br /&gt;
&lt;br /&gt;
Note: As of 3.1.2 if you wish to use the field to designate parent tags you must add parent=&amp;quot;parent&amp;quot; to the xml definition of the field.&lt;br /&gt;
&lt;br /&gt;
== Prepare the view ==&lt;br /&gt;
&lt;br /&gt;
Add an appropriate version of this to your view.html.php file before loading the layout:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$item-&amp;gt;tags = new JHelperTags;&lt;br /&gt;
$item-&amp;gt;tags-&amp;gt;getItemTags(&#039;com_newsfeeds.newsfeed.&#039; , $this-&amp;gt;item-&amp;gt;id); &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter should match a type in the types table. The second parameter is the primary key under which this record is stored in your table. This would be used in any display in any view where you want the tags associated with the item to display.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Set up the display ==&lt;br /&gt;
&lt;br /&gt;
In any layout where you want to display the tags associated with an item add:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Changing the object and property names as appropriate.&lt;br /&gt;
&lt;br /&gt;
You will most likely want to add the show_tags parameter to the item parameters, the menu item parameters and component configuration as appropriate for your use case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;?php if ($this-&amp;gt;params-&amp;gt;get(&#039;show_tags&#039;, 1) &amp;amp;&amp;amp; !empty($this-&amp;gt;item-&amp;gt;tags)) : ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
	&amp;lt;?php endif; ?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Batch processing ==&lt;br /&gt;
If you want to add the ability to do batch tagging to a backend list view do the following.&lt;br /&gt;
&lt;br /&gt;
Add tag to the default_batch layout&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
        &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&lt;br /&gt;
				&amp;lt;?php echo JHtml::_(&#039;batch.tag&#039;);?&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;/div&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add tag to the button class in the modal footer (Not totally necessary at this point, but good for potential future changes e.g. if unTag is added.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;button class=&amp;quot;btn&amp;quot; type=&amp;quot;button&amp;quot; onclick=&amp;quot;document.id(&#039;batch-category-id&#039;).value=&#039;&#039;;document.id(&#039;batch-access&#039;).value=&#039;&#039;;document.id(&#039;batch-language-id&#039;).value=&#039;&#039;;document.id(&#039;batch-user-id&#039;).value=&#039;&#039;;document.id(&#039;batch-tag-id)&#039;).value=&#039;&#039;&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a batch method to your model if you are not extending JModelAdmin or overriding the batch method. ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Batch tag a list of item.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @param   integer  $value     The value of the new tag.&lt;br /&gt;
	 * @param   array    $pks       An array of row IDs.&lt;br /&gt;
	 * @param   array    $contexts  An array of item contexts.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @return  void.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @since   3.1&lt;br /&gt;
	 */&lt;br /&gt;
	protected function batchTag($value, $pks, $contexts)&lt;br /&gt;
	{&lt;br /&gt;
		$tagsHelper = new JHelperTags();&lt;br /&gt;
		$tagsHelper-&amp;gt;tagItems($value, $pks, $contexts);&lt;br /&gt;
&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And modify your batch method by adding&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		if (!empty($commands[&#039;tag&#039;]))&lt;br /&gt;
		{&lt;br /&gt;
			if (!$this-&amp;gt;batchTag($commands[&#039;tag&#039;], $pks, $contexts))&lt;br /&gt;
			{&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			$done = true;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pay attention to any JSON encoded strings that you need special handling for in batch processing -- remember that you are saving a copy of the core fields and you need save to work as expected.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s it, now create some tags, tag some items and you are set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]][[Category:Content Tags]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x_talk:Using_Tags_in_an_Extension&amp;diff=117715</id>
		<title>J3.x talk:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x_talk:Using_Tags_in_an_Extension&amp;diff=117715"/>
		<updated>2014-04-24T15:28:54Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* Page contents */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Page contents ==&lt;br /&gt;
&lt;br /&gt;
The page contents as currently written are substantially incomprehensible. &lt;br /&gt;
It requires not so much copy-editing as re-interpretation into every day English. [[User:GreyHead|GreyHead]] ([[User talk:GreyHead|talk]]) 10:28, 24 April 2014 (CDT)&lt;br /&gt;
&lt;br /&gt;
== ??? ==&lt;br /&gt;
Also bind() on component&#039;s table class  on JTable should be overridden. Metadata should be json encoded before preStoreProcess.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
public function bind($array, $ignore = &#039;&#039;)&lt;br /&gt;
{&lt;br /&gt;
  if (isset($array[&#039;metadata&#039;]) &amp;amp;&amp;amp; is_array($array[&#039;metadata&#039;]))&lt;br /&gt;
  {&lt;br /&gt;
    $registry = new JRegistry;&lt;br /&gt;
    $registry-&amp;gt;loadArray($array[&#039;metadata&#039;]);&lt;br /&gt;
    $array[&#039;metadata&#039;] = (string) $registry;&lt;br /&gt;
  }&lt;br /&gt;
  return parent::bind($array, $ignore);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
([[User:Kasiopi|Kasiopi]] ([[User talk:Kasiopi|talk]]) 05:42, 25 June 2013 (CDT))&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x_talk:Using_Tags_in_an_Extension&amp;diff=117714</id>
		<title>J3.x talk:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x_talk:Using_Tags_in_an_Extension&amp;diff=117714"/>
		<updated>2014-04-24T15:28:29Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: Contents incomprehensible&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Page contents ==&lt;br /&gt;
&lt;br /&gt;
The page contents as currently written are substantially incomprehensible. &lt;br /&gt;
It requires not so much copy-editing as re-interpretation into every day English.&lt;br /&gt;
&lt;br /&gt;
== ??? ==&lt;br /&gt;
Also bind() on component&#039;s table class  on JTable should be overridden. Metadata should be json encoded before preStoreProcess.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
public function bind($array, $ignore = &#039;&#039;)&lt;br /&gt;
{&lt;br /&gt;
  if (isset($array[&#039;metadata&#039;]) &amp;amp;&amp;amp; is_array($array[&#039;metadata&#039;]))&lt;br /&gt;
  {&lt;br /&gt;
    $registry = new JRegistry;&lt;br /&gt;
    $registry-&amp;gt;loadArray($array[&#039;metadata&#039;]);&lt;br /&gt;
    $array[&#039;metadata&#039;] = (string) $registry;&lt;br /&gt;
  }&lt;br /&gt;
  return parent::bind($array, $ignore);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
([[User:Kasiopi|Kasiopi]] ([[User talk:Kasiopi|talk]]) 05:42, 25 June 2013 (CDT))&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=JDOC:Local_wiki_templates&amp;diff=117713</id>
		<title>JDOC:Local wiki templates</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=JDOC:Local_wiki_templates&amp;diff=117713"/>
		<updated>2014-04-24T15:23:59Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* Copyedit */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;p style=&amp;quot;text-align:center;border:1px solid #ccc&amp;quot;&amp;gt;[[JDOC:Local wiki templates|Local wiki templates]] &amp;amp;bull; [[JDOC:Local wiki extensions|Local wiki extensions]] &amp;amp;bull; [[JDOC:Local interwiki links|Local interwiki links]]&amp;lt;/p&amp;gt;&lt;br /&gt;
[[:Category:Wiki Templates|MediaWiki templates]] should not to be confused with Joomla! templates!  MediaWiki templates are &amp;quot;boiler-plate&amp;quot; items that can be included in your wiki pages by inserting a simple reference.&lt;br /&gt;
&lt;br /&gt;
==Incomplete Content Notice==&lt;br /&gt;
Use this notice to indicate a page or section is incomplete and missing key information. The {{tl|incomplete}} or {{tl|underconstruction}} are different. The {{tl|underconstruction}} template is used to note the page is not finished, but a contributor will be completing the page in the very near future.&lt;br /&gt;
Insert {{tl|incomplete}} to obtain the following notice:&lt;br /&gt;
{{incomplete}}&lt;br /&gt;
&lt;br /&gt;
==Page In Use Notice==&lt;br /&gt;
Use this notice should be used during &#039;&#039;&#039;Doc Sprints&#039;&#039;&#039; or if you will be editing a page for an extended period of time, e.g. more than 30 minutes. Please don&#039;t forget to remove it when you are done.&lt;br /&gt;
Insert {{tl|inuse}} at the top of the page to obtain the following notice:&lt;br /&gt;
{{inuse|category=no}}&lt;br /&gt;
&lt;br /&gt;
==Under Construction Notice==&lt;br /&gt;
The {{tl|underconstruction}} template is used to note the page is not finished, but a contributor will be completing the page in the very near future.&lt;br /&gt;
Insert {{tl|underconstruction}} to obtain the following notice:&lt;br /&gt;
{{underconstruction}}&lt;br /&gt;
&lt;br /&gt;
==Copyedit==&lt;br /&gt;
Use: &amp;lt;nowiki&amp;gt;{{copyedit}}&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{copyedit}}&lt;br /&gt;
&lt;br /&gt;
==Article Reviews and Needs==&lt;br /&gt;
Once you&#039;re done with editing please replace {{tl|underconstruction}} and {{tl|inuse}} with {{tl|review}}. This will add your document to the [[:Category:Needs review]].&lt;br /&gt;
{{review}}&lt;br /&gt;
You can also use the {{tl|page}} notice if the page needs review or something other than a review.&amp;lt;br/&amp;gt;&lt;br /&gt;
Examples are:&#039;&#039;&#039;&amp;lt;nowiki&amp;gt;{{page|needs more content}}&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; which would add the article to the [[:Category:Needs more content]].&lt;br /&gt;
{{page|needs more content}}Or use, &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;{{page|needs images}}&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; which would add the article to the [[:Category:Needs images]].&lt;br /&gt;
{{page|needs images}}&lt;br /&gt;
==Joomla Version Icons==&lt;br /&gt;
To indicate availability of a class, method or some piece of functionality in or from a particular version of Joomla, you can use the {{tl|JVer}} template.  Only certain specific version numbers are supported:&lt;br /&gt;
{|class=&amp;quot;prettytable parameters&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Version&lt;br /&gt;
!Short icon&lt;br /&gt;
!Long icon&lt;br /&gt;
|-&lt;br /&gt;
|CMS 1.0&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|1.0}}&amp;lt;/nowiki&amp;gt;: {{JVer|1.0}}&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|1.0|Joomla 1.0|long}}&amp;lt;/nowiki&amp;gt;: {{JVer|1.0|Joomla 1.0|long}}&lt;br /&gt;
|-&lt;br /&gt;
|CMS 1.5&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|1.5}}&amp;lt;/nowiki&amp;gt;: {{JVer|1.5}}&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|1.5|Joomla 1.5|long}}&amp;lt;/nowiki&amp;gt;: {{JVer|1.5|Joomla 1.5|long}}&lt;br /&gt;
|-&lt;br /&gt;
|CMS 1.6&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|1.6}}&amp;lt;/nowiki&amp;gt;: {{JVer|1.6}}&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|1.6|Joomla 1.6|long}}&amp;lt;/nowiki&amp;gt;: {{JVer|1.6|Joomla 1.6|long}}&lt;br /&gt;
|-&lt;br /&gt;
|CMS 1.7&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|1.7}}&amp;lt;/nowiki&amp;gt;: {{JVer|1.7}}&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|1.7|Joomla 1.7|long}}&amp;lt;/nowiki&amp;gt;: {{JVer|1.7|Joomla 1.7|long}}&lt;br /&gt;
|-&lt;br /&gt;
|CMS 2.5&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|2.5}}&amp;lt;/nowiki&amp;gt;: {{JVer|2.5}}&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|2,5|Joomla 2.5|long}}&amp;lt;/nowiki&amp;gt;: {{JVer|2.5|Joomla 2.5|long}}&lt;br /&gt;
|-&lt;br /&gt;
|CMS 3.x&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|3.x}}&amp;lt;/nowiki&amp;gt;: {{JVer|3.x}}&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|3.x|Joomla 3.x|long}}&amp;lt;/nowiki&amp;gt;: {{JVer|3.x|Joomla 3.x|long}}&lt;br /&gt;
|-&lt;br /&gt;
|CMS 3.0&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|3.0}}&amp;lt;/nowiki&amp;gt;: {{JVer|3.0}}&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|3.0|Joomla 3.0|long}}&amp;lt;/nowiki&amp;gt;: {{JVer|3.0|Joomla 3.0|long}}&lt;br /&gt;
|-&lt;br /&gt;
|CMS 3.1&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|3.1}}&amp;lt;/nowiki&amp;gt;: {{JVer|3.1}}&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|3.1|Joomla 3.1|long}}&amp;lt;/nowiki&amp;gt;: {{JVer|3.1|Joomla 3.1|long}}&lt;br /&gt;
|-&lt;br /&gt;
|Platform&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|Platform}}&amp;lt;/nowiki&amp;gt;: {{JVer|Platform}}&lt;br /&gt;
|Not available&lt;br /&gt;
|-&lt;br /&gt;
|Platform 11.1&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|11.1}}&amp;lt;/nowiki&amp;gt;: {{JVer|11.1}}&lt;br /&gt;
|Not available&lt;br /&gt;
|-&lt;br /&gt;
|Platform 11.2&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|11.2}}&amp;lt;/nowiki&amp;gt;: {{JVer|11.2}}&lt;br /&gt;
|Not available&lt;br /&gt;
|-&lt;br /&gt;
|Platform 11.3&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer|11.3}}&amp;lt;/nowiki&amp;gt;: {{JVer|11.3}}&lt;br /&gt;
|Not available&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
By default the &#039;&#039;alt&#039;&#039; tag generated will be the same as the version number, prefixed with the word &amp;quot;Joomla&amp;quot;.  You can override this default by adding a second parameter.  For example, to get an &#039;&#039;alt&#039;&#039; tag of &amp;quot;Only available in Joomla 1.5&amp;quot; you should use &amp;lt;nowiki&amp;gt;{{JVer|1.5|Only available in Joomla 1.5}}&amp;lt;/nowiki&amp;gt; which produces this: {{JVer|1.5|Only available in Joomla 1.5}}&lt;br /&gt;
&lt;br /&gt;
Full documentation on the JVer template can be found [[Template:JVer/doc|here]].&lt;br /&gt;
&lt;br /&gt;
You can also use the {{tl|JVer/multi}} Template to place multiple version icons.&lt;br /&gt;
&lt;br /&gt;
{|class=&amp;quot;prettytable parameters&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Versions&lt;br /&gt;
!Short icon&lt;br /&gt;
!Long icon&lt;br /&gt;
|-&lt;br /&gt;
|CMS Versions&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer/multi|1.0,1.5}}&amp;lt;/nowiki&amp;gt;: {{JVer/multi|1.0,1.5}}&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer/multi|1.6,1.7,2.5|Joomla Version|long}}&amp;lt;/nowiki&amp;gt;: {{JVer/multi|1.6,1.7,2.5|Joomla Version|long}}&lt;br /&gt;
|-&lt;br /&gt;
|Platform&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer/multi|11.1,11.2}}&amp;lt;/nowiki&amp;gt;: {{JVer/multi|11.1,11.2}}&lt;br /&gt;
| &#039;&#039;&#039;Not Available&#039;&#039;&#039;&lt;br /&gt;
|-&lt;br /&gt;
|Mix CMS/Platform&lt;br /&gt;
|&amp;lt;nowiki&amp;gt;{{JVer/multi|1.7,11.2}}&amp;lt;/nowiki&amp;gt;: {{JVer/multi|1.7,11.2}}&lt;br /&gt;
| &#039;&#039;&#039;Not Available&#039;&#039;&#039;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
For more information on use, specifically the long version icon use, please see, {{tl|JVer/multi}}&lt;br /&gt;
&lt;br /&gt;
==Table of Contents==&lt;br /&gt;
You will get a table of contents(TOC) box on each page by default as soon as you make 4 or more headings and subheadings on the page. As an example, please look at the one you see on this page.  The default table of contents box is located on the right and text will flow around it.  &lt;br /&gt;
*You can force the table of contents in the location you add the text: &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;__TOC__&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; &lt;br /&gt;
*Alternatively, you can suppress the table of contents box by adding anywhere on the page: &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;__NOTOC__&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==Organisational Templates==&lt;br /&gt;
Used to mark articles for deletion, merging or version splitting.&lt;br /&gt;
&lt;br /&gt;
===Delete===&lt;br /&gt;
Use the template {{tlx|delete|&amp;lt;reason text&amp;gt;}} on the very top of the page.&lt;br /&gt;
{{delete|This marker is just an example of use.}}&lt;br /&gt;
&lt;br /&gt;
===Merge===&lt;br /&gt;
Use the template {{tlx|merge||title of target page}} on the very top of the page.&lt;br /&gt;
{{merge|JDOC:Local wiki templates}}&lt;br /&gt;
&lt;br /&gt;
===JSplit===&lt;br /&gt;
Use the template {{tl|JSplit}} on the very top of the page.&lt;br /&gt;
{{JSplit}}&lt;br /&gt;
&lt;br /&gt;
===Split===&lt;br /&gt;
Use the template {{tlx|split|&amp;lt;reason for splitting&amp;gt;}} on the very top of the page.&lt;br /&gt;
{{split|This page is too long and hard to follow.}}&lt;br /&gt;
&lt;br /&gt;
==Version Notice==&lt;br /&gt;
If an article is written for a specific Joomla! Version, please use the {{tl|version}} template. It should be placed at the very top(first of all wiki markup) of an article so a person will see what version the page is relevant to. For example: &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;{{version|1.6,1.7,2.5}}&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; will place this:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{version|1.6,1.7,2.5}}&lt;br /&gt;
&lt;br /&gt;
If the article is a tutorial or part of a Tutorial Series, please use the &#039;&#039;&#039;&amp;lt;nowiki&amp;gt;{{version/tutor|1.5}}&amp;lt;/nowiki&amp;gt;&#039;&#039;&#039; template, which will show this:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{version/tutor|1.5}}&lt;br /&gt;
&lt;br /&gt;
This helps readers know what version the information references and aids in version icon(s) inclusion in lists of articles and tutorials. For more information and how to use extended features of this template, please see {{tl|version}} template documentation.&lt;br /&gt;
&lt;br /&gt;
==Standard License Notice==&lt;br /&gt;
Insert {{tl|license}} to obtain the following notice:&lt;br /&gt;
{{1}}{{license}}&lt;br /&gt;
&lt;br /&gt;
==Shortcuts, Links &amp;amp;amp; Navigation==&lt;br /&gt;
Some URLs can become utterly long, some are ugly, and some are just tedious to type over and over again. Adding categories to a page can become equally annoying. &lt;br /&gt;
&lt;br /&gt;
To simplify your job try some of the following templates. Similar to the interwikis, they&#039;re true [[:Category:Typing-aid templates|keystroke-savers]]:&lt;br /&gt;
* {{tlx|jsite|keyword}}, for example:&lt;br /&gt;
: &amp;lt;nowiki&amp;gt;{{jsite}}&amp;lt;/nowiki&amp;gt; = {{jsite}}&lt;br /&gt;
: &amp;lt;nowiki&amp;gt;{{jsite|forum}}&amp;lt;/nowiki&amp;gt; = {{jsite|forum}}&lt;br /&gt;
: &amp;lt;nowiki&amp;gt;{{jsite|ext}}&amp;lt;/nowiki&amp;gt; = {{jsite|ext}}. For more see {{tl|jsite}}.&lt;br /&gt;
* {{tlx|jforum|keyword}}, shortcuts for the many Working Group fora, i.e.&lt;br /&gt;
: &amp;lt;nowiki&amp;gt;{{jforum}}&amp;lt;/nowiki&amp;gt; = {{jforum}}&lt;br /&gt;
: &amp;lt;nowiki&amp;gt;{{jforum|doc}}&amp;lt;/nowiki&amp;gt; = {{jforum|doc}}&lt;br /&gt;
: &amp;lt;nowiki&amp;gt;{{jforum|devdoc}}&amp;lt;/nowiki&amp;gt; = {{jforum|devdoc}}. For more see {{tl|jforum}}.&lt;br /&gt;
&lt;br /&gt;
See also:&lt;br /&gt;
* [[:Category:Typing-aid templates]]&lt;br /&gt;
* [[:Category:Wiki Templates]] for the &#039;&#039;big list&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
[[Category:Style guides|{{PAGENAME}}]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117709</id>
		<title>J3.x:Using Tags in an Extension</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J3.x:Using_Tags_in_an_Extension&amp;diff=117709"/>
		<updated>2014-04-23T15:21:42Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla&#039;s tagging system is used in all core content extensions and  is designed to be be easy to integrate into other extensions that use standard Joomla design patterns. &lt;br /&gt;
&lt;br /&gt;
Using tags in an extension is relatively straightforward but it requires small changes in a number of specific places.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;!!! This document is mostly incomprehensible, please provide a translation into everyday English !!!&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
== Create a content type for each view ==&lt;br /&gt;
Note: Types can be added using either sql or postflight by creating a JTableContenttype instance and adding a row.&lt;br /&gt;
&lt;br /&gt;
First identify which tables contain data that will be tagged and what the name of the single item view displaying each of these tables. For example:&lt;br /&gt;
* #__contact_details  is the table displayed in the view contact of component com_contact with table class ContactTableContact.&lt;br /&gt;
* #__weblinks is the table displayed in the view weblink of com_weblinks so WeblinksTableWeblink. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;source lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
 CREATE TABLE IF NOT EXISTS `#__content_types` (&lt;br /&gt;
  `type_id` int(10) unsigned NOT NULL AUTO_INCREMENT,&lt;br /&gt;
  `type_title` varchar(255) NOT NULL DEFAULT &#039;&#039;,&lt;br /&gt;
  `type_alias` varchar(255) NOT NULL DEFAULT &#039;&#039;,&lt;br /&gt;
  `table` varchar(255) NOT NULL DEFAULT &#039;&#039;,&lt;br /&gt;
  `rules` text NOT NULL,&lt;br /&gt;
   `field_mappings` text NOT NULL,&lt;br /&gt;
 `router` varchar(255) NOT NULL DEFAULT &#039;&#039;,&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For each table/option/view you will need to make an entry in #__content_types.  You can do this either using sql or postflight by creating an instance of JTableContenttype. Don&#039;t forget a category type if you use the Joomla categories API.&lt;br /&gt;
&lt;br /&gt;
A string giving component.view (that would be in the page request, typically matching the model name) goes in the **type_alias** field.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;table&#039;&#039;&#039; gives the complete table class information for the table class stored as a JSON object in which the first element represents your &amp;quot;special&amp;quot; table and the second an optional common table (otherwise it will default the JTableCorecontent. This includes the name of the database table, the name of the primary key, the prefix, the name, an option array as used in your constructor and getInstance() methods. This enables the tagging system (and other APIs) to access your table easily. &lt;br /&gt;
&lt;br /&gt;
If you are using Joomla categories make sure to create a category type so that they can be tagged. In Joomla 3.1 and 3.1.1 there is an error where the tag field will show even if there is not a type, but this is corrected in 3.1.4.  &lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important note&#039;&#039;&#039; : Please note that the table name for the common table is #__ucm_content. This is INCORRECT in 3.1 and 3.1.1 data but is not currently used.  The data was updated in 3.1.4.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
{&amp;quot;special&amp;quot;:{&amp;quot;dbtable&amp;quot;:&amp;quot;#__content&amp;quot;,&amp;quot;key&amp;quot;:&amp;quot;id&amp;quot;,&amp;quot;type&amp;quot;:&amp;quot;Content&amp;quot;,&amp;quot;prefix&amp;quot;:&amp;quot;JTable&amp;quot;,&amp;quot;config&amp;quot;:&amp;quot;array()&amp;quot;},&amp;quot;common&amp;quot;:{&amp;quot;dbtable&amp;quot;:&amp;quot;#__ucm_content&amp;quot;,&amp;quot;key&amp;quot;:&amp;quot;ucm_id&amp;quot;,&amp;quot;type&amp;quot;:&amp;quot;Corecontent&amp;quot;,&amp;quot;prefix&amp;quot;:&amp;quot;JTable&amp;quot;,&amp;quot;config&amp;quot;:&amp;quot;array()&amp;quot;}}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;type_title&#039;&#039;&#039; field would potentially be used for display although this is not implemented currently except in the contentttype field.  Usually it should begin with an upper case letter if it is in English. See note about how to make this translatable. &lt;br /&gt;
&lt;br /&gt;
 note:&lt;br /&gt;
 To make your type names translatable add &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
COM_TAGS_CONTENT_TYPE_ + the type_title&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
in both the ini and sys.ini files. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Rules&#039;&#039;&#039;  is currently not used. It will likely be removed in favor of an asset_id for each type, but currently you can ignore this field which will be managed by JTable. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;field_mappings&#039;&#039;&#039; maps the names of specific fields in your table to a set of standard names. This mapping is stored as a JSON array with the first element mapping to the common fields and the second one mapping the other fields from the table. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Important note&#039;&#039;&#039;  The JHelperTags and JUcm APIS at 3.1.1 supported arrays for this field, but as of  3.1.4 either arrays or objects with a default of objects are supported.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&#039;{&amp;quot;common&amp;quot;:[{&amp;quot;core_content_item_id&amp;quot;:&amp;quot;id&amp;quot;,&amp;quot;core_title&amp;quot;:&amp;quot;title&amp;quot;,&amp;quot;core_state&amp;quot;:&amp;quot;published&amp;quot;,&amp;quot;core_alias&amp;quot;:&amp;quot;alias&amp;quot;,&amp;quot;core_created_time&amp;quot;:&amp;quot;created_time&amp;quot;,&amp;quot;core_modified_time&amp;quot;:&amp;quot;modified_time&amp;quot;,&amp;quot;core_body&amp;quot;:&amp;quot;description&amp;quot;, &amp;quot;core_hits&amp;quot;:&amp;quot;hits&amp;quot;,&amp;quot;core_publish_up&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_publish_down&amp;quot;:&amp;quot;null&amp;quot;,&amp;quot;core_access&amp;quot;:&amp;quot;access&amp;quot;, &amp;quot;core_params&amp;quot;:&amp;quot;params&amp;quot;, &amp;quot;core_featured&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metadata&amp;quot;:&amp;quot;metadata&amp;quot;, &amp;quot;core_language&amp;quot;:&amp;quot;language&amp;quot;, &amp;quot;core_images&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_urls&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_version&amp;quot;:&amp;quot;version&amp;quot;, &amp;quot;core_ordering&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;core_metakey&amp;quot;:&amp;quot;metakey&amp;quot;, &amp;quot;core_metadesc&amp;quot;:&amp;quot;metadesc&amp;quot;, &amp;quot;core_catid&amp;quot;:&amp;quot;parent_id&amp;quot;, &amp;quot;core_xreference&amp;quot;:&amp;quot;null&amp;quot;, &amp;quot;asset_id&amp;quot;:&amp;quot;asset_id&amp;quot;}], &amp;quot;special&amp;quot;: [{&amp;quot;parent_id&amp;quot;:&amp;quot;parent_id&amp;quot;,&amp;quot;lft&amp;quot;:&amp;quot;lft&amp;quot;,&amp;quot;rgt&amp;quot;:&amp;quot;rgt&amp;quot;,&amp;quot;level&amp;quot;:&amp;quot;level&amp;quot;,&amp;quot;path&amp;quot;:&amp;quot;path&amp;quot;,&amp;quot;extension&amp;quot;:&amp;quot;extension&amp;quot;,&amp;quot;note&amp;quot;:&amp;quot;note&amp;quot;}]}&#039;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is the field mapping for the Article type. Note that article uses the name attribs for the core field called params.  If your table does not contain a field, put “null” instead.  Leaving it blank may cause SQL issues. The special fields are optional.  At a minimum for common fields you need to map: content_item_id, alias and title in order to successfully create urls in the tagged items list.  You also will probably want: access, status, and language.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Router&#039;&#039;&#039; is an optional name of a static helper router method for this type found in its front end helpers folder. &lt;br /&gt;
If you only store data in #__ucm_content you will eventually be able to leave the router field blank although this option is not currently implemented. If you do not have a custom router tags falls back to the rules found in JHelperRoute.&lt;br /&gt;
&lt;br /&gt;
== Modify your component&#039;s table class (or classes if you have multiple tables) ==&lt;br /&gt;
Add the following to your JTable constructor:&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;_observers = new JObserverUpdater($this);&lt;br /&gt;
JObserverMapper::attachAllObservers($this);&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The addition material formerly located here is no longer needed after 3.1.4&#039;&#039;&#039; Please read the document history for this page if you are required to support 3.1.0 or 3.1.1.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;For joomla 3.2 I needed this line:&#039;&#039;&#039; ( Dont forget to replace weblinks by your component name )&lt;br /&gt;
&lt;br /&gt;
JObserverMapper::addObserverClassToClass(&#039;JTableObserverTags&#039;, &#039;WeblinksTableWeblink&#039;, array(&#039;typeAlias&#039; =&amp;gt; &#039;com_weblinks.weblink&#039;));&lt;br /&gt;
&lt;br /&gt;
== Add tags to the getItem() method of the model ==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Note: this was only required in 3.1.0 and 3.1.1. It should not be used in 3.1.4 or later.&#039;&#039;&#039; Please read the history of this page if you need instructions for older versions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add a tag field to edit screens ==&lt;br /&gt;
In any edit layouts where you want to allow tagging, you need to add the field to the xml and the appropriate layouts if necessary. The core layouts us a JLayout to manage this and the same layout by any extension.&lt;br /&gt;
&lt;br /&gt;
Update: Note that in &#039;&#039;&#039;3.1.1 only&#039;&#039;&#039; there is special handling of this in the core.  Tags in the edit screen &#039;&#039;&#039;MUST&#039;&#039;&#039; be part of a metadata &amp;lt;fields&amp;gt;&amp;lt;/fields&amp;gt; group.  The core provides two JLayouts to help you manage standard layouts, one (details) for the metadata and one for the sidebar that includes the tabs. In update an extension to 3.1.4 you may need to fix adjust your edit views.&lt;br /&gt;
&lt;br /&gt;
In 3.1.4 or later this special handling is not necessary.  Best practice is to use the standard JLayouts since this provides a consistent experience for users.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field loads all the Javascript libraries required. You don&#039;t need to worry about that.&lt;br /&gt;
&lt;br /&gt;
The field supports two modes:  &lt;br /&gt;
* &#039;&#039;&#039;Nested tags mode.&#039;&#039;&#039; Hierarchical tag list. Doesn&#039;t support on the fly tag creation.&lt;br /&gt;
* &#039;&#039;&#039;AJAX mode.&#039;&#039;&#039;  Tags are searched while user types (3 min. chars required to launch the AJAX search). Custom tags are added by pressing ENTER or COMMA keys. Tags show the global route/path. Example: &amp;lt;code&amp;gt;grandpa/parent/tag&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The field mode can be forced or use the com_tags setting &amp;lt;code&amp;gt;Tag field mode&amp;lt;/code&amp;gt; to determine its mode. To set/force the field mode we have to add &#039;&#039;&#039;mode=&amp;quot;ajax&amp;quot;&#039;&#039;&#039; or &#039;&#039;&#039;mode=&amp;quot;nested&amp;quot;&#039;&#039;&#039; to the tag field definition.  &lt;br /&gt;
&lt;br /&gt;
Example of forced AJAX mode:  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The field also includes an attribute to allow/deny the user to enter custom values. Currently this only works in AJAX mode. The attribute has to be added to the field definition like **custom=&amp;quot;allow&amp;quot;** or **custom=&amp;quot;deny&amp;quot;**  &lt;br /&gt;
&lt;br /&gt;
Example field definition with custom tags denied:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;field name=&amp;quot;tags&amp;quot; type=&amp;quot;tag&amp;quot; mode=&amp;quot;ajax&amp;quot; custom=&amp;quot;denied&amp;quot;&lt;br /&gt;
			label=&amp;quot;JTAG&amp;quot; description=&amp;quot;JTAG_DESC&amp;quot;&lt;br /&gt;
			class=&amp;quot;inputbox span12 small&amp;quot; multiple=&amp;quot;true&amp;quot;&lt;br /&gt;
		&amp;gt;&lt;br /&gt;
		&amp;lt;/field&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
if it is not already there. Usually multiple should be true unless you have a specific reason for it not to be.&lt;br /&gt;
In the core components in administrator, the field is in the group shown on the right, below the language field.&lt;br /&gt;
&lt;br /&gt;
Note: As of 3.1.2 if you wish to use the field to designate parent tags you must add parent=&amp;quot;parent&amp;quot; to the xml definition of the field.&lt;br /&gt;
&lt;br /&gt;
== Prepare the view ==&lt;br /&gt;
Add an appropriate version of this to your view.html.php file before loading the layout:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$item-&amp;gt;tags = new JHelperTags;&lt;br /&gt;
$item-&amp;gt;tags-&amp;gt;getItemTags(&#039;com_newsfeeds.newsfeed.&#039; , $this-&amp;gt;item-&amp;gt;id); &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter should match a type in the types table. The second parameter is the primary key under which this record is stored in your table. This would be used in any display in any view where you want the tags associated with the item to display.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Set up the display ==&lt;br /&gt;
&lt;br /&gt;
In any layout where you want to display the tags associated with an item add:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Changing the object and property names as appropriate.&lt;br /&gt;
&lt;br /&gt;
You will most likely want to add the show_tags parameter to the item parameters, the menu item parameters and component configuration as appropriate for your use case.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;?php if ($this-&amp;gt;params-&amp;gt;get(&#039;show_tags&#039;, 1) &amp;amp;&amp;amp; !empty($this-&amp;gt;item-&amp;gt;tags)) : ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php $this-&amp;gt;item-&amp;gt;tagLayout = new JLayoutFile(&#039;joomla.content.tags&#039;); ?&amp;gt;&lt;br /&gt;
		&amp;lt;?php echo $this-&amp;gt;item-&amp;gt;tagLayout-&amp;gt;render($this-&amp;gt;item-&amp;gt;tags-&amp;gt;itemTags); ?&amp;gt;&lt;br /&gt;
	&amp;lt;?php endif; ?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Batch processing ==&lt;br /&gt;
If you want to add the ability to do batch tagging to a backend list view do the following.&lt;br /&gt;
&lt;br /&gt;
Add tag to the default_batch layout&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
        &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;div class=&amp;quot;control-group&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;div class=&amp;quot;controls&amp;quot;&amp;gt;&lt;br /&gt;
				&amp;lt;?php echo JHtml::_(&#039;batch.tag&#039;);?&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;/div&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And add tag to the button class in the modal footer (Not totally necessary at this point, but good for potential future changes e.g. if unTag is added.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;button class=&amp;quot;btn&amp;quot; type=&amp;quot;button&amp;quot; onclick=&amp;quot;document.id(&#039;batch-category-id&#039;).value=&#039;&#039;;document.id(&#039;batch-access&#039;).value=&#039;&#039;;document.id(&#039;batch-language-id&#039;).value=&#039;&#039;;document.id(&#039;batch-user-id&#039;).value=&#039;&#039;;document.id(&#039;batch-tag-id)&#039;).value=&#039;&#039;&amp;quot; data-dismiss=&amp;quot;modal&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a batch method to your model if you are not extending JModelAdmin or overriding the batch method. ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	/**&lt;br /&gt;
	 * Batch tag a list of item.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @param   integer  $value     The value of the new tag.&lt;br /&gt;
	 * @param   array    $pks       An array of row IDs.&lt;br /&gt;
	 * @param   array    $contexts  An array of item contexts.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @return  void.&lt;br /&gt;
	 *&lt;br /&gt;
	 * @since   3.1&lt;br /&gt;
	 */&lt;br /&gt;
	protected function batchTag($value, $pks, $contexts)&lt;br /&gt;
	{&lt;br /&gt;
		$tagsHelper = new JHelperTags();&lt;br /&gt;
		$tagsHelper-&amp;gt;tagItems($value, $pks, $contexts);&lt;br /&gt;
&lt;br /&gt;
		return true;&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And modify your batch method by adding&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
		if (!empty($commands[&#039;tag&#039;]))&lt;br /&gt;
		{&lt;br /&gt;
			if (!$this-&amp;gt;batchTag($commands[&#039;tag&#039;], $pks, $contexts))&lt;br /&gt;
			{&lt;br /&gt;
				return false;&lt;br /&gt;
			}&lt;br /&gt;
&lt;br /&gt;
			$done = true;&lt;br /&gt;
		}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pay attention to any JSON encoded strings that you need special handling for in batch processing -- remember that you are saving a copy of the core fields and you need save to work as expected.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That’s it, now create some tags, tag some items and you are set.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]][[Category:Content Tags]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=103392</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=103392"/>
		<updated>2013-09-09T15:04:39Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: Re-added link to the really useful information&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version/tutor|1.5,2.5,3.1}}&lt;br /&gt;
==Useful information==&lt;br /&gt;
This tutorial is split into two independent parts:&lt;br /&gt;
* [[Inserting, Updating and Removing data using JDatabase|Inserting, updating and removing data from the database]]. It also touches on transactions.&lt;br /&gt;
* [[Selecting data using JDatabase|Selecting data from one or more tables]] and retrieving it in a variety of different forms&lt;br /&gt;
&lt;br /&gt;
Note: If you are looking for practical information about accessing the database it has been moved to the [[J1.5:Accessing_the_database_using_JDatabase|Joomla! 1.5 page here]]&lt;br /&gt;
&lt;br /&gt;
==Supported Storage Connectors==&lt;br /&gt;
&lt;br /&gt;
The table below outlines the database and storage connectors available for Joomla! as well as which version of Joomla they became available in.&lt;br /&gt;
&lt;br /&gt;
To make a connector available in Joomla&#039;s installer or global configuration manager, you will need to ensure the PHP library is installed (E.g. for PHP5 and MySQL the php5-mysql library would need to be installed).&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Database &lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Joomla Versions&lt;br /&gt;
|-&lt;br /&gt;
| MySQL || {{JVer|1.5}}{{JVer|2.5}}{{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Postgresql || {{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Microsoft SQL Server || {{JVer|2.5}}{{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Microsoft SQL Azure || {{JVer|2.5}}{{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Oracle DB || {{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| SQL Lite || {{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| PHP Data Objects (PDO)* || {{JVer|3.0}}&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
* PHP Data Objects is a database abstraction layer and is shipped with PHP 5.1+.&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Accessing_the_database_using_JDatabase&amp;diff=79811</id>
		<title>J1.5:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Accessing_the_database_using_JDatabase&amp;diff=79811"/>
		<updated>2013-01-08T10:12:16Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{version|1.5|test}}Though most of this article still works on 1.5+, some improved ways need to be specified here or in a version specific copy of this article.&amp;lt;hr/&amp;gt;&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for third party developers. This guide will help you use this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla can use different kinds of SQL database systems and run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object you need just two lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db = JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object; then we prepare the query. You can use the normal SQL syntax. The only thing you have to change is the table prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but rather insert a row into it, we need one more function. Every string value in the SQL syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This results in clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==== setQuery($query) ====&lt;br /&gt;
The &#039;&#039;setQuery($query)&#039;&#039; method sets up a database query for later execution either by the query() method or one of the Load result methods.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
The parameter $query must be a valid SQL string. It can either be added as a string parameter or as a variable. Generally a variable is preferred; it leads to more legible code and can help in debugging.&lt;br /&gt;
&lt;br /&gt;
setQuery() also takes three other parameters: $offset, $limit (both used in list pagination) and $prefix, an alternative table prefix. All three variables have default values set and can usually be ignored.&lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
==== query() ====&lt;br /&gt;
The &#039;&#039;query()&#039;&#039; method is the basic tool for executing SQL queries on a database. In Joomla it is most often used for updating or administering the database simply because the various load methods detailed on this page have the query step built into them.&lt;br /&gt;
&lt;br /&gt;
The syntax is very straightforward:&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$db = JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;query();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: $query() returns an appropriate database resource if successful, or FALSE if not.&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows()&lt;br /&gt;
* explain()&lt;br /&gt;
&lt;br /&gt;
==== insertid() ====&lt;br /&gt;
If you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the insertid() function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$query = &amp;quot;INSERT INTO &#039;#__example_table&#039; (&#039;name&#039;,&#039;email&#039;,&#039;username&#039;)&lt;br /&gt;
        VALUES (&#039;John Smith&#039;,&#039;johnsmith@domain.example&#039;,&#039;johnsmith&#039;)&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$user_id = $db-&amp;gt;insertid();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject()&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
==== loadResult() ====&lt;br /&gt;
Use &#039;&#039;&#039;loadResult()&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@domain.example || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@domain.example || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@domain.example || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db = JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db = JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@domain.example || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@domain.example || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@domain.example || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow() ====&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@domain.example [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful.&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc() ====&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssoc();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@domain.example [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful.&lt;br /&gt;
&lt;br /&gt;
==== loadObject() ====&lt;br /&gt;
loadObject returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObject();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@domain.example [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$result-&amp;gt;index // e.g. $result-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful.&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@domain.example || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@domain.example || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@domain.example || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray() ====&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT name, email, username&lt;br /&gt;
    FROM . . . &amp;quot;;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# loadResultArray() is equivalent to loadResultArray(0).&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray($index) ====&lt;br /&gt;
loadResultArray($index) returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT name, email, username&lt;br /&gt;
    FROM . . . &amp;quot;;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray(1);&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; johnsmith@domain.example [1] =&amp;gt; magda_h@domain.example [2] =&amp;gt; ydg@domain.example )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
loadResultArray($index) allows you to iterate through a series of columns in the results&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
for ( $i = 0; $i &amp;lt;= 2; $i++ ) {&lt;br /&gt;
  $column= $db-&amp;gt;loadResultArray($i);&lt;br /&gt;
  print_r($column);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&lt;br /&gt;
Array ( [0] =&amp;gt; johnsmith@domain.example [1] =&amp;gt; magda_h@domain.example [2] =&amp;gt; ydg@domain.example )&lt;br /&gt;
Array ( [0] =&amp;gt; johnsmith [1] =&amp;gt; magdah [2] =&amp;gt; ydegaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@domain.example || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@domain.example || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@domain.example || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList() ====&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@domain.example [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@domain.example [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@domain.example [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList() ====&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@domain.example [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@domain.example [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@domain.example [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;column_name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList($key) ====&lt;br /&gt;
loadAssocList(&#039;key&#039;) returns an associated array - indexed on &#039;key&#039; - of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@domain.example [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@domain.example [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@domain.example [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;][&#039;column_name&#039;] // e.g. $row[&#039;johnsmith&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList() ====&lt;br /&gt;
loadObjectList() returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@domain.example [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@domain.example [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@domain.example [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList(&#039;key&#039;) ====&lt;br /&gt;
loadObjectList($key) returns an associated array - indexed on &#039;key&#039; - of objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadObjectList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@domain.example [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@domain.example [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@domain.example [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;]-&amp;gt;column_name // e.g. $row[&#039;johnsmith&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
=== Miscellaneous Result Set Methods ===&lt;br /&gt;
==== getNumRows() ====&lt;br /&gt;
getNumRows() will return the number of result rows found by the last query and waiting to be read. To get a result from getNumRows() you have to run it &#039;&#039;&#039;after&#039;&#039;&#039; the query and &#039;&#039;&#039;before&#039;&#039;&#039; you have retrieved any results.  &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$num_rows = $db-&amp;gt;getNumRows();&lt;br /&gt;
print_r($num_rows);&lt;br /&gt;
$result = $db-&amp;gt;loadRowList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will return &amp;lt;pre&amp;gt;3&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: if you run getNumRows() after loadRowList() - or any other retrieval method - you may get a PHP Warning:&lt;br /&gt;
&amp;lt;pre&amp;gt;Warning: mysql_num_rows(): 80 is not a valid MySQL result resource &lt;br /&gt;
in D:\xampp\htdocs\joomla1.5a\libraries\joomla\database\database\mysql.php on line 344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
===Subqueries===&lt;br /&gt;
Subqueries should be written as follows:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT id FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kinds of queries are supported since MySQL 4.1. If this method fails, you can split the query into two as demonstrated below. Please note that this will (often unnecessarily) increase the load on the database server.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT id FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadResultArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Using MySQL User-Defined Variables===&lt;br /&gt;
MySQL User-Defined Variables are created and maintained for the lifespan of a connection. In Joomla terms this is usually the lifespan of a page request. They can be used in consecutive queries, retaining their value, as long as each query is inside the same connection lifespan.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$database = JFactory::getDBO();&lt;br /&gt;
$database-&amp;gt;setQuery(&amp;quot;SET @num := 0&amp;quot;);&lt;br /&gt;
$database-&amp;gt;query();&lt;br /&gt;
$database-&amp;gt;setQuery(&amp;quot;SET @num := @num + 5&amp;quot;);&lt;br /&gt;
$database-&amp;gt;query();&lt;br /&gt;
$database-&amp;gt;setQuery(&amp;quot;SELECT @num&amp;quot;);&lt;br /&gt;
$result = $localDB-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The first query defines the MySQL User-Defined Variable, the second one increments it by 5, the third one retrieves it&#039;s value and will in this case return a value of 5.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
The fact that they retain their value can be quite useful, however there is also a little risk. If you use the same User-Defined Variable, in this case @num, in another query, make sure you reset it first. If that other query runs within the same connection lifespan, @num will have remembered its value of 5. You may expect it to start at null or 0.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
For details on working with MySQL&#039;s User-Defined Variables, please refer to the MySQL documentation at [http://dev.mysql.com/doc/ http://dev.mysql.com/doc/]&lt;br /&gt;
&lt;br /&gt;
===Developer-Friendly Tips===&lt;br /&gt;
Here is a quick way to do four developer-friendly things at once:&lt;br /&gt;
* Use a simple constant as an SQL seperator (which can probably be used in many queries).&lt;br /&gt;
* Make your SQL-in-PHP code easy to read (for yourself and possibly other developers later on).&lt;br /&gt;
* Give an error inside your (component-) content without really setting debugging on.&lt;br /&gt;
* Have a visibly nice SQL by splitting SQL groups with linebreaks in your error.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDBO();&lt;br /&gt;
$jAp = JFactory::getApplication();&lt;br /&gt;
    //We define a linebreak constant&lt;br /&gt;
define(&#039;L&#039;, chr(10));&lt;br /&gt;
    //Here is the most magic&lt;br /&gt;
$db-&amp;gt;setQuery(&lt;br /&gt;
	&#039;SELECT * FROM #__table&#039;.L.&lt;br /&gt;
	&#039;WHERE something=&amp;quot;something else&amp;quot;)&#039;.L.&lt;br /&gt;
	&#039;ORDER BY date desc&#039;&lt;br /&gt;
); &lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
    //display and convert to HTML when SQL error&lt;br /&gt;
if (is_null($posts=$db-&amp;gt;loadRowList())) {$jAp-&amp;gt;enqueueMessage(nl2br($db-&amp;gt;getErrorMsg()),&#039;error&#039;); return;} &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Tutorials]][[Category:Development]][[Category:Database]]&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===See your resulting query===&lt;br /&gt;
when building an extension, if you want to see the resulting query you can use:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
echo nl2br(str_replace(&#039;#__&#039;,&#039;jos_&#039;,$query));die; //Where &#039;jos_&#039; is your database prefix &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=79810</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=79810"/>
		<updated>2013-01-08T10:10:32Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Useful information==&lt;br /&gt;
If you are looking for practical information about accessing the database it has been moved to the Joomla! 1.5 page linked below&lt;br /&gt;
 &lt;br /&gt;
{{underconstruction}}&lt;br /&gt;
&lt;br /&gt;
{{version/tutor|1.5,2.5,3.0}}{{multiversion|The &#039;&#039;&#039;{{PAGENAME}}&#039;&#039;&#039; Tutorial is available in these versions:-|version|pages=no}}&lt;br /&gt;
&lt;br /&gt;
==Supported Storage Connectors==&lt;br /&gt;
&lt;br /&gt;
The table below outlines the database and storage connectors available for Joomla! as well as which version of Joomla they became available in.&lt;br /&gt;
&lt;br /&gt;
To make a connector available in Joomla&#039;s installer or global configuration manager, you will need to ensure the PHP library is installed (E.g. for PHP5 and MySQL the php5-mysql library would need to be installed).&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Database &lt;br /&gt;
! scope=&amp;quot;col&amp;quot; | Joomla Versions&lt;br /&gt;
|-&lt;br /&gt;
| MySQL || {{JVer|1.5}}{{JVer|2.5}}{{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Postgresql || {{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Microsoft SQL Server || {{JVer|2.5}}{{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Microsoft SQL Azure || {{JVer|2.5}}{{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| Oracle DB || {{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| SQL Lite || {{JVer|3.0}}&lt;br /&gt;
|-&lt;br /&gt;
| PHP Data Objects (PDO)* || {{JVer|3.0}}&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
* PHP Data Objects is a database abstraction layer and is shipped with PHP 5.1+.&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Talk:Accessing_the_database_using_JDatabase&amp;diff=22063</id>
		<title>Talk:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Talk:Accessing_the_database_using_JDatabase&amp;diff=22063"/>
		<updated>2010-03-06T10:48:53Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* Merging the 2 suggested articles */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Can anyone provide more info regarding insertObject()?&lt;br /&gt;
I just have no idea of how to insert a row into a table. I tried to do the following but it just doesn&#039;t work:&lt;br /&gt;
&lt;br /&gt;
$db	= &amp;amp; JFactory::getDBO();&lt;br /&gt;
$query =  &amp;quot;insert into #__my_table(field1, field2) values (&#039;&amp;quot;.$value1.&amp;quot;&#039;,&#039;&amp;quot;.$value2.&amp;quot;&#039;)&amp;quot;;&lt;br /&gt;
echo $query;&lt;br /&gt;
$db-&amp;gt;setQuery( $query );&lt;br /&gt;
return($db-&amp;gt;Query());&lt;br /&gt;
&lt;br /&gt;
Any ideas?&lt;br /&gt;
&lt;br /&gt;
Thanks!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
*&lt;br /&gt;
* $object-&amp;gt;fieldname1 = &#039;value1&#039;;&lt;br /&gt;
* $object-&amp;gt;fieldname2 = &#039;value2&#039;;&lt;br /&gt;
* $object-&amp;gt;fieldname3 = &#039;value3&#039;;&lt;br /&gt;
* $object-&amp;gt;fieldname4 = &#039;value4&#039;;&lt;br /&gt;
* $object-&amp;gt;fieldname5 = &#039;value5&#039;;&lt;br /&gt;
*&lt;br /&gt;
* $table = &#039;#__tablename&#039;;&lt;br /&gt;
*&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
$db = JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;insertObject($table, $object, &#039;id&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
*&lt;br /&gt;
* The query will be generated:&lt;br /&gt;
* &lt;br /&gt;
* INSERT INTO `#__tablename` ( &#039;fieldname1&#039;, &#039;fieldname2&#039;, &#039;fieldname3&#039;, &#039;fieldname4&#039;, &#039;fieldname5&#039; ) VALUES ( &#039;value1&#039;, &#039;value2&#039;, &#039;value3&#039;, &#039;value4&#039;, &#039;value5&#039; );&lt;br /&gt;
*&lt;br /&gt;
* and executed&lt;br /&gt;
*&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
echo $object-&amp;gt;id; // will output the last insert ID&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you wanna use &amp;quot;your&amp;quot; syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDOB();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;INSERT INTO `#__tablename` ( &#039;field1&#039;, &#039;field2&#039; ) VALUES ( &#039;&amp;quot;.$value1.&amp;quot;&#039;, &#039;&amp;quot;.$value2.&amp;quot;&#039;)&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
*&lt;br /&gt;
* $db-&amp;gt;setQuery($query);&lt;br /&gt;
*&lt;br /&gt;
* if ($dump) {&lt;br /&gt;
*    echo $db-&amp;gt;getQuery();&lt;br /&gt;
* }&lt;br /&gt;
*&lt;br /&gt;
* $db-&amp;gt;Query();&lt;br /&gt;
*&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;Execute($query);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
But it&#039;s recommend to use a controller/model/table construct:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// controller&lt;br /&gt;
&lt;br /&gt;
$model = $this-&amp;gt;getModel(&#039;modelname&#039;)&lt;br /&gt;
$model-&amp;gt;store($data);&lt;br /&gt;
&lt;br /&gt;
// Model&lt;br /&gt;
&lt;br /&gt;
public function store($data) {&lt;br /&gt;
&lt;br /&gt;
    [...]&lt;br /&gt;
&lt;br /&gt;
    $row = JTable::getInstance(&#039;KomponentNameTableName&#039;);&lt;br /&gt;
&lt;br /&gt;
    //save it in the db&lt;br /&gt;
    if (!$row-&amp;gt;save($data)) {&lt;br /&gt;
        JError::raiseError( 500, $row-&amp;gt;_db-&amp;gt;getError() );&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    [...]&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// table/komponentnametablename.php&lt;br /&gt;
&lt;br /&gt;
class JTableKomponentNameTableName extends JTable {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Primary Key&lt;br /&gt;
     * @var int&lt;br /&gt;
     */&lt;br /&gt;
    var $id = null;&lt;br /&gt;
    /** @var int */&lt;br /&gt;
    var $field1 = null;&lt;br /&gt;
    /** @var char */&lt;br /&gt;
    var $field2 = null;&lt;br /&gt;
&lt;br /&gt;
    public function __construct(&amp;amp; $db) {&lt;br /&gt;
        parent::__construct(&#039;#__tablename&#039;, &#039;id&#039;, $db);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public function check () {&lt;br /&gt;
&lt;br /&gt;
        // Check all fields $this-&amp;gt;fieldname&lt;br /&gt;
        return true;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
-- [[User:Bembelimen|Bembelimen]] 12:34, 16 March 2009 (UTC)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I think, you can &#039;&#039;&#039;not&#039;&#039;&#039; repeat the call to loadAssoc to get further rows, because&lt;br /&gt;
loadAssoc runs the query unconditionally, I got a infinite recursion using it this way:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
    while($row = $db-&amp;gt;loadAssoc()){&lt;br /&gt;
    // do something...&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
See: [http://api.joomla.org/__filesource/fsource_Joomla-Framework_Database_joomladatabasedatabasemysql.php.html#a390] &lt;br /&gt;
line: 392&lt;br /&gt;
&lt;br /&gt;
== Merging the 2 suggested articles ==&lt;br /&gt;
&lt;br /&gt;
* Article 1:[[How to use the database classes in your script]]&lt;br /&gt;
* Article 2:[[Tutorial:How to use the database classes in your script]]&lt;br /&gt;
Discuss here ([[User:E-builds|E-builds]] 17:38, 11 January 2010 (UTC))&lt;br /&gt;
&lt;br /&gt;
While they have the same name one is &#039;supposed&#039; to be a reference and the other a &#039;how to&#039; tutorial. Given that I think that the two should be complementary and linked but not merged. [[User:GreyHead|GreyHead]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Talk:Accessing_the_database_using_JDatabase&amp;diff=22062</id>
		<title>Talk:Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Talk:Accessing_the_database_using_JDatabase&amp;diff=22062"/>
		<updated>2010-03-06T10:47:37Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* Merging the 2 suggested articles */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Can anyone provide more info regarding insertObject()?&lt;br /&gt;
I just have no idea of how to insert a row into a table. I tried to do the following but it just doesn&#039;t work:&lt;br /&gt;
&lt;br /&gt;
$db	= &amp;amp; JFactory::getDBO();&lt;br /&gt;
$query =  &amp;quot;insert into #__my_table(field1, field2) values (&#039;&amp;quot;.$value1.&amp;quot;&#039;,&#039;&amp;quot;.$value2.&amp;quot;&#039;)&amp;quot;;&lt;br /&gt;
echo $query;&lt;br /&gt;
$db-&amp;gt;setQuery( $query );&lt;br /&gt;
return($db-&amp;gt;Query());&lt;br /&gt;
&lt;br /&gt;
Any ideas?&lt;br /&gt;
&lt;br /&gt;
Thanks!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
*&lt;br /&gt;
* $object-&amp;gt;fieldname1 = &#039;value1&#039;;&lt;br /&gt;
* $object-&amp;gt;fieldname2 = &#039;value2&#039;;&lt;br /&gt;
* $object-&amp;gt;fieldname3 = &#039;value3&#039;;&lt;br /&gt;
* $object-&amp;gt;fieldname4 = &#039;value4&#039;;&lt;br /&gt;
* $object-&amp;gt;fieldname5 = &#039;value5&#039;;&lt;br /&gt;
*&lt;br /&gt;
* $table = &#039;#__tablename&#039;;&lt;br /&gt;
*&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
$db = JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;insertObject($table, $object, &#039;id&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
*&lt;br /&gt;
* The query will be generated:&lt;br /&gt;
* &lt;br /&gt;
* INSERT INTO `#__tablename` ( &#039;fieldname1&#039;, &#039;fieldname2&#039;, &#039;fieldname3&#039;, &#039;fieldname4&#039;, &#039;fieldname5&#039; ) VALUES ( &#039;value1&#039;, &#039;value2&#039;, &#039;value3&#039;, &#039;value4&#039;, &#039;value5&#039; );&lt;br /&gt;
*&lt;br /&gt;
* and executed&lt;br /&gt;
*&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
echo $object-&amp;gt;id; // will output the last insert ID&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you wanna use &amp;quot;your&amp;quot; syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db = JFactory::getDOB();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;INSERT INTO `#__tablename` ( &#039;field1&#039;, &#039;field2&#039; ) VALUES ( &#039;&amp;quot;.$value1.&amp;quot;&#039;, &#039;&amp;quot;.$value2.&amp;quot;&#039;)&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
*&lt;br /&gt;
* $db-&amp;gt;setQuery($query);&lt;br /&gt;
*&lt;br /&gt;
* if ($dump) {&lt;br /&gt;
*    echo $db-&amp;gt;getQuery();&lt;br /&gt;
* }&lt;br /&gt;
*&lt;br /&gt;
* $db-&amp;gt;Query();&lt;br /&gt;
*&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
$db-&amp;gt;Execute($query);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
But it&#039;s recommend to use a controller/model/table construct:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// controller&lt;br /&gt;
&lt;br /&gt;
$model = $this-&amp;gt;getModel(&#039;modelname&#039;)&lt;br /&gt;
$model-&amp;gt;store($data);&lt;br /&gt;
&lt;br /&gt;
// Model&lt;br /&gt;
&lt;br /&gt;
public function store($data) {&lt;br /&gt;
&lt;br /&gt;
    [...]&lt;br /&gt;
&lt;br /&gt;
    $row = JTable::getInstance(&#039;KomponentNameTableName&#039;);&lt;br /&gt;
&lt;br /&gt;
    //save it in the db&lt;br /&gt;
    if (!$row-&amp;gt;save($data)) {&lt;br /&gt;
        JError::raiseError( 500, $row-&amp;gt;_db-&amp;gt;getError() );&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    [...]&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// table/komponentnametablename.php&lt;br /&gt;
&lt;br /&gt;
class JTableKomponentNameTableName extends JTable {&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Primary Key&lt;br /&gt;
     * @var int&lt;br /&gt;
     */&lt;br /&gt;
    var $id = null;&lt;br /&gt;
    /** @var int */&lt;br /&gt;
    var $field1 = null;&lt;br /&gt;
    /** @var char */&lt;br /&gt;
    var $field2 = null;&lt;br /&gt;
&lt;br /&gt;
    public function __construct(&amp;amp; $db) {&lt;br /&gt;
        parent::__construct(&#039;#__tablename&#039;, &#039;id&#039;, $db);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public function check () {&lt;br /&gt;
&lt;br /&gt;
        // Check all fields $this-&amp;gt;fieldname&lt;br /&gt;
        return true;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
-- [[User:Bembelimen|Bembelimen]] 12:34, 16 March 2009 (UTC)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
I think, you can &#039;&#039;&#039;not&#039;&#039;&#039; repeat the call to loadAssoc to get further rows, because&lt;br /&gt;
loadAssoc runs the query unconditionally, I got a infinite recursion using it this way:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
    while($row = $db-&amp;gt;loadAssoc()){&lt;br /&gt;
    // do something...&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
See: [http://api.joomla.org/__filesource/fsource_Joomla-Framework_Database_joomladatabasedatabasemysql.php.html#a390] &lt;br /&gt;
line: 392&lt;br /&gt;
&lt;br /&gt;
== Merging the 2 suggested articles ==&lt;br /&gt;
&lt;br /&gt;
* Article 1:[[How to use the database classes in your script]]&lt;br /&gt;
* Article 2:[[Tutorial:How to use the database classes in your script]]&lt;br /&gt;
Discuss here ([[User:E-builds|E-builds]] 17:38, 11 January 2010 (UTC))&lt;br /&gt;
&lt;br /&gt;
While thaty have the same name one is &#039;supposed&#039; to be a reference and the other a &#039;how to&#039; tutorial. Given that I think that the two should be complementary and linked but not merged.&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=13426</id>
		<title>J1.5:Developing a MVC Component/Using the Database</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=13426"/>
		<updated>2009-03-07T16:08:57Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: Added Category:Database&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first two tutorials, we showed you how to build a simple model-view-controller component. We had one view which retrieved data from a model (which was created in the 2nd tutorial). In this tutorial, we will be working with the model. Instead of the data being hard coded in the model, the model will retrieve the data from a table in the database.&lt;br /&gt;
&lt;br /&gt;
This tutorial will demonstrate how to use the JDatabase class to retrieve data from the database.&lt;br /&gt;
&lt;br /&gt;
== Retrieving the Data ==&lt;br /&gt;
&lt;br /&gt;
Our model currently has one method: getGreeting(). This method is very simple - all it does is return the hard-coded greeting.&lt;br /&gt;
&lt;br /&gt;
To make things more interesting, we will load the greeting from a database table. We will demonstrate later how to create an SQL file and add the appropriate code to the XML manifest file so that the table and some sample data will be created when the component is installed. For now, we will simply replace our return statement with some code that will retrieve the greeting from the database and return it.&lt;br /&gt;
&lt;br /&gt;
The first step is to obtain a reference to a database object. Since Joomla! uses the database for its normal operation, a database connection already exists; therefore, it is not necessary to create your own. A reference to the existing database can be obtained using:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$db =&amp;amp; JFactory::getDBO();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JFactory is a static class that is used to retrieve references to many of the system objects. More information about this class can be found in the API documentation.&lt;br /&gt;
&lt;br /&gt;
The method name (getDBO) stands for get DataBase Object, and is easy and important to remember.&lt;br /&gt;
&lt;br /&gt;
Now that we have obtained a reference to the database object, we can retrieve our data. We do this in two steps:&lt;br /&gt;
&lt;br /&gt;
* store our query in the database object&lt;br /&gt;
* load the result&lt;br /&gt;
&lt;br /&gt;
Our new getGreeting() method will therefore look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;function getGreeting()&lt;br /&gt;
{&lt;br /&gt;
   $db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
   $query = &#039;SELECT greeting FROM #__hello&#039;;&lt;br /&gt;
   $db-&amp;gt;setQuery( $query );&lt;br /&gt;
   $greeting = $db-&amp;gt;loadResult();&lt;br /&gt;
&lt;br /&gt;
   return $greeting;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
hello is the name of the table that we will create later, and greeting is the name of the field that stores the greetings. If you are not familiar with SQL, it would be helpful to take a tutorial or a lesson to get yourself up to speed. One such tutorial can be found at [http://www.w3schools.com/sql/default.asp w3schools].&lt;br /&gt;
&lt;br /&gt;
The $db-&amp;gt;loadResult() method will execute the stored database query and return the first field of the first row of the result. See [http://api.joomla.org/Joomla-Framework/Database/JDatabase.html JDatabase API reference] for more information about other load methods in the JDatabase class.&lt;br /&gt;
&lt;br /&gt;
== Creating the Installation SQL File ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! installer has built-in support for executing queries during component installation. These queries are all stored in a standard text file.&lt;br /&gt;
&lt;br /&gt;
We will have three queries in our install file: the first will drop the table in case it already exists, the second will create the table with the appropriate fields, and the third will insert the data.&lt;br /&gt;
&lt;br /&gt;
Here are our queries:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE `#__hello` (&lt;br /&gt;
  `id` int(11) NOT NULL auto_increment,&lt;br /&gt;
  `greeting` varchar(25) NOT NULL,&lt;br /&gt;
  PRIMARY KEY  (`id`)&lt;br /&gt;
) ENGINE=MyISAM AUTO_INCREMENT DEFAULT CHARSET=utf8;&lt;br /&gt;
&lt;br /&gt;
INSERT INTO `#__hello` (`greeting`) VALUES (&#039;Hello, World!&#039;),&lt;br /&gt;
(&#039;Bonjour, Monde!&#039;),&lt;br /&gt;
(&#039;Ciao, Mondo!&#039;);&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You might find the  prefix on the table names rather odd. Joomla! will replace this prefix with the prefix used by the current install. For most installs, this table will become jos_hello. This allows multiple installs of Joomla! to use the same database, and prevents collisions with other applications using the same table names (i.e. two applications might share a database, but might both require a &#039;users&#039; table. This convention avoids problems.)&lt;br /&gt;
&lt;br /&gt;
We have specified two fields in our database. The first field is id, and is called the &#039;primary key&#039;. The primary key of a database table is a field that is used to uniquely identify a record. This is often used to lookup rows in the database. The other field is greeting. This is the field that stores the greeting that is returned from the query that we used above.&lt;br /&gt;
&lt;br /&gt;
We will save our queries in a file called install.utf.sql.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Uninstall SQL File ===&lt;br /&gt;
&lt;br /&gt;
Though we might hope that people will never want to uninstall our component, it is important that if they do, we don&#039;t leave anything behind. Joomla! will look after deleting the files and directories that were created during install, but you must manually include queries that will remove any tables that have been added to the database. Since we have only created one table, we only need one query:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will save this query in a file called uninstall.utf.sql.&lt;br /&gt;
&lt;br /&gt;
== Updating our Install File ==&lt;br /&gt;
&lt;br /&gt;
We need to change a few things in our install file. First, we need to add our two new files to the list of files to install. SQL install file have to go in the admin directory. Second, we need to tell the installer to execute the queries in our files on install and uninstall.&lt;br /&gt;
&lt;br /&gt;
Our new file looks like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;3.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;install&amp;gt;&lt;br /&gt;
  &amp;lt;sql&amp;gt;&lt;br /&gt;
   &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;install.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
  &amp;lt;/sql&amp;gt;&lt;br /&gt;
 &amp;lt;/install&amp;gt;&lt;br /&gt;
 &amp;lt;uninstall&amp;gt;&lt;br /&gt;
  &amp;lt;sql&amp;gt;&lt;br /&gt;
   &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;uninstall.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
  &amp;lt;/sql&amp;gt;&lt;br /&gt;
 &amp;lt;/uninstall&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;install.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;uninstall.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
&amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice two attributes present on the &amp;lt;file&amp;gt; tags within the &amp;lt;install&amp;gt; and &amp;lt;uninstall&amp;gt; sections: charset and driver. The charset is the type of charset to use. The only valid charset is utf8. If you want to create install files for non-utf8 databases (for older version of MySQL), you should omit this attribute.&lt;br /&gt;
&lt;br /&gt;
The driver attribute specifies which database the queries were written for. Currently, this can only be mysql, but in future versions of Joomla! there may be more database drivers available.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a component that takes advantage of both the Joomla! MVC framework classes and the JDatabase classes. You are now able to write MVC components that interact with the database and can use the Joomla! installer to create and populate database tables.&lt;br /&gt;
&lt;br /&gt;
== Other Articles in this Series ==&lt;br /&gt;
[[Developing a Model-View-Controller Component - Part 1]]&lt;br /&gt;
&lt;br /&gt;
[[Developing a Model-View-Controller Component - Part 2 - Adding a Model]]&lt;br /&gt;
&lt;br /&gt;
[[Developing a Model-View-Controller Component - Part 4 - Creating an Administrator Interface]]&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8110/29435/com_hello3_01.zip com_hello3_01]&lt;br /&gt;
[[Category:Database]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13425</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13425"/>
		<updated>2009-03-07T16:08:28Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: Added Category:Database&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla is build to be able to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, you only need 2 lines of code to get a result from the database and that in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but insert a row into it, we need one more function. Every string-value in the SQL-syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This helps writing clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==== setQuery($query) ====&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;setQuery($query)&#039;&#039; method sets up a database query for later execution either by the query() method or one of the Load result methods.  &amp;lt;pre&amp;gt;$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
The parameter $query must be a valid sql string, it can either be added as a string parameter or as a variable; generally a variable is preferred as it leads to more legible code and can help in debugging.&lt;br /&gt;
&lt;br /&gt;
setQuery() also takes three other parameters: $offset, $limit - both used in list pagination; and $prefix - an alternative table prefix. All three of these variables have default values set and can usually be ignored. &lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
&lt;br /&gt;
==== query() ====&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;query()&#039;&#039; method is the the basic tool for executing sql queries on a database. In Joomla it is most often used for updating or administering the database simple because the various load methods detail on this page have the query step built in to them.&lt;br /&gt;
&lt;br /&gt;
The syntax is very straightforward:&amp;lt;pre&amp;gt;$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;query();&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: $query() returns an appropriate database resource if successful, or FALSE if not&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows()&lt;br /&gt;
* explain()&lt;br /&gt;
* insertid()&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject()&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
&lt;br /&gt;
==== loadResult() ====&lt;br /&gt;
&lt;br /&gt;
Use &#039;&#039;&#039;loadResult()&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow() ====&lt;br /&gt;
&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc() ====&lt;br /&gt;
&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadObject() ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObject();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row-&amp;gt;index // e.g. $row-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray() ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# loadResultArray() is equivalent to loadResultArray(0)&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray($index) ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray($index) returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT name, email, username&lt;br /&gt;
    FROM . . . &amp;quot;;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray(2);&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; johnsmith@example.com [1] =&amp;gt; magda_h@example.com [2] =&amp;gt; ydg@example.com )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
loadResultArray($index) allows you to iterate through a series of columns in the results&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
for ( $i = 0; $i &amp;lt;= 2; $i++ ) {&lt;br /&gt;
  $column= $db-&amp;gt;loadResultArray($i);&lt;br /&gt;
  print_r($column);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&lt;br /&gt;
Array ( [0] =&amp;gt; johnsmith@example.com [1] =&amp;gt; magda_h@example.com [2] =&amp;gt; ydg@example.com )&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; johnsmith [1] =&amp;gt; magdah [2] =&amp;gt; ydegaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList() ====&lt;br /&gt;
&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@example.com [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@example.com [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList() ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;column_name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList($key) ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList(&#039;key&#039;) returns an associated array - indexed on &#039;key&#039; - of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;][&#039;column_name&#039;] // e.g. $row[&#039;johnsmith&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList() ====&lt;br /&gt;
&lt;br /&gt;
loadObjectList() returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList(&#039;key&#039;) ====&lt;br /&gt;
&lt;br /&gt;
loadObjectList($key) returns an associated array - indexed on &#039;key&#039; - of objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadObjectList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;]-&amp;gt;column_name // e.g. $row[&#039;johnsmith&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
=== Misc Result Set Methods ===&lt;br /&gt;
&lt;br /&gt;
==== getNumRows() ====&lt;br /&gt;
&lt;br /&gt;
getNumRows() will return the number of result rows found by the last query and waiting to be read. To get a result from getNumRows() you have to run it &#039;&#039;&#039;after&#039;&#039;&#039; the query and &#039;&#039;&#039;before&#039;&#039;&#039; you have retrieved any results.  &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$num_rows = $db-&amp;gt;getNumRows();&lt;br /&gt;
print_r($rows);&lt;br /&gt;
$result = $db-&amp;gt;loadResultList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will return &amp;lt;pre&amp;gt;3&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: if you run getNumRows() after loadResultList() - or any other retrieval method - you may get a PHP Warning:&lt;br /&gt;
&amp;lt;pre&amp;gt;Warning: mysql_num_rows(): 80 is not a valid MySQL result resource &lt;br /&gt;
in D:\xampp\htdocs\joomla1.5a\libraries\joomla\database\database\mysql.php on line 344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
We had a few people lately using sub-queries like these:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT * FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kind of queries are only possible in MySQL 4.1 and above. Another way to achieve this, is splitting the query into two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]][[Category:Database]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=How_to_determine_your_database_prefix&amp;diff=13420</id>
		<title>How to determine your database prefix</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=How_to_determine_your_database_prefix&amp;diff=13420"/>
		<updated>2009-03-07T16:05:00Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: Added Category:Database&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The database prefix can be found in the file configuration.php or in the admin panel under the menu &amp;quot;Site - Configuration File - Global Configuration - Server&amp;quot;&lt;br /&gt;
&lt;br /&gt;
[[Category:Database]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=How_to_apply_a_.sql_file_to_a_database&amp;diff=13419</id>
		<title>How to apply a .sql file to a database</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=How_to_apply_a_.sql_file_to_a_database&amp;diff=13419"/>
		<updated>2009-03-07T16:04:29Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: Added Category:Database&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Using phpMyAdmin==&lt;br /&gt;
Login to your MySQL database via phpMyAdmin [http://www.phpmyadmin.net]. Select your database. Choose the SQL tab. Enter the code lines into the text box and press Go. You will be provided with the results of the command on this screen.&lt;br /&gt;
&lt;br /&gt;
==Using MySQL Query Browser==&lt;br /&gt;
Login to your MySQL database via MySQL Query Browser [http://www.mysql.com/products/tools/query-browser/]. Double click on your database on the right hand side to select it. Create a new &amp;quot;script&amp;quot; tab and enter the code lines into the text box provided and press &amp;quot;Execute&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
==Using the command line MySQL client (advanced!)==&lt;br /&gt;
MySQL by default ships with a command line client [http://dev.mysql.com/doc/refman/5.0/en/mysql.html] that enables access to MySQL databases.&lt;br /&gt;
# Change to the directory containing a copy of the script file.  For example,&lt;br /&gt;
#:&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;cd /public_html/installation/sql/mysql&amp;lt;/source&amp;gt;&lt;br /&gt;
# Use the tool to connect to your Joomla! database and use a redirection operator (&amp;quot;&amp;lt;&amp;quot;) to take commands from the .sql file.  For example,&lt;br /&gt;
#:&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;mysql --user=name joomla_database_name &amp;lt; diff_rc3_to_rc4.sql&amp;lt;/source&amp;gt;&lt;br /&gt;
#: You will be prompted to enter the password before the command is carried out.  Do not use the &amp;quot;--password&amp;quot; option to enter your password on the command line as this is not secure (potentially anyone else logged in to your system can see the password).&lt;br /&gt;
&lt;br /&gt;
[[Category:Database]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Database_structure&amp;diff=13418</id>
		<title>J1.5:Database structure</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Database_structure&amp;diff=13418"/>
		<updated>2009-03-07T16:03:40Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: added Category:Database&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{stub}}&lt;br /&gt;
{{inuse}}&lt;br /&gt;
==Structure==&lt;br /&gt;
The J! 1.5.x database structure includes 36 Tables&lt;br /&gt;
Tables listing w/out prefix_&lt;br /&gt;
#banner&lt;br /&gt;
#bannerclient&lt;br /&gt;
#bannertrack&lt;br /&gt;
#categories&lt;br /&gt;
#components&lt;br /&gt;
#contact_details&lt;br /&gt;
#content&lt;br /&gt;
#content_frontpage&lt;br /&gt;
#content_rating&lt;br /&gt;
#core_acl_aro&lt;br /&gt;
#core_acl_aro_groups&lt;br /&gt;
#core_acl_aro_map&lt;br /&gt;
#core_acl_aro_sections&lt;br /&gt;
#core_acl_aro_groups_aro_map&lt;br /&gt;
#core_log_items&lt;br /&gt;
#core_log_searches&lt;br /&gt;
#groups&lt;br /&gt;
#menu&lt;br /&gt;
#menu_types&lt;br /&gt;
#messages&lt;br /&gt;
#messages_cfg&lt;br /&gt;
#migration_backlinks&lt;br /&gt;
#modules&lt;br /&gt;
#modules_menu&lt;br /&gt;
#newsfeeds&lt;br /&gt;
#plugins&lt;br /&gt;
#polls&lt;br /&gt;
#poll_data&lt;br /&gt;
#poll_date&lt;br /&gt;
#poll_menu&lt;br /&gt;
#sections&lt;br /&gt;
#sessions&lt;br /&gt;
#stats_agents&lt;br /&gt;
#templates_menu&lt;br /&gt;
#users&lt;br /&gt;
#weblinks&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;API table Relationships joomla 1.5&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
#Lien : http://www.torkiljohnsen.com/wp-content/uploads/2006/04/joomla_15_database_schema.png&lt;br /&gt;
&lt;br /&gt;
#[[Detailed list of fields]] [[Category:Database]]&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Connecting_to_an_external_database&amp;diff=13417</id>
		<title>Connecting to an external database</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Connecting_to_an_external_database&amp;diff=13417"/>
		<updated>2009-03-07T15:57:33Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: Added database category&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;If you need to access tables within the same database as your Joomla! installation then you can simply use the [[JFactory/getDBO|JFactory-&amp;gt;getDBO]] method.  This uses the already established connection that Joomla! uses to connect to the database.  For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$db = JFactory::getDBO();&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
$db is now an object of type [[JDatabase]] and you can perform database operations on it using the usual methods.&lt;br /&gt;
&lt;br /&gt;
But what if you want to connect to a completely different database from the one used by Joomla!?.  This might be a different database on the same machine as your Joomla! site or it might be on a different host and perhaps even require a different database driver.  Well, you can do this using the [[JDatabase/getInstance|JDatabase-&amp;gt;getInstance]] method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$option = array(); //prevent problems&lt;br /&gt;
&lt;br /&gt;
$option[&#039;driver&#039;]   = &#039;mysql&#039;;            // Database driver name&lt;br /&gt;
$option[&#039;host&#039;]     = &#039;db.myhost.com&#039;;    // Database host name&lt;br /&gt;
$option[&#039;user&#039;]     = &#039;fredbloggs&#039;;       // User for database authentication&lt;br /&gt;
$option[&#039;password&#039;] = &#039;s9(39s£h[%dkFd&#039;;   // Password for database authentication&lt;br /&gt;
$option[&#039;database&#039;] = &#039;bigdatabase&#039;;      // Database name&lt;br /&gt;
$option[&#039;prefix&#039;]   = &#039;abc_&#039;;             // Database prefix (may be empty)&lt;br /&gt;
&lt;br /&gt;
$db = &amp;amp; JDatabase::getInstance( $option );&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
$db is now an object of type [[JDatabase]] and you can perform database operations on it using the usual methods.&lt;br /&gt;
&lt;br /&gt;
Note that if the database uses a non-standard port number then this can be specified by adding it to the end of the host name.  For example, you might have your MySQL database running on port 3307 (the default is port 3306), in which case your host name might be &#039;db.myhost.com:3307&#039;.&lt;br /&gt;
&lt;br /&gt;
One feature of using [[JDatabase/getInstance|JDatabase-&amp;gt;getInstance]] is that if another call is made with the same parameters it will return the previously created object rather than creating a fresh one.&lt;br /&gt;
&lt;br /&gt;
Note, however, that the parameters must match exactly for this to happen.  For example, if two calls were made to a MySQL database using [[JDatabase/getInstance|JDatabase-&amp;gt;getInstance]], with the first using a host name of &#039;db.myhost.com&#039; and the second using &#039;db.myhost.com:3306&#039;, then two separate connections would be made, even though port 3306 is the default port for MySQL and so the parameters are logically the same.&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]] [[Category:Database]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13285</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13285"/>
		<updated>2009-02-23T20:04:48Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* loadResultArray() */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla is build to be able to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, you only need 2 lines of code to get a result from the database and that in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but insert a row into it, we need one more function. Every string-value in the SQL-syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This helps writing clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==== setQuery($query) ====&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;setQuery($query)&#039;&#039; method sets up a database query for later execution either by the query() method or one of the Load result methods.  &amp;lt;pre&amp;gt;$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
The parameter $query must be a valid sql string, it can either be added as a string parameter or as a variable; generally a variable is preferred as it leads to more legible code and can help in debugging.&lt;br /&gt;
&lt;br /&gt;
setQuery() also takes three other parameters: $offset, $limit - both used in list pagination; and $prefix - an alternative table prefix. All three of these variables have default values set and can usually be ignored. &lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
&lt;br /&gt;
==== query() ====&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;query()&#039;&#039; method is the the basic tool for executing sql queries on a database. In Joomla it is most often used for updating or administering the database simple because the various load methods detail on this page have the query step built in to them.&lt;br /&gt;
&lt;br /&gt;
The syntax is very straightforward:&amp;lt;pre&amp;gt;$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;query();&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: $query() returns an appropriate database resource if successful, or FALSE if not&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows()&lt;br /&gt;
* explain()&lt;br /&gt;
* insertid()&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject()&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
&lt;br /&gt;
==== loadResult() ====&lt;br /&gt;
&lt;br /&gt;
Use &#039;&#039;&#039;loadResult()&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow() ====&lt;br /&gt;
&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc() ====&lt;br /&gt;
&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadObject() ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObject();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row-&amp;gt;index // e.g. $row-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray() ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# loadResultArray() is equivalent to loadResultArray(0)&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray($index) ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray($index) returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT name, email, username&lt;br /&gt;
    FROM . . . &amp;quot;;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray(2);&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; johnsmith@example.com [1] =&amp;gt; magda_h@example.com [2] =&amp;gt; ydg@example.com )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
loadResultArray($index) allows you to iterate through a series of columns in the results&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
for ( $i = 0; $i &amp;lt;= 2; $i++ ) {&lt;br /&gt;
  $column= $db-&amp;gt;loadResultArray($i);&lt;br /&gt;
  print_r($column);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&lt;br /&gt;
Array ( [0] =&amp;gt; johnsmith@example.com [1] =&amp;gt; magda_h@example.com [2] =&amp;gt; ydg@example.com )&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; johnsmith [1] =&amp;gt; magdah [2] =&amp;gt; ydegaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList() ====&lt;br /&gt;
&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@example.com [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@example.com [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList() ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;column_name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList($key) ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList(&#039;key&#039;) returns an associated array - indexed on &#039;key&#039; - of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;][&#039;column_name&#039;] // e.g. $row[&#039;johnsmith&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList() ====&lt;br /&gt;
&lt;br /&gt;
loadObjectList() returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList(&#039;key&#039;) ====&lt;br /&gt;
&lt;br /&gt;
loadObjectList($key) returns an associated array - indexed on &#039;key&#039; - of objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadObjectList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;]-&amp;gt;column_name // e.g. $row[&#039;johnsmith&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
=== Misc Result Set Methods ===&lt;br /&gt;
&lt;br /&gt;
==== getNumRows() ====&lt;br /&gt;
&lt;br /&gt;
getNumRows() will return the number of result rows found by the last query and waiting to be read. To get a result from getNumRows() you have to run it &#039;&#039;&#039;after&#039;&#039;&#039; the query and &#039;&#039;&#039;before&#039;&#039;&#039; you have retrieved any results.  &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$num_rows = $db-&amp;gt;getNumRows();&lt;br /&gt;
print_r($rows);&lt;br /&gt;
$result = $db-&amp;gt;loadResultList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will return &amp;lt;pre&amp;gt;3&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: if you run getNumRows() after loadResultList() - or any other retrieval method - you may get a PHP Warning:&lt;br /&gt;
&amp;lt;pre&amp;gt;Warning: mysql_num_rows(): 80 is not a valid MySQL result resource &lt;br /&gt;
in D:\xampp\htdocs\joomla1.5a\libraries\joomla\database\database\mysql.php on line 344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
We had a few people lately using sub-queries like these:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT * FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kind of queries are only possible in MySQL 4.1 and above. Another way to achieve this, is splitting the query into two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13284</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13284"/>
		<updated>2009-02-23T19:15:27Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* loadResultArray($index) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla is build to be able to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, you only need 2 lines of code to get a result from the database and that in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but insert a row into it, we need one more function. Every string-value in the SQL-syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This helps writing clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==== setQuery($query) ====&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;setQuery($query)&#039;&#039; method sets up a database query for later execution either by the query() method or one of the Load result methods.  &amp;lt;pre&amp;gt;$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
The parameter $query must be a valid sql string, it can either be added as a string parameter or as a variable; generally a variable is preferred as it leads to more legible code and can help in debugging.&lt;br /&gt;
&lt;br /&gt;
setQuery() also takes three other parameters: $offset, $limit - both used in list pagination; and $prefix - an alternative table prefix. All three of these variables have default values set and can usually be ignored. &lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
&lt;br /&gt;
==== query() ====&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;query()&#039;&#039; method is the the basic tool for executing sql queries on a database. In Joomla it is most often used for updating or administering the database simple because the various load methods detail on this page have the query step built in to them.&lt;br /&gt;
&lt;br /&gt;
The syntax is very straightforward:&amp;lt;pre&amp;gt;$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;query();&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: $query() returns an appropriate database resource if successful, or FALSE if not&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows()&lt;br /&gt;
* explain()&lt;br /&gt;
* insertid()&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject()&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
&lt;br /&gt;
==== loadResult() ====&lt;br /&gt;
&lt;br /&gt;
Use &#039;&#039;&#039;loadResult()&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow() ====&lt;br /&gt;
&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc() ====&lt;br /&gt;
&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadObject() ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObject();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row-&amp;gt;index // e.g. $row-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray() ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further columns, loadResultArray($index) might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray($index) ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray($index) returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT name, email, username&lt;br /&gt;
    FROM . . . &amp;quot;;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray(2);&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; johnsmith@example.com [1] =&amp;gt; magda_h@example.com [2] =&amp;gt; ydg@example.com )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
loadResultArray($index) allows you to iterate through a series of columns in the results&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
for ( $i = 0; $i &amp;lt;= 2; $i++ ) {&lt;br /&gt;
  $column= $db-&amp;gt;loadResultArray($i);&lt;br /&gt;
  print_r($column);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&lt;br /&gt;
Array ( [0] =&amp;gt; johnsmith@example.com [1] =&amp;gt; magda_h@example.com [2] =&amp;gt; ydg@example.com )&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; johnsmith [1] =&amp;gt; magdah [2] =&amp;gt; ydegaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList() ====&lt;br /&gt;
&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@example.com [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@example.com [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList() ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;column_name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList($key) ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList(&#039;key&#039;) returns an associated array - indexed on &#039;key&#039; - of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;][&#039;column_name&#039;] // e.g. $row[&#039;johnsmith&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList() ====&lt;br /&gt;
&lt;br /&gt;
loadObjectList() returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList(&#039;key&#039;) ====&lt;br /&gt;
&lt;br /&gt;
loadObjectList($key) returns an associated array - indexed on &#039;key&#039; - of objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadObjectList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;]-&amp;gt;column_name // e.g. $row[&#039;johnsmith&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
=== Misc Result Set Methods ===&lt;br /&gt;
&lt;br /&gt;
==== getNumRows() ====&lt;br /&gt;
&lt;br /&gt;
getNumRows() will return the number of result rows found by the last query and waiting to be read. To get a result from getNumRows() you have to run it &#039;&#039;&#039;after&#039;&#039;&#039; the query and &#039;&#039;&#039;before&#039;&#039;&#039; you have retrieved any results.  &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$num_rows = $db-&amp;gt;getNumRows();&lt;br /&gt;
print_r($rows);&lt;br /&gt;
$result = $db-&amp;gt;loadResultList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will return &amp;lt;pre&amp;gt;3&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: if you run getNumRows() after loadResultList() - or any other retrieval method - you may get a PHP Warning:&lt;br /&gt;
&amp;lt;pre&amp;gt;Warning: mysql_num_rows(): 80 is not a valid MySQL result resource &lt;br /&gt;
in D:\xampp\htdocs\joomla1.5a\libraries\joomla\database\database\mysql.php on line 344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
We had a few people lately using sub-queries like these:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT * FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kind of queries are only possible in MySQL 4.1 and above. Another way to achieve this, is splitting the query into two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13283</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13283"/>
		<updated>2009-02-23T19:12:41Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* Single Column Results */  added loadResultArray($index)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla is build to be able to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, you only need 2 lines of code to get a result from the database and that in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but insert a row into it, we need one more function. Every string-value in the SQL-syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This helps writing clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==== setQuery($query) ====&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;setQuery($query)&#039;&#039; method sets up a database query for later execution either by the query() method or one of the Load result methods.  &amp;lt;pre&amp;gt;$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
The parameter $query must be a valid sql string, it can either be added as a string parameter or as a variable; generally a variable is preferred as it leads to more legible code and can help in debugging.&lt;br /&gt;
&lt;br /&gt;
setQuery() also takes three other parameters: $offset, $limit - both used in list pagination; and $prefix - an alternative table prefix. All three of these variables have default values set and can usually be ignored. &lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
&lt;br /&gt;
==== query() ====&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;query()&#039;&#039; method is the the basic tool for executing sql queries on a database. In Joomla it is most often used for updating or administering the database simple because the various load methods detail on this page have the query step built in to them.&lt;br /&gt;
&lt;br /&gt;
The syntax is very straightforward:&amp;lt;pre&amp;gt;$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;query();&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: $query() returns an appropriate database resource if successful, or FALSE if not&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows()&lt;br /&gt;
* explain()&lt;br /&gt;
* insertid()&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject()&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
&lt;br /&gt;
==== loadResult() ====&lt;br /&gt;
&lt;br /&gt;
Use &#039;&#039;&#039;loadResult()&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow() ====&lt;br /&gt;
&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc() ====&lt;br /&gt;
&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadObject() ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObject();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row-&amp;gt;index // e.g. $row-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray() ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further columns, loadResultArray($index) might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray($index) ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray($index) returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT name, email, username&lt;br /&gt;
    FROM . . .&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray(2);&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; johnsmith@example.com [1] =&amp;gt; magda_h@example.com [2] =&amp;gt; ydg@example.com )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
loadResultArray($index) allows you to iterate through a series of columns in the results&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
for ( $i = 0; $i &amp;lt;= 2; $i++ ) {&lt;br /&gt;
  $column= $db-&amp;gt;loadResultArray($i);&lt;br /&gt;
  print_r($column);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&lt;br /&gt;
Array ( [0] =&amp;gt; johnsmith@example.com [1] =&amp;gt; magda_h@example.com [2] =&amp;gt; ydg@example.com )&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; johnsmith [1] =&amp;gt; magdah [2] =&amp;gt; ydegaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList() ====&lt;br /&gt;
&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@example.com [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@example.com [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList() ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;column_name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList($key) ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList(&#039;key&#039;) returns an associated array - indexed on &#039;key&#039; - of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;][&#039;column_name&#039;] // e.g. $row[&#039;johnsmith&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList() ====&lt;br /&gt;
&lt;br /&gt;
loadObjectList() returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList(&#039;key&#039;) ====&lt;br /&gt;
&lt;br /&gt;
loadObjectList($key) returns an associated array - indexed on &#039;key&#039; - of objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadObjectList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;]-&amp;gt;column_name // e.g. $row[&#039;johnsmith&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
=== Misc Result Set Methods ===&lt;br /&gt;
&lt;br /&gt;
==== getNumRows() ====&lt;br /&gt;
&lt;br /&gt;
getNumRows() will return the number of result rows found by the last query and waiting to be read. To get a result from getNumRows() you have to run it &#039;&#039;&#039;after&#039;&#039;&#039; the query and &#039;&#039;&#039;before&#039;&#039;&#039; you have retrieved any results.  &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$num_rows = $db-&amp;gt;getNumRows();&lt;br /&gt;
print_r($rows);&lt;br /&gt;
$result = $db-&amp;gt;loadResultList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will return &amp;lt;pre&amp;gt;3&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: if you run getNumRows() after loadResultList() - or any other retrieval method - you may get a PHP Warning:&lt;br /&gt;
&amp;lt;pre&amp;gt;Warning: mysql_num_rows(): 80 is not a valid MySQL result resource &lt;br /&gt;
in D:\xampp\htdocs\joomla1.5a\libraries\joomla\database\database\mysql.php on line 344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
We had a few people lately using sub-queries like these:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT * FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kind of queries are only possible in MySQL 4.1 and above. Another way to achieve this, is splitting the query into two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13143</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13143"/>
		<updated>2009-02-10T16:05:38Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: Moved $setQuery and added () to method titles&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla is build to be able to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, you only need 2 lines of code to get a result from the database and that in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but insert a row into it, we need one more function. Every string-value in the SQL-syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This helps writing clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==== setQuery($query) ====&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;setQuery($query)&#039;&#039; method sets up a database query for later execution either by the query() method or one of the Load result methods.  &amp;lt;pre&amp;gt;$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
The parameter $query must be a valid sql string, it can either be added as a string parameter or as a variable; generally a variable is preferred as it leads to more legible code and can help in debugging.&lt;br /&gt;
&lt;br /&gt;
setQuery() also takes three other parameters: $offset, $limit - both used in list pagination; and $prefix - an alternative table prefix. All three of these variables have default values set and can usually be ignored. &lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
&lt;br /&gt;
==== query() ====&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;query()&#039;&#039; method is the the basic tool for executing sql queries on a database. In Joomla it is most often used for updating or administering the database simple because the various load methods detail on this page have the query step built in to them.&lt;br /&gt;
&lt;br /&gt;
The syntax is very straightforward:&amp;lt;pre&amp;gt;$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;query();&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: $query() returns an appropriate database resource if successful, or FALSE if not&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows()&lt;br /&gt;
* explain()&lt;br /&gt;
* insertid()&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject()&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
&lt;br /&gt;
==== loadResult() ====&lt;br /&gt;
&lt;br /&gt;
Use &#039;&#039;&#039;loadResult()&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow() ====&lt;br /&gt;
&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc() ====&lt;br /&gt;
&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadObject() ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row-&amp;gt;index // e.g. $row-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray() ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further columns, a different query with one of the functions that returns multiple columns might be more useful&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList() ====&lt;br /&gt;
&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@example.com [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@example.com [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList() ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;column_name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList($key) ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList(&#039;key&#039;) returns an associated array - indexed on &#039;key&#039; - of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;][&#039;column_name&#039;] // e.g. $row[&#039;johnsmith&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList() ====&lt;br /&gt;
&lt;br /&gt;
loadObjectList() returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList(&#039;key&#039;) ====&lt;br /&gt;
&lt;br /&gt;
loadObjectList($key) returns an associated array - indexed on &#039;key&#039; - of objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadObjectList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;]-&amp;gt;column_name // e.g. $row[&#039;johnsmith&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
=== Misc Result Set Methods ===&lt;br /&gt;
&lt;br /&gt;
==== getNumRows() ====&lt;br /&gt;
&lt;br /&gt;
getNumRows() will return the number of result rows found by the last query and waiting to be read. To get a result from getNumRows() you have to run it &#039;&#039;&#039;after&#039;&#039;&#039; the query and &#039;&#039;&#039;before&#039;&#039;&#039; you have retrieved any results.  &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$num_rows = $db-&amp;gt;getNumRows();&lt;br /&gt;
print_r($rows);&lt;br /&gt;
$result = $db-&amp;gt;loadResultList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will return &amp;lt;pre&amp;gt;3&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: if you run getNumRows() after loadResultList() - or any other retrieval method - you may get a PHP Warning:&lt;br /&gt;
&amp;lt;pre&amp;gt;Warning: mysql_num_rows(): 80 is not a valid MySQL result resource &lt;br /&gt;
in D:\xampp\htdocs\joomla1.5a\libraries\joomla\database\database\mysql.php on line 344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
We had a few people lately using sub-queries like these:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT * FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kind of queries are only possible in MySQL 4.1 and above. Another way to achieve this, is splitting the query into two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13142</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13142"/>
		<updated>2009-02-10T16:02:09Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* Basic Query Execution */  added setQuery content&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla is build to be able to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, you only need 2 lines of code to get a result from the database and that in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but insert a row into it, we need one more function. Every string-value in the SQL-syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This helps writing clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
&lt;br /&gt;
==== setQuery($query) ====&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;setQuery($query)&#039;&#039; method sets up a database query for later execution either by the query() method or one of the Load result methods.  &amp;lt;pre&amp;gt;$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
The parameter $query must be a valid sql string, it can either be added as a string parameter or as a variable; generally a variable is preferred as it leads to more legible code and can help in debugging.&lt;br /&gt;
&lt;br /&gt;
setQuery() also takes three other parameters: $offset, $limit - both used in list pagination; and $prefix - an alternative table prefix. All three of these variables have default values set and can usually be ignored.  &lt;br /&gt;
&lt;br /&gt;
==== query() ====&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;query()&#039;&#039; method is the the basic tool for executing sql queries on a database. In Joomla it is most often used for updating or administering the database simple because the various load methods detail on this page have the query step built in to them.&lt;br /&gt;
&lt;br /&gt;
The syntax is very straightforward:&amp;lt;pre&amp;gt;$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;query();&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: $query() returns an appropriate database resource if successful, or FALSE if not&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows&lt;br /&gt;
* explain&lt;br /&gt;
* insertid&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
==== loadResult ====&lt;br /&gt;
&lt;br /&gt;
Use &#039;&#039;&#039;loadResult&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow ====&lt;br /&gt;
&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc ====&lt;br /&gt;
&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadObject ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row-&amp;gt;index // e.g. $row-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further columns, a different query with one of the functions that returns multiple columns might be more useful&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList ====&lt;br /&gt;
&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@example.com [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@example.com [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList() ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;column_name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList($key) ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList(&#039;key&#039;) returns an associated array - indexed on &#039;key&#039; - of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;][&#039;column_name&#039;] // e.g. $row[&#039;johnsmith&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList(&#039;key&#039;) ====&lt;br /&gt;
&lt;br /&gt;
loadObjectList($key) returns an associated array - indexed on &#039;key&#039; - of objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadObjectList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;]-&amp;gt;column_name // e.g. $row[&#039;johnsmith&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
=== Misc Result Set Methods ===&lt;br /&gt;
&lt;br /&gt;
==== getNumRows ====&lt;br /&gt;
&lt;br /&gt;
getNumRows() will return the number of result rows found by the last query and waiting to be read. To get a result from getNumRows() you have to run it &#039;&#039;&#039;after&#039;&#039;&#039; the query and &#039;&#039;&#039;before&#039;&#039;&#039; you have retrieved any results.  &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$num_rows = $db-&amp;gt;getNumRows();&lt;br /&gt;
print_r($rows);&lt;br /&gt;
$result = $db-&amp;gt;loadResultList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will return &amp;lt;pre&amp;gt;3&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: if you run getNumRows() after loadResultList() - or any other retrieval method - you may get a PHP Warning:&lt;br /&gt;
&amp;lt;pre&amp;gt;Warning: mysql_num_rows(): 80 is not a valid MySQL result resource &lt;br /&gt;
in D:\xampp\htdocs\joomla1.5a\libraries\joomla\database\database\mysql.php on line 344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
We had a few people lately using sub-queries like these:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT * FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kind of queries are only possible in MySQL 4.1 and above. Another way to achieve this, is splitting the query into two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13141</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13141"/>
		<updated>2009-02-10T15:52:31Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* Basic Query Execution */  added content&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla is build to be able to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, you only need 2 lines of code to get a result from the database and that in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but insert a row into it, we need one more function. Every string-value in the SQL-syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This helps writing clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
&lt;br /&gt;
==== query() ====&lt;br /&gt;
&lt;br /&gt;
The query() method is the the basic tool for executing sql queries on a database. In Joomla it is most often used for updating or administering the database simple because the various load methods detail on this page have the query step built in to them.&lt;br /&gt;
&lt;br /&gt;
The syntax is very straightforward:&amp;lt;pre&amp;gt;$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows&lt;br /&gt;
* explain&lt;br /&gt;
* insertid&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
==== loadResult ====&lt;br /&gt;
&lt;br /&gt;
Use &#039;&#039;&#039;loadResult&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow ====&lt;br /&gt;
&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc ====&lt;br /&gt;
&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadObject ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row-&amp;gt;index // e.g. $row-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further columns, a different query with one of the functions that returns multiple columns might be more useful&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList ====&lt;br /&gt;
&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@example.com [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@example.com [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList() ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;column_name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList($key) ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList(&#039;key&#039;) returns an associated array - indexed on &#039;key&#039; - of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;][&#039;column_name&#039;] // e.g. $row[&#039;johnsmith&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList(&#039;key&#039;) ====&lt;br /&gt;
&lt;br /&gt;
loadObjectList($key) returns an associated array - indexed on &#039;key&#039; - of objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadObjectList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;]-&amp;gt;column_name // e.g. $row[&#039;johnsmith&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
=== Misc Result Set Methods ===&lt;br /&gt;
&lt;br /&gt;
==== getNumRows ====&lt;br /&gt;
&lt;br /&gt;
getNumRows() will return the number of result rows found by the last query and waiting to be read. To get a result from getNumRows() you have to run it &#039;&#039;&#039;after&#039;&#039;&#039; the query and &#039;&#039;&#039;before&#039;&#039;&#039; you have retrieved any results.  &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$num_rows = $db-&amp;gt;getNumRows();&lt;br /&gt;
print_r($rows);&lt;br /&gt;
$result = $db-&amp;gt;loadResultList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will return &amp;lt;pre&amp;gt;3&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: if you run getNumRows() after loadResultList() - or any other retrieval method - you may get a PHP Warning:&lt;br /&gt;
&amp;lt;pre&amp;gt;Warning: mysql_num_rows(): 80 is not a valid MySQL result resource &lt;br /&gt;
in D:\xampp\htdocs\joomla1.5a\libraries\joomla\database\database\mysql.php on line 344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
We had a few people lately using sub-queries like these:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT * FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kind of queries are only possible in MySQL 4.1 and above. Another way to achieve this, is splitting the query into two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13105</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13105"/>
		<updated>2009-02-07T16:18:25Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* loadObjectList(&amp;#039;key&amp;#039;) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla is build to be able to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, you only need 2 lines of code to get a result from the database and that in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but insert a row into it, we need one more function. Every string-value in the SQL-syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This helps writing clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
* query&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows&lt;br /&gt;
* explain&lt;br /&gt;
* insertid&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
==== loadResult ====&lt;br /&gt;
&lt;br /&gt;
Use &#039;&#039;&#039;loadResult&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow ====&lt;br /&gt;
&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc ====&lt;br /&gt;
&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadObject ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row-&amp;gt;index // e.g. $row-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further columns, a different query with one of the functions that returns multiple columns might be more useful&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList ====&lt;br /&gt;
&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@example.com [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@example.com [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList() ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;column_name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList($key) ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList(&#039;key&#039;) returns an associated array - indexed on &#039;key&#039; - of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;][&#039;column_name&#039;] // e.g. $row[&#039;johnsmith&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList(&#039;key&#039;) ====&lt;br /&gt;
&lt;br /&gt;
loadObjectList($key) returns an associated array - indexed on &#039;key&#039; - of objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadObjectList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;]-&amp;gt;column_name // e.g. $row[&#039;johnsmith&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
=== Misc Result Set Methods ===&lt;br /&gt;
&lt;br /&gt;
==== getNumRows ====&lt;br /&gt;
&lt;br /&gt;
getNumRows() will return the number of result rows found by the last query and waiting to be read. To get a result from getNumRows() you have to run it &#039;&#039;&#039;after&#039;&#039;&#039; the query and &#039;&#039;&#039;before&#039;&#039;&#039; you have retrieved any results.  &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$num_rows = $db-&amp;gt;getNumRows();&lt;br /&gt;
print_r($rows);&lt;br /&gt;
$result = $db-&amp;gt;loadResultList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will return &amp;lt;pre&amp;gt;3&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: if you run getNumRows() after loadResultList() - or any other retrieval method - you may get a PHP Warning:&lt;br /&gt;
&amp;lt;pre&amp;gt;Warning: mysql_num_rows(): 80 is not a valid MySQL result resource &lt;br /&gt;
in D:\xampp\htdocs\joomla1.5a\libraries\joomla\database\database\mysql.php on line 344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
We had a few people lately using sub-queries like these:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT * FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kind of queries are only possible in MySQL 4.1 and above. Another way to achieve this, is splitting the query into two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13104</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13104"/>
		<updated>2009-02-07T16:17:53Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* loadObjectList(&amp;#039;key&amp;#039;) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla is build to be able to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, you only need 2 lines of code to get a result from the database and that in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but insert a row into it, we need one more function. Every string-value in the SQL-syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This helps writing clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
* query&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows&lt;br /&gt;
* explain&lt;br /&gt;
* insertid&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
==== loadResult ====&lt;br /&gt;
&lt;br /&gt;
Use &#039;&#039;&#039;loadResult&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow ====&lt;br /&gt;
&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc ====&lt;br /&gt;
&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadObject ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row-&amp;gt;index // e.g. $row-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further columns, a different query with one of the functions that returns multiple columns might be more useful&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList ====&lt;br /&gt;
&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@example.com [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@example.com [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList() ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;column_name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList($key) ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList(&#039;key&#039;) returns an associated array - indexed on &#039;key&#039; - of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;][&#039;column_name&#039;] // e.g. $row[&#039;johnsmith&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList(&#039;key&#039;) ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList($key) returns an associated array - indexed on &#039;key&#039; - of objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadObjectList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;]-&amp;gt;column_name // e.g. $row[&#039;johnsmith&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
=== Misc Result Set Methods ===&lt;br /&gt;
&lt;br /&gt;
==== getNumRows ====&lt;br /&gt;
&lt;br /&gt;
getNumRows() will return the number of result rows found by the last query and waiting to be read. To get a result from getNumRows() you have to run it &#039;&#039;&#039;after&#039;&#039;&#039; the query and &#039;&#039;&#039;before&#039;&#039;&#039; you have retrieved any results.  &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$num_rows = $db-&amp;gt;getNumRows();&lt;br /&gt;
print_r($rows);&lt;br /&gt;
$result = $db-&amp;gt;loadResultList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will return &amp;lt;pre&amp;gt;3&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: if you run getNumRows() after loadResultList() - or any other retrieval method - you may get a PHP Warning:&lt;br /&gt;
&amp;lt;pre&amp;gt;Warning: mysql_num_rows(): 80 is not a valid MySQL result resource &lt;br /&gt;
in D:\xampp\htdocs\joomla1.5a\libraries\joomla\database\database\mysql.php on line 344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
We had a few people lately using sub-queries like these:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT * FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kind of queries are only possible in MySQL 4.1 and above. Another way to achieve this, is splitting the query into two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13103</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13103"/>
		<updated>2009-02-07T16:17:15Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* loadObjectList */ added loadObjectList(&amp;#039;key&amp;#039;)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla is build to be able to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, you only need 2 lines of code to get a result from the database and that in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but insert a row into it, we need one more function. Every string-value in the SQL-syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This helps writing clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
* query&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows&lt;br /&gt;
* explain&lt;br /&gt;
* insertid&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
==== loadResult ====&lt;br /&gt;
&lt;br /&gt;
Use &#039;&#039;&#039;loadResult&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow ====&lt;br /&gt;
&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc ====&lt;br /&gt;
&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadObject ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row-&amp;gt;index // e.g. $row-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further columns, a different query with one of the functions that returns multiple columns might be more useful&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList ====&lt;br /&gt;
&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@example.com [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@example.com [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList() ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;column_name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList($key) ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList(&#039;key&#039;) returns an associated array - indexed on &#039;key&#039; - of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;][&#039;column_name&#039;] // e.g. $row[&#039;johnsmith&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList(&#039;key&#039;) ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList($key) returns an associated array - indexed on &#039;key&#039; - of objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadObjectList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;]-&amp;gt;column_name // e.g. $row[&#039;johnsmith&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
=== Misc Result Set Methods ===&lt;br /&gt;
&lt;br /&gt;
==== getNumRows ====&lt;br /&gt;
&lt;br /&gt;
getNumRows() will return the number of result rows found by the last query and waiting to be read. To get a result from getNumRows() you have to run it &#039;&#039;&#039;after&#039;&#039;&#039; the query and &#039;&#039;&#039;before&#039;&#039;&#039; you have retrieved any results.  &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$num_rows = $db-&amp;gt;getNumRows();&lt;br /&gt;
print_r($rows);&lt;br /&gt;
$result = $db-&amp;gt;loadResultList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will return &amp;lt;pre&amp;gt;3&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: if you run getNumRows() after loadResultList() - or any other retrieval method - you may get a PHP Warning:&lt;br /&gt;
&amp;lt;pre&amp;gt;Warning: mysql_num_rows(): 80 is not a valid MySQL result resource &lt;br /&gt;
in D:\xampp\htdocs\joomla1.5a\libraries\joomla\database\database\mysql.php on line 344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
We had a few people lately using sub-queries like these:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT * FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kind of queries are only possible in MySQL 4.1 and above. Another way to achieve this, is splitting the query into two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13102</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13102"/>
		<updated>2009-02-07T16:12:12Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* loadAssocList */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla is build to be able to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, you only need 2 lines of code to get a result from the database and that in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but insert a row into it, we need one more function. Every string-value in the SQL-syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This helps writing clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
* query&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows&lt;br /&gt;
* explain&lt;br /&gt;
* insertid&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
==== loadResult ====&lt;br /&gt;
&lt;br /&gt;
Use &#039;&#039;&#039;loadResult&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow ====&lt;br /&gt;
&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc ====&lt;br /&gt;
&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadObject ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row-&amp;gt;index // e.g. $row-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further columns, a different query with one of the functions that returns multiple columns might be more useful&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList ====&lt;br /&gt;
&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@example.com [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@example.com [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList() ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;column_name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList($key) ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList(&#039;key&#039;) returns an associated array - indexed on &#039;key&#039; - of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;][&#039;column_name&#039;] // e.g. $row[&#039;johnsmith&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Misc Result Set Methods ===&lt;br /&gt;
&lt;br /&gt;
==== getNumRows ====&lt;br /&gt;
&lt;br /&gt;
getNumRows() will return the number of result rows found by the last query and waiting to be read. To get a result from getNumRows() you have to run it &#039;&#039;&#039;after&#039;&#039;&#039; the query and &#039;&#039;&#039;before&#039;&#039;&#039; you have retrieved any results.  &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$num_rows = $db-&amp;gt;getNumRows();&lt;br /&gt;
print_r($rows);&lt;br /&gt;
$result = $db-&amp;gt;loadResultList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will return &amp;lt;pre&amp;gt;3&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: if you run getNumRows() after loadResultList() - or any other retrieval method - you may get a PHP Warning:&lt;br /&gt;
&amp;lt;pre&amp;gt;Warning: mysql_num_rows(): 80 is not a valid MySQL result resource &lt;br /&gt;
in D:\xampp\htdocs\joomla1.5a\libraries\joomla\database\database\mysql.php on line 344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
We had a few people lately using sub-queries like these:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT * FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kind of queries are only possible in MySQL 4.1 and above. Another way to achieve this, is splitting the query into two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13101</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13101"/>
		<updated>2009-02-07T15:55:05Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* getNumRows */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla is build to be able to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, you only need 2 lines of code to get a result from the database and that in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but insert a row into it, we need one more function. Every string-value in the SQL-syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This helps writing clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
* query&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows&lt;br /&gt;
* explain&lt;br /&gt;
* insertid&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
==== loadResult ====&lt;br /&gt;
&lt;br /&gt;
Use &#039;&#039;&#039;loadResult&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow ====&lt;br /&gt;
&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc ====&lt;br /&gt;
&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadObject ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row-&amp;gt;index // e.g. $row-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further columns, a different query with one of the functions that returns multiple columns might be more useful&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList ====&lt;br /&gt;
&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@example.com [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@example.com [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Misc Result Set Methods ===&lt;br /&gt;
&lt;br /&gt;
==== getNumRows ====&lt;br /&gt;
&lt;br /&gt;
getNumRows() will return the number of result rows found by the last query and waiting to be read. To get a result from getNumRows() you have to run it &#039;&#039;&#039;after&#039;&#039;&#039; the query and &#039;&#039;&#039;before&#039;&#039;&#039; you have retrieved any results.  &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$num_rows = $db-&amp;gt;getNumRows();&lt;br /&gt;
print_r($rows);&lt;br /&gt;
$result = $db-&amp;gt;loadResultList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will return &amp;lt;pre&amp;gt;3&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: if you run getNumRows() after loadResultList() - or any other retrieval method - you may get a PHP Warning:&lt;br /&gt;
&amp;lt;pre&amp;gt;Warning: mysql_num_rows(): 80 is not a valid MySQL result resource &lt;br /&gt;
in D:\xampp\htdocs\joomla1.5a\libraries\joomla\database\database\mysql.php on line 344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
We had a few people lately using sub-queries like these:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT * FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kind of queries are only possible in MySQL 4.1 and above. Another way to achieve this, is splitting the query into two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13100</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13100"/>
		<updated>2009-02-07T15:53:40Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* Misc Result Set Methods */ added content&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla is build to be able to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, you only need 2 lines of code to get a result from the database and that in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but insert a row into it, we need one more function. Every string-value in the SQL-syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This helps writing clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
* query&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows&lt;br /&gt;
* explain&lt;br /&gt;
* insertid&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
==== loadResult ====&lt;br /&gt;
&lt;br /&gt;
Use &#039;&#039;&#039;loadResult&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow ====&lt;br /&gt;
&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc ====&lt;br /&gt;
&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadObject ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row-&amp;gt;index // e.g. $row-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further columns, a different query with one of the functions that returns multiple columns might be more useful&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList ====&lt;br /&gt;
&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@example.com [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@example.com [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Misc Result Set Methods ===&lt;br /&gt;
&lt;br /&gt;
==== getNumRows ====&lt;br /&gt;
&lt;br /&gt;
getNumRows() will return the number of result rows found and waiting to be read by the last query. To get a result from getNumRows() you have to run it &#039;&#039;&#039;after&#039;&#039;&#039; the query and &#039;&#039;&#039;before&#039;&#039;&#039; you have retrieved any results.  &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$num_rows = $db-&amp;gt;getNumRows();&lt;br /&gt;
print_r($rows);&lt;br /&gt;
$result = $db-&amp;gt;loadResultList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will return &amp;lt;pre&amp;gt;3&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: if you run getNumRows() after loadResultList() - or any other retrieval method - you may get a PHP Warning:&lt;br /&gt;
&amp;lt;pre&amp;gt;Warning: mysql_num_rows(): 80 is not a valid MySQL result resource &lt;br /&gt;
in D:\xampp\htdocs\joomla1.5a\libraries\joomla\database\database\mysql.php on line 344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
We had a few people lately using sub-queries like these:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT * FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kind of queries are only possible in MySQL 4.1 and above. Another way to achieve this, is splitting the query into two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13090</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13090"/>
		<updated>2009-02-07T08:48:47Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* loadObjectList */ updated results&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla is build to be able to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, you only need 2 lines of code to get a result from the database and that in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but insert a row into it, we need one more function. Every string-value in the SQL-syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This helps writing clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
* query&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows&lt;br /&gt;
* explain&lt;br /&gt;
* insertid&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
==== loadResult ====&lt;br /&gt;
&lt;br /&gt;
Use &#039;&#039;&#039;loadResult&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow ====&lt;br /&gt;
&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc ====&lt;br /&gt;
&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadObject ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row-&amp;gt;index // e.g. $row-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further columns, a different query with one of the functions that returns multiple columns might be more useful&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList ====&lt;br /&gt;
&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@example.com [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@example.com [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Misc Result Set Methods ===&lt;br /&gt;
* getNumRows&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
We had a few people lately using sub-queries like these:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT * FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kind of queries are only possible in MySQL 4.1 and above. Another way to achieve this, is splitting the query into two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13089</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13089"/>
		<updated>2009-02-07T08:47:08Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* loadAssocList */ updated results&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla is build to be able to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, you only need 2 lines of code to get a result from the database and that in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but insert a row into it, we need one more function. Every string-value in the SQL-syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This helps writing clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
* query&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows&lt;br /&gt;
* explain&lt;br /&gt;
* insertid&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
==== loadResult ====&lt;br /&gt;
&lt;br /&gt;
Use &#039;&#039;&#039;loadResult&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow ====&lt;br /&gt;
&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc ====&lt;br /&gt;
&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadObject ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row-&amp;gt;index // e.g. $row-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further columns, a different query with one of the functions that returns multiple columns might be more useful&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList ====&lt;br /&gt;
&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@example.com [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@example.com [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;array(&lt;br /&gt;
[0] =&amp;gt; stdObject([&#039;id&#039;] =&amp;gt; &#039;0&#039;, [&#039;name&#039;] =&amp;gt; &#039;John Smith&#039;, [&#039;email&#039;] =&amp;gt; &#039;johnsmith@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;johnsmith&#039;)&lt;br /&gt;
[1] =&amp;gt; stdObject(([&#039;id&#039;] =&amp;gt; &#039;1&#039;, [&#039;name&#039;] =&amp;gt; &#039;Magda Hellman&#039;, [&#039;email&#039;] =&amp;gt; &#039;magda_h@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;magdah&#039;)&lt;br /&gt;
[2] =&amp;gt; stdObject([&#039;id&#039;] =&amp;gt; &#039;2&#039;, [&#039;name&#039;] =&amp;gt; &#039;Yvonne de Gaulle &#039;, [&#039;email&#039;] =&amp;gt; &#039;ydg@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;ydegaulle&#039;)&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Misc Result Set Methods ===&lt;br /&gt;
* getNumRows&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
We had a few people lately using sub-queries like these:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT * FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kind of queries are only possible in MySQL 4.1 and above. Another way to achieve this, is splitting the query into two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13088</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13088"/>
		<updated>2009-02-07T08:45:21Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* loadRowList */  updated results&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla is build to be able to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, you only need 2 lines of code to get a result from the database and that in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but insert a row into it, we need one more function. Every string-value in the SQL-syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This helps writing clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
* query&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows&lt;br /&gt;
* explain&lt;br /&gt;
* insertid&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
==== loadResult ====&lt;br /&gt;
&lt;br /&gt;
Use &#039;&#039;&#039;loadResult&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow ====&lt;br /&gt;
&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc ====&lt;br /&gt;
&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadObject ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row-&amp;gt;index // e.g. $row-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further columns, a different query with one of the functions that returns multiple columns might be more useful&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList ====&lt;br /&gt;
&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@example.com [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@example.com [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;array(&lt;br /&gt;
[0] =&amp;gt; array([&#039;id&#039;] =&amp;gt; &#039;0&#039;, [&#039;name&#039;] =&amp;gt; &#039;John Smith&#039;, [&#039;email&#039;] =&amp;gt; &#039;johnsmith@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;johnsmith&#039;)&lt;br /&gt;
[1] =&amp;gt; array([&#039;id&#039;] =&amp;gt; &#039;1&#039;, [&#039;name&#039;] =&amp;gt; &#039;Magda Hellman&#039;, [&#039;email&#039;] =&amp;gt; &#039;magda_h@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;magdah&#039;)&lt;br /&gt;
[2] =&amp;gt; array([&#039;id&#039;] =&amp;gt; &#039;2&#039;, [&#039;name&#039;] =&amp;gt; &#039;Yvonne de Gaulle &#039;, [&#039;email&#039;] =&amp;gt; &#039;ydg@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;ydegaulle&#039;)&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;array(&lt;br /&gt;
[0] =&amp;gt; stdObject([&#039;id&#039;] =&amp;gt; &#039;0&#039;, [&#039;name&#039;] =&amp;gt; &#039;John Smith&#039;, [&#039;email&#039;] =&amp;gt; &#039;johnsmith@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;johnsmith&#039;)&lt;br /&gt;
[1] =&amp;gt; stdObject(([&#039;id&#039;] =&amp;gt; &#039;1&#039;, [&#039;name&#039;] =&amp;gt; &#039;Magda Hellman&#039;, [&#039;email&#039;] =&amp;gt; &#039;magda_h@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;magdah&#039;)&lt;br /&gt;
[2] =&amp;gt; stdObject([&#039;id&#039;] =&amp;gt; &#039;2&#039;, [&#039;name&#039;] =&amp;gt; &#039;Yvonne de Gaulle &#039;, [&#039;email&#039;] =&amp;gt; &#039;ydg@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;ydegaulle&#039;)&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Misc Result Set Methods ===&lt;br /&gt;
* getNumRows&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
We had a few people lately using sub-queries like these:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT * FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kind of queries are only possible in MySQL 4.1 and above. Another way to achieve this, is splitting the query into two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13087</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13087"/>
		<updated>2009-02-07T08:43:14Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* loadResultArray */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla is build to be able to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, you only need 2 lines of code to get a result from the database and that in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but insert a row into it, we need one more function. Every string-value in the SQL-syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This helps writing clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
* query&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows&lt;br /&gt;
* explain&lt;br /&gt;
* insertid&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
==== loadResult ====&lt;br /&gt;
&lt;br /&gt;
Use &#039;&#039;&#039;loadResult&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow ====&lt;br /&gt;
&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc ====&lt;br /&gt;
&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadObject ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row-&amp;gt;index // e.g. $row-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further columns, a different query with one of the functions that returns multiple columns might be more useful&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList ====&lt;br /&gt;
&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;array(&lt;br /&gt;
[0] =&amp;gt; array([0] =&amp;gt; &#039;0&#039;, [1] =&amp;gt; &#039;John Smith&#039;, [2] =&amp;gt; &#039;johnsmith@example.com&#039;, [3] =&amp;gt; &#039;johnsmith&#039;)&lt;br /&gt;
[1] =&amp;gt; array([0] =&amp;gt; &#039;1&#039;, [1] =&amp;gt; &#039;Magda Hellman&#039;, [2] =&amp;gt; &#039;magda_h@example.com&#039;, [3] =&amp;gt; &#039;magdah&#039;)&lt;br /&gt;
[2] =&amp;gt; array([0] =&amp;gt; &#039;2&#039;, [1] =&amp;gt; &#039;Yvonne de Gaulle &#039;, [2] =&amp;gt; &#039;ydg@example.com&#039;, [3] =&amp;gt; &#039;ydegaulle&#039;)&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;array(&lt;br /&gt;
[0] =&amp;gt; array([&#039;id&#039;] =&amp;gt; &#039;0&#039;, [&#039;name&#039;] =&amp;gt; &#039;John Smith&#039;, [&#039;email&#039;] =&amp;gt; &#039;johnsmith@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;johnsmith&#039;)&lt;br /&gt;
[1] =&amp;gt; array([&#039;id&#039;] =&amp;gt; &#039;1&#039;, [&#039;name&#039;] =&amp;gt; &#039;Magda Hellman&#039;, [&#039;email&#039;] =&amp;gt; &#039;magda_h@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;magdah&#039;)&lt;br /&gt;
[2] =&amp;gt; array([&#039;id&#039;] =&amp;gt; &#039;2&#039;, [&#039;name&#039;] =&amp;gt; &#039;Yvonne de Gaulle &#039;, [&#039;email&#039;] =&amp;gt; &#039;ydg@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;ydegaulle&#039;)&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;array(&lt;br /&gt;
[0] =&amp;gt; stdObject([&#039;id&#039;] =&amp;gt; &#039;0&#039;, [&#039;name&#039;] =&amp;gt; &#039;John Smith&#039;, [&#039;email&#039;] =&amp;gt; &#039;johnsmith@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;johnsmith&#039;)&lt;br /&gt;
[1] =&amp;gt; stdObject(([&#039;id&#039;] =&amp;gt; &#039;1&#039;, [&#039;name&#039;] =&amp;gt; &#039;Magda Hellman&#039;, [&#039;email&#039;] =&amp;gt; &#039;magda_h@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;magdah&#039;)&lt;br /&gt;
[2] =&amp;gt; stdObject([&#039;id&#039;] =&amp;gt; &#039;2&#039;, [&#039;name&#039;] =&amp;gt; &#039;Yvonne de Gaulle &#039;, [&#039;email&#039;] =&amp;gt; &#039;ydg@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;ydegaulle&#039;)&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Misc Result Set Methods ===&lt;br /&gt;
* getNumRows&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
We had a few people lately using sub-queries like these:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT * FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kind of queries are only possible in MySQL 4.1 and above. Another way to achieve this, is splitting the query into two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13086</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=13086"/>
		<updated>2009-02-07T08:42:31Z</updated>

		<summary type="html">&lt;p&gt;GreyHead: /* loadResultArray */ updated results&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla is build to be able to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, you only need 2 lines of code to get a result from the database and that in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but insert a row into it, we need one more function. Every string-value in the SQL-syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This helps writing clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
* query&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows&lt;br /&gt;
* explain&lt;br /&gt;
* insertid&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
==== loadResult ====&lt;br /&gt;
&lt;br /&gt;
Use &#039;&#039;&#039;loadResult&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow ====&lt;br /&gt;
&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc ====&lt;br /&gt;
&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadObject ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row-&amp;gt;index // e.g. $row-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further columns, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList ====&lt;br /&gt;
&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;array(&lt;br /&gt;
[0] =&amp;gt; array([0] =&amp;gt; &#039;0&#039;, [1] =&amp;gt; &#039;John Smith&#039;, [2] =&amp;gt; &#039;johnsmith@example.com&#039;, [3] =&amp;gt; &#039;johnsmith&#039;)&lt;br /&gt;
[1] =&amp;gt; array([0] =&amp;gt; &#039;1&#039;, [1] =&amp;gt; &#039;Magda Hellman&#039;, [2] =&amp;gt; &#039;magda_h@example.com&#039;, [3] =&amp;gt; &#039;magdah&#039;)&lt;br /&gt;
[2] =&amp;gt; array([0] =&amp;gt; &#039;2&#039;, [1] =&amp;gt; &#039;Yvonne de Gaulle &#039;, [2] =&amp;gt; &#039;ydg@example.com&#039;, [3] =&amp;gt; &#039;ydegaulle&#039;)&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;array(&lt;br /&gt;
[0] =&amp;gt; array([&#039;id&#039;] =&amp;gt; &#039;0&#039;, [&#039;name&#039;] =&amp;gt; &#039;John Smith&#039;, [&#039;email&#039;] =&amp;gt; &#039;johnsmith@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;johnsmith&#039;)&lt;br /&gt;
[1] =&amp;gt; array([&#039;id&#039;] =&amp;gt; &#039;1&#039;, [&#039;name&#039;] =&amp;gt; &#039;Magda Hellman&#039;, [&#039;email&#039;] =&amp;gt; &#039;magda_h@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;magdah&#039;)&lt;br /&gt;
[2] =&amp;gt; array([&#039;id&#039;] =&amp;gt; &#039;2&#039;, [&#039;name&#039;] =&amp;gt; &#039;Yvonne de Gaulle &#039;, [&#039;email&#039;] =&amp;gt; &#039;ydg@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;ydegaulle&#039;)&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList ====&lt;br /&gt;
&lt;br /&gt;
loadRow returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;array(&lt;br /&gt;
[0] =&amp;gt; stdObject([&#039;id&#039;] =&amp;gt; &#039;0&#039;, [&#039;name&#039;] =&amp;gt; &#039;John Smith&#039;, [&#039;email&#039;] =&amp;gt; &#039;johnsmith@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;johnsmith&#039;)&lt;br /&gt;
[1] =&amp;gt; stdObject(([&#039;id&#039;] =&amp;gt; &#039;1&#039;, [&#039;name&#039;] =&amp;gt; &#039;Magda Hellman&#039;, [&#039;email&#039;] =&amp;gt; &#039;magda_h@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;magdah&#039;)&lt;br /&gt;
[2] =&amp;gt; stdObject([&#039;id&#039;] =&amp;gt; &#039;2&#039;, [&#039;name&#039;] =&amp;gt; &#039;Yvonne de Gaulle &#039;, [&#039;email&#039;] =&amp;gt; &#039;ydg@example.com&#039;, [&#039;username&#039;] =&amp;gt; &#039;ydegaulle&#039;)&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Misc Result Set Methods ===&lt;br /&gt;
* getNumRows&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
We had a few people lately using sub-queries like these:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT * FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kind of queries are only possible in MySQL 4.1 and above. Another way to achieve this, is splitting the query into two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>GreyHead</name></author>
	</entry>
</feed>