<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Power by Result Search &#187; joomla</title>
	<atom:link href="http://www.result-search.com/sty/category/technology/joomla/feed" rel="self" type="application/rss+xml" />
	<link>http://www.result-search.com/sty</link>
	<description>Just another weblog</description>
	<lastBuildDate>Wed, 23 Jun 2010 03:12:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Developing a Model-View-Controller Component &#8211; Part 4 &#8211; Creating an Administrator Interface</title>
		<link>http://www.result-search.com/sty/2009/07/22/developing-a-model-view-controller-component-part-4-creating-an-administrator-interface.html</link>
		<comments>http://www.result-search.com/sty/2009/07/22/developing-a-model-view-controller-component-part-4-creating-an-administrator-interface.html#comments</comments>
		<pubDate>Wed, 22 Jul 2009 06:37:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[joomla]]></category>

		<guid isPermaLink="false">http://www.yaaahaaa.com/?p=943</guid>
		<description><![CDATA[Introduction
In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In this tutorial, we will develop an administrator section for our component which will make [...]]]></description>
			<content:encoded><![CDATA[<h2><span class="mw-headline">Introduction</span></h2>
<p>In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In this tutorial, we will develop an administrator section for our component which will make it possible to manage the entries in the database.<br />
<span id="more-943"></span><br />
<a id="Creating_the_Basic_Framework" name="Creating_the_Basic_Framework"></a></p>
<h2><span class="mw-headline">Creating the Basic Framework</span></h2>
<p>The basic framework of the administrator panel is very similar to the site portion. The main entry point for the administrator section of the component is hello.php. This file is identical to the hello.php file that was used in the site portion except the name of the controller it loads will be changed to HellosController. The default controller is also called controller.php and this file is identical to the default controller in the site portion, with the exception that the controller is named HellosController instead of HelloController. This difference is so that JController will by default load the hellos view, which will display a list of our greetings.</p>
<p>Here is the listing for hello.php:</p>
<div dir="ltr">
<pre class="source-php"><span class="kw2">&lt;?php</span>
<span class="coMULTI">/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4
 * @license    GNU/GPL
*/</span>

<span class="co1">// No direct access</span>

<span class="kw3">defined</span><span class="br0">(</span> <span class="st0">'_JEXEC'</span> <span class="br0">)</span> or <span class="kw3">die</span><span class="br0">(</span> <span class="st0">'Restricted access'</span> <span class="br0">)</span>;

<span class="co1">// Require the base controller</span>

<span class="kw1">require_once</span><span class="br0">(</span> JPATH_COMPONENT.DS.<span class="st0">'controller.php'</span> <span class="br0">)</span>;

<span class="co1">// Require specific controller if requested</span>
<span class="kw1">if</span><span class="br0">(</span><span class="re0">$controller</span> = JRequest::<span class="me2">getWord</span><span class="br0">(</span><span class="st0">'controller'</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span>
    <span class="re0">$path</span> = JPATH_COMPONENT.DS.<span class="st0">'controllers'</span>.DS.<span class="re0">$controller</span>.<span class="st0">'.php'</span>;
    <span class="kw1">if</span> <span class="br0">(</span><span class="kw3">file_exists</span><span class="br0">(</span><span class="re0">$path</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span>
        <span class="kw1">require_once</span> <span class="re0">$path</span>;
    <span class="br0">}</span> <span class="kw1">else</span> <span class="br0">{</span>
        <span class="re0">$controller</span> = <span class="st0">''</span>;
    <span class="br0">}</span>
<span class="br0">}</span>

<span class="co1">// Create the controller</span>
<span class="re0">$classname</span>    = <span class="st0">'HellosController'</span>.<span class="re0">$controller</span>;
<span class="re0">$controller</span>   = <span class="kw2">new</span> <span class="re0">$classname</span><span class="br0">(</span> <span class="br0">)</span>;

<span class="co1">// Perform the Request task</span>
<span class="re0">$controller</span>-&gt;<span class="me1">execute</span><span class="br0">(</span> JRequest::<span class="me2">getVar</span><span class="br0">(</span> <span class="st0">'task'</span> <span class="br0">)</span> <span class="br0">)</span>;

<span class="co1">// Redirect if set by the controller</span>
<span class="re0">$controller</span>-&gt;<span class="me1">redirect</span><span class="br0">(</span><span class="br0">)</span>;</pre>
</div>
<p>The view and model that we will start with is the hellos view and the hellos model. We will start with the model.</p>
<p><a id="The_Hellos_Model" name="The_Hellos_Model"></a></p>
<h4><span class="mw-headline">The Hellos Model</span></h4>
<p>The Hellos Model will be very simple. The only operation that we currently need is the ability to retrieve the list of hellos from the database. This operation will be implemented in a method called getData().</p>
<p>The JModel class has a built in protected method called _getList(). This method can be used to simplify the task of retrieving a list of records from the database. We simply need to pass it the query and it will return the list of records.</p>
<p>At a later point in time, we might want to use our query from within another method. Therefore, we will create a private method called _buildQuery() which will return the query that will be passed to _getList(). This makes it easier to change the query as well since it is localized in one place.</p>
<p>Therefore we need two methods in our class: getData() and _buildQuery().</p>
<p>_buildQuery() simply returns the query. It looks like:</p>
<div dir="ltr">
<pre class="source-php"><span class="coMULTI">/**
 * Returns the query
 * @return string The query to be used to retrieve the rows from the database
 */</span>
<span class="kw2">function</span> _buildQuery<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="re0">$query</span> = <span class="st0">' SELECT * '</span>
           . <span class="st0">' FROM #__hello '</span>
    ;

    <span class="kw1">return</span> <span class="re0">$query</span>;
<span class="br0">}</span></pre>
</div>
<p>getData() will obtain the query and retrieve the records from the database. Now it might happen that we need to retrieve this list of data twice in one page load. It would be a waste to have to query the database twice. Therefore, we will have this method store the data in a protected property so that on subsequent requests it can simply return the data it has already retrieved. This property will be called _data.</p>
<p>Here is the getData() method:</p>
<div dir="ltr">
<pre class="source-php"><span class="coMULTI">/**
 * Retrieves the hello data
 * @return array Array of objects containing the data from the database
 */</span>
<span class="kw2">function</span> getData<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="co1">// Lets load the data if it doesn't already exist</span>
    <span class="kw1">if</span> <span class="br0">(</span><span class="kw3">empty</span><span class="br0">(</span> <span class="re0">$this</span>-&gt;_data <span class="br0">)</span><span class="br0">)</span>
    <span class="br0">{</span>
        <span class="re0">$query</span> = <span class="re0">$this</span>-&gt;_buildQuery<span class="br0">(</span><span class="br0">)</span>;
        <span class="re0">$this</span>-&gt;_data = <span class="re0">$this</span>-&gt;_getList<span class="br0">(</span> <span class="re0">$query</span> <span class="br0">)</span>;
    <span class="br0">}</span>

    <span class="kw1">return</span> <span class="re0">$this</span>-&gt;_data;
<span class="br0">}</span></pre>
</div>
<p>The completed model looks like:</p>
<div dir="ltr">
<pre class="source-php"><span class="kw2">&lt;?php</span>
<span class="coMULTI">/**
 * Hellos Model for Hello World Component
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4
 * @license        GNU/GPL
 */</span>

<span class="co1">// Check to ensure this file is included in Joomla!</span>
<span class="kw3">defined</span><span class="br0">(</span><span class="st0">'_JEXEC'</span><span class="br0">)</span> or <span class="kw3">die</span><span class="br0">(</span><span class="br0">)</span>;

jimport<span class="br0">(</span> <span class="st0">'joomla.application.component.model'</span> <span class="br0">)</span>;

<span class="coMULTI">/**
 * Hello Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */</span>
<span class="kw2">class</span> HellosModelHellos <span class="kw2">extends</span> JModel
<span class="br0">{</span>
    <span class="coMULTI">/**
     * Hellos data array
     *
     * @var array
     */</span>
    <span class="kw2">var</span> <span class="re0">$_data</span>;

    <span class="coMULTI">/**
     * Returns the query
     * @return string The query to be used to retrieve the rows from the database
     */</span>
    <span class="kw2">function</span> _buildQuery<span class="br0">(</span><span class="br0">)</span>
    <span class="br0">{</span>
        <span class="re0">$query</span> = <span class="st0">' SELECT * '</span>
            . <span class="st0">' FROM #__hello '</span>
        ;
        <span class="kw1">return</span> <span class="re0">$query</span>;
    <span class="br0">}</span>

    <span class="coMULTI">/**
     * Retrieves the hello data
     * @return array Array of objects containing the data from the database
     */</span>
    <span class="kw2">function</span> getData<span class="br0">(</span><span class="br0">)</span>
    <span class="br0">{</span>
        <span class="co1">// Lets load the data if it doesn't already exist</span>
        <span class="kw1">if</span> <span class="br0">(</span><span class="kw3">empty</span><span class="br0">(</span> <span class="re0">$this</span>-&gt;_data <span class="br0">)</span><span class="br0">)</span>
        <span class="br0">{</span>
            <span class="re0">$query</span> = <span class="re0">$this</span>-&gt;_buildQuery<span class="br0">(</span><span class="br0">)</span>;
            <span class="re0">$this</span>-&gt;_data = <span class="re0">$this</span>-&gt;_getList<span class="br0">(</span> <span class="re0">$query</span> <span class="br0">)</span>;
        <span class="br0">}</span>

        <span class="kw1">return</span> <span class="re0">$this</span>-&gt;_data;
    <span class="br0">}</span>
<span class="br0">}</span></pre>
</div>
<p>This file is saved as models/hellos.php.</p>
<p><a id="The_Hellos_View" name="The_Hellos_View"></a></p>
<h4><span class="mw-headline">The Hellos View</span></h4>
<p>Now that we have a model to retrieve our data, we need to display it. This view will be fairly similar to the view from the site section as well.</p>
<p>Just as our model was automatically instantiated in the site, so it is in the administrator. Methods that start with get in the model can be accessed using the get() method of the JView class. So our view has three lines: one to retrieve the data from the model, one to push the data into the template, and one to invoke the display method to display the output. Thus we have:</p>
<div dir="ltr">
<pre class="source-php"><span class="kw2">&lt;?php</span>
<span class="coMULTI">/**
 * Hellos View for Hello World Component
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4
 * @license        GNU/GPL
 */</span>

<span class="co1">// Check to ensure this file is included in Joomla!</span>
<span class="kw3">defined</span><span class="br0">(</span><span class="st0">'_JEXEC'</span><span class="br0">)</span> or <span class="kw3">die</span><span class="br0">(</span><span class="br0">)</span>;

jimport<span class="br0">(</span> <span class="st0">'joomla.application.component.view'</span> <span class="br0">)</span>;

<span class="coMULTI">/**
 * Hellos View
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */</span>
<span class="kw2">class</span> HellosViewHellos <span class="kw2">extends</span> JView
<span class="br0">{</span>
    <span class="coMULTI">/**
     * Hellos view display method
     * @return void
     **/</span>
    <span class="kw2">function</span> display<span class="br0">(</span><span class="re0">$tpl</span> = <span class="kw2">null</span><span class="br0">)</span>
    <span class="br0">{</span>
        JToolBarHelper::<span class="me2">title</span><span class="br0">(</span> JText::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'Hello Manager'</span> <span class="br0">)</span>, <span class="st0">'generic.png'</span> <span class="br0">)</span>;
        JToolBarHelper::<span class="me2">deleteList</span><span class="br0">(</span><span class="br0">)</span>;
        JToolBarHelper::<span class="me2">editListX</span><span class="br0">(</span><span class="br0">)</span>;
        JToolBarHelper::<span class="me2">addNewX</span><span class="br0">(</span><span class="br0">)</span>;

        <span class="co1">// Get data from the model</span>
        <span class="re0">$items</span> =&amp; <span class="re0">$this</span>-&gt;<span class="me1">get</span><span class="br0">(</span> <span class="st0">'Data'</span><span class="br0">)</span>;

        <span class="re0">$this</span>-&gt;<span class="me1">assignRef</span><span class="br0">(</span> <span class="st0">'items'</span>, <span class="re0">$items</span> <span class="br0">)</span>;

        parent::<span class="me2">display</span><span class="br0">(</span><span class="re0">$tpl</span><span class="br0">)</span>;
    <span class="br0">}</span>
<span class="br0">}</span></pre>
</div>
<p>This file is saved as views/hellos/view.html.php.</p>
<p><a id="The_Hellos_Template" name="The_Hellos_Template"></a></p>
<h4><span class="mw-headline">The Hellos Template</span></h4>
<p>The template will take the data pushed into it from the view and produce the output. We will display our output in a simple table. While the frontend template was very simple, in the administrator we will need a minimal amount of extra logic to handle looping through the data.</p>
<p>Here is our template:</p>
<div dir="ltr">
<pre class="source-php"><span class="kw2">&lt;?php</span> <span class="kw3">defined</span><span class="br0">(</span><span class="st0">'_JEXEC'</span><span class="br0">)</span> or <span class="kw3">die</span><span class="br0">(</span><span class="st0">'Restricted access'</span><span class="br0">)</span>; <span class="kw2">?&gt;</span>
&lt;form action=<span class="st0">"index.php"</span> method=<span class="st0">"post"</span> name=<span class="st0">"adminForm"</span>&gt;
&lt;div id=<span class="st0">"editcell"</span>&gt;
    &lt;table <span class="kw2">class</span>=<span class="st0">"adminlist"</span>&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th width=<span class="st0">"5"</span>&gt;
                <span class="kw2">&lt;?php</span> <span class="kw3">echo</span> JText::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'ID'</span> <span class="br0">)</span>; <span class="kw2">?&gt;</span>
            &lt;/th&gt;
            &lt;th&gt;
                <span class="kw2">&lt;?php</span> <span class="kw3">echo</span> JText::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'Greeting'</span> <span class="br0">)</span>; <span class="kw2">?&gt;</span>
            &lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    <span class="kw2">&lt;?php</span>
    <span class="re0">$k</span> = <span class="nu0">0</span>;
    <span class="kw1">for</span> <span class="br0">(</span><span class="re0">$i</span>=<span class="nu0">0</span>, <span class="re0">$n</span>=<span class="kw3">count</span><span class="br0">(</span> <span class="re0">$this</span>-&gt;<span class="me1">items</span> <span class="br0">)</span>; <span class="re0">$i</span> &lt; <span class="re0">$n</span>; <span class="re0">$i</span>++<span class="br0">)</span>
    <span class="br0">{</span>
        <span class="re0">$row</span> =&amp; <span class="re0">$this</span>-&gt;<span class="me1">items</span><span class="br0">[</span><span class="re0">$i</span><span class="br0">]</span>;
        <span class="kw2">?&gt;</span>
        &lt;tr <span class="kw2">class</span>=<span class="st0">"&lt;?php echo "</span>row<span class="re0">$k</span><span class="st0">"; ?&gt;"</span>&gt;
            &lt;td&gt;
                <span class="kw2">&lt;?php</span> <span class="kw3">echo</span> <span class="re0">$row</span>-&gt;<span class="me1">id</span>; <span class="kw2">?&gt;</span>
            &lt;/td&gt;
            &lt;td&gt;
                <span class="kw2">&lt;?php</span> <span class="kw3">echo</span> <span class="re0">$row</span>-&gt;<span class="me1">greeting</span>; <span class="kw2">?&gt;</span>
            &lt;/td&gt;
        &lt;/tr&gt;
        <span class="kw2">&lt;?php</span>
        <span class="re0">$k</span> = <span class="nu0">1</span> - <span class="re0">$k</span>;
    <span class="br0">}</span>
    <span class="kw2">?&gt;</span>
    &lt;/table&gt;
&lt;/div&gt;

&lt;input type=<span class="st0">"hidden"</span> name=<span class="st0">"option"</span> value=<span class="st0">"com_hello"</span> /&gt;
&lt;input type=<span class="st0">"hidden"</span> name=<span class="st0">"task"</span> value=<span class="st0">""</span> /&gt;
&lt;input type=<span class="st0">"hidden"</span> name=<span class="st0">"boxchecked"</span> value=<span class="st0">"0"</span> /&gt;
&lt;input type=<span class="st0">"hidden"</span> name=<span class="st0">"controller"</span> value=<span class="st0">"hello"</span> /&gt;

&lt;/form&gt;</pre>
</div>
<p>This template is saved as views/hellos/tmpl/default.php.</p>
<p>You will notice that our output is enclosed in a form. Though this is not necessary now, it will be soon.</p>
<p>We have now completed the basic part of the first view. We have added five files to the admin section of our component:</p>
<ul>
<li>hello.php</li>
<li>controller.php</li>
<li>models/hellos.php</li>
<li>views/hellos/view.html.php</li>
<li>views/hellos/tmpl/default.php</li>
</ul>
<p>You can now add these files to the XML install file and give it a try!</p>
<p><a id="Adding_Functionality" name="Adding_Functionality"></a></p>
<h2><span class="mw-headline">Adding Functionality</span></h2>
<p>So far our administrator section is pretty useless. It doesn&#8217;t really do anything &#8211; all it does is display the entries that we have in our database.</p>
<p>In order to make it useful, we need to add some buttons and links.</p>
<p><a id="The_Toolbar" name="The_Toolbar"></a></p>
<h4><span class="mw-headline">The Toolbar</span></h4>
<p>You may have noticed the toolbar that appears at the top of other Joomla! component administrator panels. Our component needs one as well. Joomla! makes this very easy to do. We will add buttons Delete records, Edit records, and create New records. We will also add a title that will be displayed on our toolbar.</p>
<p>This is done by adding code to the view. To add the buttons, we use static methods from the Joomla! JToolBarHelper class. The code looks like:</p>
<div dir="ltr">
<pre class="source-php">JToolBarHelper::<span class="me2">title</span><span class="br0">(</span>   JText::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'Hello Manager'</span> <span class="br0">)</span>, <span class="st0">'generic.png'</span> <span class="br0">)</span>;
JToolBarHelper::<span class="me2">deleteList</span><span class="br0">(</span><span class="br0">)</span>;
JToolBarHelper::<span class="me2">editListX</span><span class="br0">(</span><span class="br0">)</span>;
JToolBarHelper::<span class="me2">addNewX</span><span class="br0">(</span><span class="br0">)</span>;</pre>
</div>
<p>These three methods will create the appropriate buttons. The deleteList() method can optionally take up to three parameters &#8211; the first parameter is a string to display to the user to confirm that they want to delete the records. The second is the task that should be sent with the query (the default is &#8216;remove&#8217;), and the third is the text that should be displayed below the button.</p>
<p>The editListX() and addNewX() methods can each take two optional parameters. The first is the task (which are by default edit and add, respectively), and the second is the text that should be displayed below the button.</p>
<ul>
<li>You may have noticed the use of the JText::_ method in the template before and here as well. This is a handy function that makes component translation much easier. The JText::_ method will look up the string in your component language file and return the translated string. If no translation text is found, it will return the string that you passed to it. If you want to translate your component into another language, all you have to do is create a language file that will map the strings within the quotes to the translated version of the string.</li>
</ul>
<p><a id="Checkboxes_and_Links" name="Checkboxes_and_Links"></a></p>
<h4><span class="mw-headline">Checkboxes and Links</span></h4>
<p>We now have buttons. Two of those buttons operate on existing records. But how do we know which records to operate on? We have to let the user tell us. To do this, we need to add checkboxes to our table so that the user can select certain records. This is done in our template.</p>
<p>In order to the add the checkboxes, we need to add an extra column into our table. We will add the column in between the two that we already have.</p>
<p>In the header of the column, we will add a checkbox which can be used to toggle all the boxes below it on or off:</p>
<div dir="ltr">
<pre class="source-php">&lt;th width=<span class="st0">"20"</span>&gt;
    &lt;input type=<span class="st0">"checkbox"</span> name=<span class="st0">"toggle"</span> value=<span class="st0">""</span> onclick=<span class="st0">"checkAll(&lt;?php echo count( $this-&gt;items ); ?&gt;);"</span> /&gt;
&lt;/th&gt;</pre>
</div>
<p>The Javascript checkAll function is a function that is built into the Joomla! base Javascript package that provides the functionality that we want here.</p>
<p>Now we need to add the checkboxes into the individual rows. Joomla!&#8217;s JHTML class has a method, JHTML::_(), which will generate our checkbox for us. We will add the following line to our loop:</p>
<div dir="ltr">
<pre class="source-php"><span class="re0">$checked</span>    = JHTML::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'grid.id'</span>, <span class="re0">$i</span>, <span class="re0">$row</span>-&gt;<span class="me1">id</span> <span class="br0">)</span>;</pre>
</div>
<p>after the line:</p>
<div dir="ltr">
<pre class="source-php"><span class="re0">$row</span> =&amp; <span class="re0">$this</span>-&gt;<span class="me1">items</span><span class="br0">[</span><span class="re0">$i</span><span class="br0">]</span>;</pre>
</div>
<p>Then we will add a cell in between the two that we already have:</p>
<div dir="ltr">
<pre class="source-php">&lt;td&gt;
    <span class="kw2">&lt;?php</span> <span class="kw3">echo</span> <span class="re0">$checked</span>; <span class="kw2">?&gt;</span>
&lt;/td&gt;</pre>
</div>
<p>It can be cumbersome to have to check the box that we want to edit and then move up and click the edit button. Therefore, we will add a link that it will go straight to the greeting&#8217;s edit form. We will add the following line after the call to the JHTML::_() method to generate the link HTML:</p>
<div dir="ltr">
<pre class="source-php"><span class="re0">$link</span> = JRoute::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'index.php?option=com_hello&amp;controller=hello&amp;task=edit&amp;cid[]='</span>. <span class="re0">$row</span>-&gt;<span class="me1">id</span> <span class="br0">)</span>;</pre>
</div>
<p>And we include the link in the cell showing the greeting text:</p>
<div dir="ltr">
<pre class="source-php">&lt;td&gt;
    &lt;a href=<span class="st0">"&lt;?php echo $link; ?&gt;"</span>&gt;&lt;?php <span class="kw3">echo</span> <span class="re0">$row</span>-&gt;<span class="me1">greeting</span>; ?&gt;&lt;/a&gt;
&lt;/td&gt;</pre>
</div>
<p>You will notice that this link points to the hello controller. This controller will handle the data manipulation of our greetings.</p>
<p>If you recall from above, we had four hidden input fields at the bottom of our form. The first input field was named &#8216;option&#8217;. This field is necessary so that we stay in our component. The second input field was task. This form property gets set when one of the buttons in the toolbar is clicked. A Javascript error will result and the buttons will not work if this input field is omitted. The third input field is the boxchecked field. This field keeps track of the number of boxes that are checked. The edit and delete buttons will check to ensure that this is greater than zero and will not allow the form to be submitted if it is not. The fourth input field is the controller field. This is used to specify that tasks fired from this form will be handled by the hello controller.</p>
<p>Here is the code for the completed default.php file:</p>
<div dir="ltr">
<pre class="source-php"><span class="kw2">&lt;?php</span> <span class="kw3">defined</span><span class="br0">(</span><span class="st0">'_JEXEC'</span><span class="br0">)</span> or <span class="kw3">die</span><span class="br0">(</span><span class="st0">'Restricted access'</span><span class="br0">)</span>; <span class="kw2">?&gt;</span>
&lt;form action=<span class="st0">"index.php"</span> method=<span class="st0">"post"</span> name=<span class="st0">"adminForm"</span>&gt;
&lt;div id=<span class="st0">"editcell"</span>&gt;
    &lt;table <span class="kw2">class</span>=<span class="st0">"adminlist"</span>&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th width=<span class="st0">"5"</span>&gt;
                <span class="kw2">&lt;?php</span> <span class="kw3">echo</span> JText::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'ID'</span> <span class="br0">)</span>; <span class="kw2">?&gt;</span>
            &lt;/th&gt;
            &lt;th width=<span class="st0">"20"</span>&gt;
              &lt;input type=<span class="st0">"checkbox"</span> name=<span class="st0">"toggle"</span> value=<span class="st0">""</span> onclick=<span class="st0">"checkAll(&lt;?php echo count( $this-&gt;items ); ?&gt;);"</span> /&gt;
            &lt;/th&gt;
            &lt;th&gt;
                <span class="kw2">&lt;?php</span> <span class="kw3">echo</span> JText::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'Greeting'</span> <span class="br0">)</span>; <span class="kw2">?&gt;</span>
            &lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    <span class="kw2">&lt;?php</span>
    <span class="re0">$k</span> = <span class="nu0">0</span>;
    <span class="kw1">for</span> <span class="br0">(</span><span class="re0">$i</span>=<span class="nu0">0</span>, <span class="re0">$n</span>=<span class="kw3">count</span><span class="br0">(</span> <span class="re0">$this</span>-&gt;<span class="me1">items</span> <span class="br0">)</span>; <span class="re0">$i</span> &lt; <span class="re0">$n</span>; <span class="re0">$i</span>++<span class="br0">)</span>
    <span class="br0">{</span>
        <span class="re0">$row</span> =&amp; <span class="re0">$this</span>-&gt;<span class="me1">items</span><span class="br0">[</span><span class="re0">$i</span><span class="br0">]</span>;
        <span class="re0">$checked</span>    = JHTML::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'grid.id'</span>, <span class="re0">$i</span>, <span class="re0">$row</span>-&gt;<span class="me1">id</span> <span class="br0">)</span>;
        <span class="re0">$link</span> = JRoute::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'index.php?option=com_hello&amp;controller=hello&amp;task=edit&amp;cid[]='</span>. <span class="re0">$row</span>-&gt;<span class="me1">id</span> <span class="br0">)</span>;

        <span class="kw2">?&gt;</span>
        &lt;tr <span class="kw2">class</span>=<span class="st0">"&lt;?php echo "</span>row<span class="re0">$k</span><span class="st0">"; ?&gt;"</span>&gt;
            &lt;td&gt;
                <span class="kw2">&lt;?php</span> <span class="kw3">echo</span> <span class="re0">$row</span>-&gt;<span class="me1">id</span>; <span class="kw2">?&gt;</span>
            &lt;/td&gt;
            &lt;td&gt;
              <span class="kw2">&lt;?php</span> <span class="kw3">echo</span> <span class="re0">$checked</span>; <span class="kw2">?&gt;</span>
            &lt;/td&gt;
            &lt;td&gt;
                &lt;a href=<span class="st0">"&lt;?php echo $link; ?&gt;"</span>&gt;&lt;?php <span class="kw3">echo</span> <span class="re0">$row</span>-&gt;<span class="me1">greeting</span>; ?&gt;&lt;/a&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        <span class="kw2">&lt;?php</span>
        <span class="re0">$k</span> = <span class="nu0">1</span> - <span class="re0">$k</span>;
    <span class="br0">}</span>
    <span class="kw2">?&gt;</span>
    &lt;/table&gt;
&lt;/div&gt;

&lt;input type=<span class="st0">"hidden"</span> name=<span class="st0">"option"</span> value=<span class="st0">"com_hello"</span> /&gt;
&lt;input type=<span class="st0">"hidden"</span> name=<span class="st0">"task"</span> value=<span class="st0">""</span> /&gt;
&lt;input type=<span class="st0">"hidden"</span> name=<span class="st0">"boxchecked"</span> value=<span class="st0">"0"</span> /&gt;
&lt;input type=<span class="st0">"hidden"</span> name=<span class="st0">"controller"</span> value=<span class="st0">"hello"</span> /&gt;

&lt;/form&gt;</pre>
</div>
<p>Our hellos view is now complete.</p>
<p><a id="Getting_Down_and_Dirty:_Doing_the_Real_Work" name="Getting_Down_and_Dirty:_Doing_the_Real_Work"></a></p>
<h2><span class="mw-headline">Getting Down and Dirty: Doing the Real Work</span></h2>
<p>Now that the Hellos view is done, it is time to move to the Hello controller and model. This is where the real work will get done.</p>
<p><a id="The_Hello_Controller" name="The_Hello_Controller"></a></p>
<h4><span class="mw-headline">The Hello Controller</span></h4>
<p>Our default controller just isn&#8217;t going to cut it when it comes to doing work &#8211; all it is capable of doing is displaying views.</p>
<p>We need to be able to handle the tasks that we are launching from the Hellos view: add, edit and remove.</p>
<p>Add and edit are essentially the same task: they both display a form to the user that allows a greeting to be edited. The only difference is that new displays a blank form, and edit displays a form with data already in it. Since they are similar, we will map the add task onto the edit task handler. This is specified in our constructor:</p>
<div dir="ltr">
<pre class="source-php"><span class="coMULTI">/**
 * constructor (registers additional tasks to methods)
 * @return void
 */</span>
<span class="kw2">function</span> __construct<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    parent::__construct<span class="br0">(</span><span class="br0">)</span>;

    <span class="co1">// Register Extra tasks</span>
    <span class="re0">$this</span>-&gt;<span class="me1">registerTask</span><span class="br0">(</span> <span class="st0">'add'</span>  ,     <span class="st0">'edit'</span> <span class="br0">)</span>;
<span class="br0">}</span></pre>
</div>
<p>The first parameter of JController::registerTask is the task to map, and the second is the method to map it to.</p>
<p>We will start with handling the edit task. The controller&#8217;s job is fairly simple for the edit task. All it has to do is specify the view and layout to load (the hello view and the form layout). We will also tell Joomla! to disable the mainmenu while we are editing our greeting. This prevents users from leaving unsaved records open.</p>
<p>Our edit task handler looks like:</p>
<div dir="ltr">
<pre class="source-php"><span class="coMULTI">/**
 * display the edit form
 * @return void
 */</span>
<span class="kw2">function</span> edit<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    JRequest::<span class="me2">setVar</span><span class="br0">(</span> <span class="st0">'view'</span>, <span class="st0">'hello'</span> <span class="br0">)</span>;
    JRequest::<span class="me2">setVar</span><span class="br0">(</span> <span class="st0">'layout'</span>, <span class="st0">'form'</span>  <span class="br0">)</span>;
    JRequest::<span class="me2">setVar</span><span class="br0">(</span><span class="st0">'hidemainmenu'</span>, <span class="nu0">1</span><span class="br0">)</span>;

    parent::<span class="me2">display</span><span class="br0">(</span><span class="br0">)</span>;
<span class="br0">}</span></pre>
</div>
<p>NOTE on naming conventions: The top calling program (hello.php) makes assumptions about the file names and class names of the controllers. To summarize for this example:</p>
<pre>  Default controller path:    .../controllers.php
  Default controller class name:  HellosController</pre>
<pre>  Requested controller path .../controllers/&lt;requestName&gt;.php
  Requested controller class name:  HellosController&lt;requestName&gt;</pre>
<p><a id="The_Hello_View" name="The_Hello_View"></a></p>
<h4><span class="mw-headline">The Hello View</span></h4>
<p>The Hello view will display a form which will allow the user to edit a greeting. The display method if the hello view has to do a few simple tasks:</p>
<ul>
<li>retrieve the data from the model</li>
<li>create the toolbar</li>
<li>pass the data into the template</li>
<li>invoke the display() method to render the template</li>
</ul>
<p>This becomes a bit more complicated because the one view handles both the edit and add tasks. In our toolbar we want the user to know what whether they are adding or editing, so we have to determine which task was fired.</p>
<p>Since we are already retrieving the record that we want to display from the model, we can use this data to determine what task was fired. If the task was edit, then the id field of our record will have been set. If the task was new, then it will not have been set. This can be used to determine if we have a new record or an existing record.</p>
<p>We will add two buttons to the toolbar: save and cancel. Though the functionality will be the same, we want to display different buttons depending on whether it is a new or existing record. If it is a new record, we will display cancel. If it already exists, we will display close.</p>
<p>Thus our display method looks like this:</p>
<div dir="ltr">
<pre class="source-php"><span class="coMULTI">/**
 * display method of Hello view
 * @return void
 **/</span>
<span class="kw2">function</span> display<span class="br0">(</span><span class="re0">$tpl</span> = <span class="kw2">null</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="co1">//get the hello</span>
    <span class="re0">$hello</span>        =&amp; <span class="re0">$this</span>-&gt;<span class="me1">get</span><span class="br0">(</span><span class="st0">'Data'</span><span class="br0">)</span>;
    <span class="re0">$isNew</span>        = <span class="br0">(</span><span class="re0">$hello</span>-&gt;<span class="me1">id</span> &lt; <span class="nu0">1</span><span class="br0">)</span>;

    <span class="re0">$text</span> = <span class="re0">$isNew</span> ? JText::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'New'</span> <span class="br0">)</span> : JText::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'Edit'</span> <span class="br0">)</span>;
    JToolBarHelper::<span class="me2">title</span><span class="br0">(</span>   JText::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'Hello'</span> <span class="br0">)</span>.<span class="st0">': &lt;small&gt;&lt;small&gt;[ '</span> . <span class="re0">$text</span>.<span class="st0">' ]&lt;/small&gt;&lt;/small&gt;'</span> <span class="br0">)</span>;
    JToolBarHelper::<span class="me2">save</span><span class="br0">(</span><span class="br0">)</span>;
    <span class="kw1">if</span> <span class="br0">(</span><span class="re0">$isNew</span><span class="br0">)</span>  <span class="br0">{</span>
        JToolBarHelper::<span class="me2">cancel</span><span class="br0">(</span><span class="br0">)</span>;
    <span class="br0">}</span> <span class="kw1">else</span> <span class="br0">{</span>
        <span class="co1">// for existing items the button is renamed `close`</span>
        JToolBarHelper::<span class="me2">cancel</span><span class="br0">(</span> <span class="st0">'cancel'</span>, <span class="st0">'Close'</span> <span class="br0">)</span>;
    <span class="br0">}</span>

    <span class="re0">$this</span>-&gt;<span class="me1">assignRef</span><span class="br0">(</span><span class="st0">'hello'</span>, <span class="re0">$hello</span><span class="br0">)</span>;
    parent::<span class="me2">display</span><span class="br0">(</span><span class="re0">$tpl</span><span class="br0">)</span>;
<span class="br0">}</span></pre>
</div>
<p><a id="The_Hello_Model" name="The_Hello_Model"></a></p>
<h4><span class="mw-headline">The Hello Model</span></h4>
<p>Our view needs data. Therefore, we need to create a model to model a hello.</p>
<p>Our model will have two properties: _id and _data. _id will hold the id of the greeting and data will hold the data.</p>
<p>We will start with a constructor, which will attempt to retrieve the id from the query:</p>
<div dir="ltr">
<pre class="source-php"><span class="coMULTI">/**
 * Constructor that retrieves the ID from the request
 *
 * @access    public
 * @return    void
 */</span>
<span class="kw2">function</span> __construct<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    parent::__construct<span class="br0">(</span><span class="br0">)</span>;

    <span class="re0">$array</span> = JRequest::<span class="me2">getVar</span><span class="br0">(</span><span class="st0">'cid'</span>,  <span class="nu0">0</span>, <span class="st0">''</span>, <span class="st0">'array'</span><span class="br0">)</span>;
    <span class="re0">$this</span>-&gt;<span class="me1">setId</span><span class="br0">(</span><span class="br0">(</span>int<span class="br0">)</span><span class="re0">$array</span><span class="br0">[</span><span class="nu0">0</span><span class="br0">]</span><span class="br0">)</span>;
<span class="br0">}</span></pre>
</div>
<p>The JRequest::getVar() method is used to retrieve data from the request. The first parameter is the name of the form variable. The second parameter is the default value to assign if there is no value found. The third parameter is the name of the hash to retrieve the value from (get, post, etc), and the last value is the data type that should be forced on the value.</p>
<p>Our constructor will take the first value from the cid array and assign it to the id.</p>
<p>Our setId() method can be used to set our id. Changing the id that our model points to will mean the id points to the wrong data. Therefore, when we set the id, we will clear the data property:</p>
<div dir="ltr">
<pre class="source-php"><span class="coMULTI">/**
 * Method to set the hello identifier
 *
 * @access    public
 * @param    int Hello identifier
 * @return    void
 */</span>
<span class="kw2">function</span> setId<span class="br0">(</span><span class="re0">$id</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="co1">// Set id and wipe data</span>
    <span class="re0">$this</span>-&gt;_id        = <span class="re0">$id</span>;
    <span class="re0">$this</span>-&gt;_data    = <span class="kw2">null</span>;
<span class="br0">}</span></pre>
</div>
<p>Finally, we need a method to retrieve our data: getData()</p>
<p>getData will check if the _data property has already been set. If it has, it will simply return it. Otherwise, it will load the data from the database.</p>
<div dir="ltr">
<pre class="source-php"><span class="coMULTI">/**
 * Method to get a hello
 * @return object with data
 */</span>

<span class="kw2">function</span> &amp;getData<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="co1">// Load the data</span>
    <span class="kw1">if</span> <span class="br0">(</span><span class="kw3">empty</span><span class="br0">(</span> <span class="re0">$this</span>-&gt;_data <span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span>
        <span class="re0">$query</span> = <span class="st0">' SELECT * FROM #__hello '</span>.
                <span class="st0">'  WHERE id = '</span>.<span class="re0">$this</span>-&gt;_id;
        <span class="re0">$this</span>-&gt;_db-&gt;<span class="me1">setQuery</span><span class="br0">(</span> <span class="re0">$query</span> <span class="br0">)</span>;
        <span class="re0">$this</span>-&gt;_data = <span class="re0">$this</span>-&gt;_db-&gt;<span class="me1">loadObject</span><span class="br0">(</span><span class="br0">)</span>;
    <span class="br0">}</span>
    <span class="kw1">if</span> <span class="br0">(</span>!<span class="re0">$this</span>-&gt;_data<span class="br0">)</span> <span class="br0">{</span>
        <span class="re0">$this</span>-&gt;_data = <span class="kw2">new</span> stdClass<span class="br0">(</span><span class="br0">)</span>;
        <span class="re0">$this</span>-&gt;_data-&gt;<span class="me1">id</span> = <span class="nu0">0</span>;
        <span class="re0">$this</span>-&gt;_data-&gt;<span class="me1">greeting</span> = <span class="kw2">null</span>;
    <span class="br0">}</span>
    <span class="kw1">return</span> <span class="re0">$this</span>-&gt;_data;
<span class="br0">}</span></pre>
</div>
<p><a id="The_Form" name="The_Form"></a></p>
<h4><span class="mw-headline">The Form</span></h4>
<p>Now all that is left is to create the form that the data will go into. Since we specified our layout as form, the form will go in a file in the tmpl directory of the hello view called form.php:</p>
<div dir="ltr">
<pre class="source-php"><span class="kw2">&lt;?php</span> <span class="kw3">defined</span><span class="br0">(</span><span class="st0">'_JEXEC'</span><span class="br0">)</span> or <span class="kw3">die</span><span class="br0">(</span><span class="st0">'Restricted access'</span><span class="br0">)</span>; <span class="kw2">?&gt;</span>

&lt;form action=<span class="st0">"index.php"</span> method=<span class="st0">"post"</span> name=<span class="st0">"adminForm"</span> id=<span class="st0">"adminForm"</span>&gt;
&lt;div <span class="kw2">class</span>=<span class="st0">"col100"</span>&gt;
    &lt;fieldset <span class="kw2">class</span>=<span class="st0">"adminform"</span>&gt;
        &lt;legend&gt;&lt;?php <span class="kw3">echo</span> JText::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'Details'</span> <span class="br0">)</span>; ?&gt;&lt;/legend&gt;
        &lt;table <span class="kw2">class</span>=<span class="st0">"admintable"</span>&gt;
        &lt;tr&gt;
            &lt;td width=<span class="st0">"100"</span> align=<span class="st0">"right"</span> <span class="kw2">class</span>=<span class="st0">"key"</span>&gt;
                &lt;label <span class="kw1">for</span>=<span class="st0">"greeting"</span>&gt;
                    <span class="kw2">&lt;?php</span> <span class="kw3">echo</span> JText::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'Greeting'</span> <span class="br0">)</span>; <span class="kw2">?&gt;</span>:
                &lt;/label&gt;
            &lt;/td&gt;
            &lt;td&gt;
                &lt;input <span class="kw2">class</span>=<span class="st0">"text_area"</span> type=<span class="st0">"text"</span> name=<span class="st0">"greeting"</span> id=<span class="st0">"greeting"</span> size=<span class="st0">"32"</span> maxlength=<span class="st0">"250"</span> value=<span class="st0">"&lt;?php echo $this-&gt;hello-&gt;greeting;?&gt;"</span> /&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/table&gt;
    &lt;/fieldset&gt;
&lt;/div&gt;

&lt;div <span class="kw2">class</span>=<span class="st0">"clr"</span>&gt;&lt;/div&gt;

&lt;input type=<span class="st0">"hidden"</span> name=<span class="st0">"option"</span> value=<span class="st0">"com_hello"</span> /&gt;
&lt;input type=<span class="st0">"hidden"</span> name=<span class="st0">"id"</span> value=<span class="st0">"&lt;?php echo $this-&gt;hello-&gt;id; ?&gt;"</span> /&gt;
&lt;input type=<span class="st0">"hidden"</span> name=<span class="st0">"task"</span> value=<span class="st0">""</span> /&gt;
&lt;input type=<span class="st0">"hidden"</span> name=<span class="st0">"controller"</span> value=<span class="st0">"hello"</span> /&gt;
&lt;/form&gt;</pre>
</div>
<p>Notice that in addition to the input field, there is a hidden field for the id. The user doesn&#8217;t need to edit the id (and shouldn&#8217;t), so we silently pass it along in the form.</p>
<p><a id="Implementing_the_Functionality" name="Implementing_the_Functionality"></a></p>
<h4><span class="mw-headline">Implementing the Functionality</span></h4>
<p>So far, our controller only handles two tasks: edit and new. However, we also have buttons to save, delete and cancel records. We need to write code to handle and perform these tasks.</p>
<p><a id="Saving_a_Record" name="Saving_a_Record"></a></p>
<h3><span class="mw-headline">Saving a Record</span></h3>
<p>The logical next step is to implement the functionality to save a record. Normally, this would require some switches and logic to handle various cases, such as the difference between creating a new record (an INSERT query), and updating an existing query (an UPDATE query). Also, there are complexities involved in getting the data from the form and putting it into the query.</p>
<p>The Joomla! framework takes care of a lot of this for you. The JTable class makes it easy to manipulate records in the database without having to worry about writing the SQL code that lies behind these updates. It also makes it easy to transfer data from an HTML form into the database.</p>
<p><a id="Creating_the_Table_Class" name="Creating_the_Table_Class"></a></p>
<h2><span class="mw-headline">Creating the Table Class</span></h2>
<p>The JTable class is an abstract class from which you can derive child classes to work with specific tables. To use it, you simply create a class that extends the JTable class, add your database fields as properties, and override the constructor to specify the name of the table and the primary key.</p>
<p>Here is our JTable class:</p>
<div dir="ltr">
<pre class="source-php"><span class="kw2">&lt;?php</span>
<span class="coMULTI">/**
 * Hello World table class
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4
 * @license        GNU/GPL
 */</span>

<span class="co1">// No direct access</span>
<span class="kw3">defined</span><span class="br0">(</span><span class="st0">'_JEXEC'</span><span class="br0">)</span> or <span class="kw3">die</span><span class="br0">(</span><span class="st0">'Restricted access'</span><span class="br0">)</span>;

<span class="coMULTI">/**
 * Hello Table class
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */</span>
<span class="kw2">class</span> TableHello <span class="kw2">extends</span> JTable
<span class="br0">{</span>
    <span class="coMULTI">/**
     * Primary Key
     *
     * @var int
     */</span>
    <span class="kw2">var</span> <span class="re0">$id</span> = <span class="kw2">null</span>;

    <span class="coMULTI">/**
     * @var string
     */</span>
    <span class="kw2">var</span> <span class="re0">$greeting</span> = <span class="kw2">null</span>;

    <span class="coMULTI">/**
     * Constructor
     *
     * @param object Database connector object
     */</span>
    <span class="kw2">function</span> TableHello<span class="br0">(</span> &amp;<span class="re0">$db</span> <span class="br0">)</span> <span class="br0">{</span>
        parent::__construct<span class="br0">(</span><span class="st0">'#__hello'</span>, <span class="st0">'id'</span>, <span class="re0">$db</span><span class="br0">)</span>;
    <span class="br0">}</span>
<span class="br0">}</span></pre>
</div>
<p>You will see here that we have defined our two fields: the id field and the greeting field. Then we have defined a constructor, which will call the constructor of the parent class and pass it the name of the table (hello), the name of the field which is the primary key (id), and the database connector object.</p>
<p>This file should be called hello.php and it will go in a directory called tables in the administrator section of our component.</p>
<p><a id="Implementing_the_Function_in_our_Model" name="Implementing_the_Function_in_our_Model"></a></p>
<h2><span class="mw-headline">Implementing the Function in our Model</span></h2>
<p>We are now ready to add the method to the model which will save our record. We will call this method store. Our store() method will do three things: bind the data from the form to the TableHello object, check to ensure that the record is properly formed, and store the record in the database.</p>
<p>Our method looks like:</p>
<div dir="ltr">
<pre class="source-php"><span class="coMULTI">/**
 * Method to store a record
 *
 * @access    public
 * @return    boolean    True on success
 */</span>
<span class="kw2">function</span> store<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="re0">$row</span> =&amp; <span class="re0">$this</span>-&gt;<span class="me1">getTable</span><span class="br0">(</span><span class="br0">)</span>;

    <span class="re0">$data</span> = JRequest::<span class="me2">get</span><span class="br0">(</span> <span class="st0">'post'</span> <span class="br0">)</span>;
    <span class="co1">// Bind the form fields to the hello table</span>
    <span class="kw1">if</span> <span class="br0">(</span>!<span class="re0">$row</span>-&gt;<span class="me1">bind</span><span class="br0">(</span><span class="re0">$data</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span>
        <span class="re0">$this</span>-&gt;<span class="me1">setError</span><span class="br0">(</span><span class="re0">$this</span>-&gt;_db-&gt;<span class="me1">getErrorMsg</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span>;
        <span class="kw1">return</span> <span class="kw2">false</span>;
    <span class="br0">}</span>

    <span class="co1">// Make sure the hello record is valid</span>
    <span class="kw1">if</span> <span class="br0">(</span>!<span class="re0">$row</span>-&gt;<span class="me1">check</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span>
        <span class="re0">$this</span>-&gt;<span class="me1">setError</span><span class="br0">(</span><span class="re0">$this</span>-&gt;_db-&gt;<span class="me1">getErrorMsg</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span>;
        <span class="kw1">return</span> <span class="kw2">false</span>;
    <span class="br0">}</span>

    <span class="co1">// Store the web link table to the database</span>
    <span class="kw1">if</span> <span class="br0">(</span>!<span class="re0">$row</span>-&gt;<span class="me1">store</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span>
        <span class="re0">$this</span>-&gt;<span class="me1">setError</span><span class="br0">(</span><span class="re0">$this</span>-&gt;_db-&gt;<span class="me1">getErrorMsg</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span>;
        <span class="kw1">return</span> <span class="kw2">false</span>;
    <span class="br0">}</span>

    <span class="kw1">return</span> <span class="kw2">true</span>;
<span class="br0">}</span></pre>
</div>
<p>This method gets added to the hello model.</p>
<p>The method takes one parameter, which is an associative array of data that we want to store in the database. This can easily be retrieved from the request as will be seen later.</p>
<p>You will see that the first line retrieves a reference to our JTable object. If we name our table properly, we don&#8217;t have to specify its name &#8211; the JModel class knows where to find it. You may recall that we called our table class TableHello and put it in a file called hello.php in the tables directory. If you follow this convention, the JModel class can create your object automatically.</p>
<p>The second line will retrieve the data from the form. The JRequest class makes this very easy. In this case, we are retrieving all of the variables that were submitted using the &#8216;POST&#8217; method. These will be returned as an associative array.</p>
<p>The rest is easy &#8211; we bind, check and store. The bind() method will copy values from the array into the corresponding property of the table object. In this case, it will take the values of id and greeting and copy them to our TableHello object.</p>
<p>The check() method will perform data verification. In the JTable() class, this method simply returns true. While this doesn&#8217;t provide any value for us currently, by calling this method we make it possible to do data checking using our TableHello class in the future. This method can be overridden in our TableHello class with a method that performs the appropriate checks.</p>
<p>The store() method will take the data that is in the object and store it in the database. If the id is 0, it will create a new record (INSERT), otherwise, it will update the existing record (UPDATE).</p>
<p><a id="Adding_the_Task_to_the_Controller" name="Adding_the_Task_to_the_Controller"></a></p>
<h2><span class="mw-headline">Adding the Task to the Controller</span></h2>
<p>We are now ready to add our task to the controller. Since the task that we are firing is called &#8217;save&#8217;, we must call our method &#8217;save&#8217;. This is simple:</p>
<div dir="ltr">
<pre class="source-php"><span class="coMULTI">/**
 * save a record (and redirect to main page)
 * @return void
 */</span>
<span class="kw2">function</span> save<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="re0">$model</span> = <span class="re0">$this</span>-&gt;<span class="me1">getModel</span><span class="br0">(</span><span class="st0">'hello'</span><span class="br0">)</span>;

    <span class="kw1">if</span> <span class="br0">(</span><span class="re0">$model</span>-&gt;<span class="me1">store</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span>
        <span class="re0">$msg</span> = JText::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'Greeting Saved!'</span> <span class="br0">)</span>;
    <span class="br0">}</span> <span class="kw1">else</span> <span class="br0">{</span>
        <span class="re0">$msg</span> = JText::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'Error Saving Greeting'</span> <span class="br0">)</span>;
    <span class="br0">}</span>

    <span class="co1">// Check the table in so it can be edited.... we are done with it anyway</span>
    <span class="re0">$link</span> = <span class="st0">'index.php?option=com_hello'</span>;
    <span class="re0">$this</span>-&gt;<span class="me1">setRedirect</span><span class="br0">(</span><span class="re0">$link</span>, <span class="re0">$msg</span><span class="br0">)</span>;
<span class="br0">}</span></pre>
</div>
<p>All we do is get our model and invoke the store() method. Then we use the setRedirect() method to redirect back to our list of greetings. We also pass a message along, which will be displayed at the top of the page.</p>
<p><a id="Deleting_a_Record" name="Deleting_a_Record"></a></p>
<h3><span class="mw-headline">Deleting a Record</span></h3>
<p><a id="Implementing_the_Function_in_the_Model" name="Implementing_the_Function_in_the_Model"></a></p>
<h3><span class="mw-headline">Implementing the Function in the Model</span></h3>
<p>In the model, we will retrieve the list of IDs to delete and call the JTable class to delete them. Here it is:</p>
<div dir="ltr">
<pre class="source-php"><span class="coMULTI">/**
 * Method to delete record(s)
 *
 * @access    public
 * @return    boolean    True on success
 */</span>
<span class="kw2">function</span> delete<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="re0">$cids</span> = JRequest::<span class="me2">getVar</span><span class="br0">(</span> <span class="st0">'cid'</span>, <span class="kw3">array</span><span class="br0">(</span><span class="nu0">0</span><span class="br0">)</span>, <span class="st0">'post'</span>, <span class="st0">'array'</span> <span class="br0">)</span>;
    <span class="re0">$row</span> =&amp; <span class="re0">$this</span>-&gt;<span class="me1">getTable</span><span class="br0">(</span><span class="br0">)</span>;

    <span class="kw1">foreach</span><span class="br0">(</span><span class="re0">$cids</span> <span class="kw1">as</span> <span class="re0">$cid</span><span class="br0">)</span> <span class="br0">{</span>
        <span class="kw1">if</span> <span class="br0">(</span>!<span class="re0">$row</span>-&gt;<span class="me1">delete</span><span class="br0">(</span> <span class="re0">$cid</span> <span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span>
            <span class="re0">$this</span>-&gt;<span class="me1">setError</span><span class="br0">(</span> <span class="re0">$row</span>-&gt;<span class="me1">getErrorMsg</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">)</span>;
            <span class="kw1">return</span> <span class="kw2">false</span>;
        <span class="br0">}</span>
    <span class="br0">}</span>

    <span class="kw1">return</span> <span class="kw2">true</span>;
<span class="br0">}</span></pre>
</div>
<p>We invoke the JRequest::getVar() method to get the data from the request, then we invoke the $row-&gt;delete() method to delete each row. By storing errors in the model we make it possible to retrieve them later if we so choose.</p>
<p><a id="Handling_the_Remove_Task_in_the_Controller" name="Handling_the_Remove_Task_in_the_Controller"></a></p>
<h3><span class="mw-headline">Handling the Remove Task in the Controller</span></h3>
<p>This is similar to the save() method which handled the save task:</p>
<div dir="ltr">
<pre class="source-php"><span class="coMULTI">/**
 * remove record(s)
 * @return void
 */</span>
<span class="kw2">function</span> remove<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="re0">$model</span> = <span class="re0">$this</span>-&gt;<span class="me1">getModel</span><span class="br0">(</span><span class="st0">'hello'</span><span class="br0">)</span>;
    <span class="kw1">if</span><span class="br0">(</span>!<span class="re0">$model</span>-&gt;<span class="me1">delete</span><span class="br0">(</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">{</span>
        <span class="re0">$msg</span> = JText::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'Error: One or More Greetings Could not be Deleted'</span> <span class="br0">)</span>;
    <span class="br0">}</span> <span class="kw1">else</span> <span class="br0">{</span>
        <span class="re0">$msg</span> = JText::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'Greeting(s) Deleted'</span> <span class="br0">)</span>;
    <span class="br0">}</span>

    <span class="re0">$this</span>-&gt;<span class="me1">setRedirect</span><span class="br0">(</span> <span class="st0">'index.php?option=com_hello'</span>, <span class="re0">$msg</span> <span class="br0">)</span>;
<span class="br0">}</span></pre>
</div>
<p><a id="Cancelling_the_Edit_Operation" name="Cancelling_the_Edit_Operation"></a></p>
<h4><span class="mw-headline">Cancelling the Edit Operation</span></h4>
<p>To cancel the edit operation, all we have to do is redirect back to the main view:</p>
<div dir="ltr">
<pre class="source-php"><span class="coMULTI">/**
 * cancel editing a record
 * @return void
 */</span>
<span class="kw2">function</span> cancel<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="re0">$msg</span> = JText::<span class="kw3">_</span><span class="br0">(</span> <span class="st0">'Operation Cancelled'</span> <span class="br0">)</span>;
    <span class="re0">$this</span>-&gt;<span class="me1">setRedirect</span><span class="br0">(</span> <span class="st0">'index.php?option=com_hello'</span>, <span class="re0">$msg</span> <span class="br0">)</span>;
<span class="br0">}</span></pre>
</div>
<p><a id="Conclusion" name="Conclusion"></a></p>
<h2><span class="mw-headline">Conclusion</span></h2>
<p>We have now implemented a basic backend to our component. We are now able to edit the entries that are viewed in the frontend. We have demonstrated the interaction between models, views and controllers. We have shown how the JTable class can be extended to provide easy access to tables in the database. It can also be seen how the JToolBarHelper class can be used to create button bars in components to present a standardized look between components.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.result-search.com/sty/2009/07/22/developing-a-model-view-controller-component-part-4-creating-an-administrator-interface.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Developing a Model-View-Controller Component &#8211; Part 3 &#8211; Using the Database</title>
		<link>http://www.result-search.com/sty/2009/07/22/developing-a-model-view-controller-component-part-3-using-the-database.html</link>
		<comments>http://www.result-search.com/sty/2009/07/22/developing-a-model-view-controller-component-part-3-using-the-database.html#comments</comments>
		<pubDate>Wed, 22 Jul 2009 06:35:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[joomla]]></category>

		<guid isPermaLink="false">http://www.yaaahaaa.com/?p=942</guid>
		<description><![CDATA[Introduction
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 [...]]]></description>
			<content:encoded><![CDATA[<h2><span class="mw-headline">Introduction</span></h2>
<p>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.</p>
<p>This tutorial will demonstrate how to use the JDatabase class to retrieve data from the database.<br />
<span id="more-942"></span><br />
<a id="Retrieving_the_Data" name="Retrieving_the_Data"></a></p>
<h2><span class="mw-headline">Retrieving the Data</span></h2>
<p>Our model currently has one method: getGreeting(). This method is very simple &#8211; all it does is return the hard-coded greeting.</p>
<p>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.</p>
<p>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:</p>
<div dir="ltr">
<pre class="source-php"><span class="re0">$db</span> =&amp; JFactory::<span class="me2">getDBO</span><span class="br0">(</span><span class="br0">)</span>;</pre>
</div>
<p>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.</p>
<p>The method name (getDBO) stands for get DataBase Object, and is easy and important to remember.</p>
<p>Now that we have obtained a reference to the database object, we can retrieve our data. We do this in two steps:</p>
<ul>
<li>store our query in the database object</li>
<li>load the result</li>
</ul>
<p>Our new getGreeting() method will therefore look like:</p>
<div dir="ltr">
<pre class="source-php"><span class="kw2">function</span> getGreeting<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
   <span class="re0">$db</span> =&amp; JFactory::<span class="me2">getDBO</span><span class="br0">(</span><span class="br0">)</span>;

   <span class="re0">$query</span> = <span class="st0">'SELECT greeting FROM #__hello'</span>;
   <span class="re0">$db</span>-&gt;<span class="me1">setQuery</span><span class="br0">(</span> <span class="re0">$query</span> <span class="br0">)</span>;
   <span class="re0">$greeting</span> = <span class="re0">$db</span>-&gt;<span class="me1">loadResult</span><span class="br0">(</span><span class="br0">)</span>;

   <span class="kw1">return</span> <span class="re0">$greeting</span>;
<span class="br0">}</span></pre>
</div>
<p>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 <a class="external text" title="http://www.w3schools.com/sql/default.asp" rel="nofollow" href="http://www.w3schools.com/sql/default.asp">w3schools</a>.</p>
<p>The $db-&gt;loadResult() method will execute the stored database query and return the first field of the first row of the result. See <a class="external text" title="http://api.joomla.org/Joomla-Framework/Database/JDatabase.html" rel="nofollow" href="http://api.joomla.org/Joomla-Framework/Database/JDatabase.html">JDatabase API reference</a> for more information about other load methods in the JDatabase class.</p>
<p><a id="Creating_the_Installation_SQL_File" name="Creating_the_Installation_SQL_File"></a></p>
<h2><span class="mw-headline">Creating the Installation SQL File</span></h2>
<p>The Joomla! installer has built-in support for executing queries during component installation. These queries are all stored in a standard text file.</p>
<p>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.</p>
<p>Here are our queries:</p>
<div dir="ltr">
<pre class="source-php">DROP TABLE <span class="kw1">IF</span> EXISTS `<span class="co2">#__hello`;</span>

CREATE TABLE `<span class="co2">#__hello` (</span>
  `id` int<span class="br0">(</span><span class="nu0">11</span><span class="br0">)</span> NOT <span class="kw2">NULL</span> auto_increment,
  `greeting` varchar<span class="br0">(</span><span class="nu0">25</span><span class="br0">)</span> NOT <span class="kw2">NULL</span>,
  PRIMARY <span class="kw3">KEY</span>  <span class="br0">(</span>`id`<span class="br0">)</span>
<span class="br0">)</span> ENGINE=MyISAM AUTO_INCREMENT=<span class="nu0">0</span> <span class="kw2">DEFAULT</span> CHARSET=utf8;

INSERT INTO `<span class="co2">#__hello` (`greeting`) VALUES ('Hello, World!'),</span>
<span class="br0">(</span><span class="st0">'Bonjour, Monde!'</span><span class="br0">)</span>,
<span class="br0">(</span><span class="st0">'Ciao, Mondo!'</span><span class="br0">)</span>;</pre>
</div>
<p>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 &#8216;users&#8217; table. This convention avoids problems.)</p>
<p>We have specified two fields in our database. The first field is id, and is called the &#8216;primary key&#8217;. 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.</p>
<p>We will save our queries in a file called install.utf.sql.</p>
<p><a id="Creating_the_Uninstall_SQL_File" name="Creating_the_Uninstall_SQL_File"></a></p>
<h3><span class="mw-headline">Creating the Uninstall SQL File</span></h3>
<p>Though we might hope that people will never want to uninstall our component, it is important that if they do, we don&#8217;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:</p>
<div dir="ltr">
<pre class="source-php">DROP TABLE <span class="kw1">IF</span> EXISTS `<span class="co2">#__hello`;</span></pre>
</div>
<p>We will save this query in a file called uninstall.utf.sql.</p>
<p><a id="Updating_our_Install_File" name="Updating_our_Install_File"></a></p>
<h2><span class="mw-headline">Updating our Install File</span></h2>
<p>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.</p>
<p>Our new file looks like this:</p>
<div dir="ltr">
<pre class="source-xml"><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">"1.0"</span> <span class="re0">encoding</span>=<span class="st0">"utf-8"</span><span class="re2">?&gt;</span></span>
<span class="sc3"><span class="re1">&lt;install</span> <span class="re0">type</span>=<span class="st0">"component"</span> <span class="re0">version</span>=<span class="st0">"1.5.0"</span><span class="re2">&gt;</span></span>
 <span class="sc3"><span class="re1">&lt;name<span class="re2">&gt;</span></span></span>Hello<span class="sc3"><span class="re1">&lt;/name<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="coMULTI">&lt;!-- The following elements are optional and free of formatting conttraints --&gt;</span></span>
 <span class="sc3"><span class="re1">&lt;creationDate<span class="re2">&gt;</span></span></span>2007-02-22<span class="sc3"><span class="re1">&lt;/creationDate<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;author<span class="re2">&gt;</span></span></span>John Doe<span class="sc3"><span class="re1">&lt;/author<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;authorEmail<span class="re2">&gt;</span></span></span>john.doe@example.org<span class="sc3"><span class="re1">&lt;/authorEmail<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;authorUrl<span class="re2">&gt;</span></span></span>http://www.example.org<span class="sc3"><span class="re1">&lt;/authorUrl<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;copyright<span class="re2">&gt;</span></span></span>Copyright Info<span class="sc3"><span class="re1">&lt;/copyright<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;license<span class="re2">&gt;</span></span></span>License Info<span class="sc3"><span class="re1">&lt;/license<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="coMULTI">&lt;!--  The version string is recorded in the components table --&gt;</span></span>
 <span class="sc3"><span class="re1">&lt;version<span class="re2">&gt;</span></span></span>3.01<span class="sc3"><span class="re1">&lt;/version<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="coMULTI">&lt;!-- The description is optional and defaults to the name --&gt;</span></span>
 <span class="sc3"><span class="re1">&lt;description<span class="re2">&gt;</span></span></span>Description of the component ...<span class="sc3"><span class="re1">&lt;/description<span class="re2">&gt;</span></span></span>

 <span class="sc3"><span class="coMULTI">&lt;!-- Site Main File Copy Section --&gt;</span></span>
 <span class="sc3"><span class="coMULTI">&lt;!-- Note the folder attribute: This attribute describes the folder
      to copy FROM in the package to install therefore files copied
      in this section are copied from /site/ in the package --&gt;</span></span>
 <span class="sc3"><span class="re1">&lt;files</span> <span class="re0">folder</span>=<span class="st0">"site"</span><span class="re2">&gt;</span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>controller.php<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>hello.php<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>index.html<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>models/hello.php<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>models/index.html<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>views/index.html<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>views/hello/index.html<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>views/hello/view.html.php<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>views/hello/tmpl/default.php<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>views/hello/tmpl/index.html<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;/files<span class="re2">&gt;</span></span></span>

 <span class="sc3"><span class="re1">&lt;install<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;sql<span class="re2">&gt;</span></span></span>
   <span class="sc3"><span class="re1">&lt;file</span> <span class="re0">charset</span>=<span class="st0">"utf8"</span> <span class="re0">driver</span>=<span class="st0">"mysql"</span><span class="re2">&gt;</span></span>install.sql<span class="sc3"><span class="re1">&lt;/file<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;/sql<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;/install<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;uninstall<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;sql<span class="re2">&gt;</span></span></span>
   <span class="sc3"><span class="re1">&lt;file</span> <span class="re0">charset</span>=<span class="st0">"utf8"</span> <span class="re0">driver</span>=<span class="st0">"mysql"</span><span class="re2">&gt;</span></span>uninstall.sql<span class="sc3"><span class="re1">&lt;/file<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;/sql<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;/uninstall<span class="re2">&gt;</span></span></span>

 <span class="sc3"><span class="re1">&lt;administration<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="coMULTI">&lt;!-- Administration Menu Section --&gt;</span></span>
  <span class="sc3"><span class="re1">&lt;menu<span class="re2">&gt;</span></span></span>Hello World!<span class="sc3"><span class="re1">&lt;/menu<span class="re2">&gt;</span></span></span>

  <span class="sc3"><span class="coMULTI">&lt;!-- Administration Main File Copy Section --&gt;</span></span>
  <span class="sc3"><span class="re1">&lt;files</span> <span class="re0">folder</span>=<span class="st0">"admin"</span><span class="re2">&gt;</span></span>
   <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>hello.php<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
   <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>index.html<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
   <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>install.sql<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
   <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>uninstall.sql<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/files<span class="re2">&gt;</span></span></span>

 <span class="sc3"><span class="re1">&lt;/administration<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/install<span class="re2">&gt;</span></span></span></pre>
</div>
<p>You will notice two attributes present on the &lt;file&gt; tags within the &lt;install&gt; and &lt;uninstall&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.</p>
<p>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.</p>
<p><a id="Conclusion" name="Conclusion"></a></p>
<h2><span class="mw-headline">Conclusion</span></h2>
<p>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.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.result-search.com/sty/2009/07/22/developing-a-model-view-controller-component-part-3-using-the-database.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Developing a Model-View-Controller Component &#8211; Part 2 &#8211; Adding a Model</title>
		<link>http://www.result-search.com/sty/2009/07/22/developing-a-model-view-controller-component-part-2-adding-a-model.html</link>
		<comments>http://www.result-search.com/sty/2009/07/22/developing-a-model-view-controller-component-part-2-adding-a-model.html#comments</comments>
		<pubDate>Wed, 22 Jul 2009 06:35:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[joomla]]></category>

		<guid isPermaLink="false">http://www.yaaahaaa.com/?p=941</guid>
		<description><![CDATA[Introduction
In the first tutorial of this series, creating a simple view-controller component using the Joomla! 1.5 CMS framework was demonstrated.
In the first tutorial, the greeting was hardcoded into the view. This doesn&#8217;t follow the MVC pattern exactly because the view is intended to only display the data, and not contain it.
In this second part of [...]]]></description>
			<content:encoded><![CDATA[<h2><span class="mw-headline">Introduction</span></h2>
<p>In the first tutorial of this series, creating a simple view-controller component using the Joomla! 1.5 CMS framework was demonstrated.</p>
<p>In the first tutorial, the greeting was hardcoded into the view. This doesn&#8217;t follow the MVC pattern exactly because the view is intended to only display the data, and not contain it.</p>
<p>In this second part of the tutorial we will demonstrate how to move this out of the view and into a model. In future tutorials we will demonstrate the power and flexibility that this design pattern provides.<br />
<span id="more-941"></span><br />
<a id="Creating_the_Model" name="Creating_the_Model"></a></p>
<h2><span class="mw-headline">Creating the Model</span></h2>
<p>The concept of model gets its name because this class is intended to represent (or &#8216;model&#8217;) some entity. In our case, our first model will represent a &#8216;hello&#8217;, or a greeting. This is in line with our design thus far, because we have one view (&#8216;hello&#8217;), which is a view of our greeting.</p>
<p>The naming convention for models in the Joomla! framework is that the class name starts with the name of the component (in our case &#8216;hello&#8217;, followed by &#8216;model&#8217;, followed by the model name. Therefore, our model class is called HelloModelHello.</p>
<p>At this point, we will only model one behaviour of our hello, and that is retrieving the greeting. We will thus have one method, called getGreeting(). It will simply return the string &#8216;Hello, World!&#8217;.</p>
<p>Here is the code for our model class:</p>
<div dir="ltr">
<pre class="source-php"><span class="kw2">&lt;?php</span>
<span class="coMULTI">/**
 * Hello Model for Hello World Component
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2
 * @license    GNU/GPL
 */</span>

<span class="co1">// No direct access</span>

<span class="kw3">defined</span><span class="br0">(</span> <span class="st0">'_JEXEC'</span> <span class="br0">)</span> or <span class="kw3">die</span><span class="br0">(</span> <span class="st0">'Restricted access'</span> <span class="br0">)</span>;

jimport<span class="br0">(</span> <span class="st0">'joomla.application.component.model'</span> <span class="br0">)</span>;

<span class="coMULTI">/**
 * Hello Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */</span>
<span class="kw2">class</span> HelloModelHello <span class="kw2">extends</span> JModel
<span class="br0">{</span>
    <span class="coMULTI">/**
    * Gets the greeting
    * @return string The greeting to be displayed to the user
    */</span>
    <span class="kw2">function</span> getGreeting<span class="br0">(</span><span class="br0">)</span>
    <span class="br0">{</span>
        <span class="kw1">return</span> <span class="st0">'Hello, World!'</span>;
    <span class="br0">}</span>
<span class="br0">}</span></pre>
</div>
<p>You will notice a line that starts with jimport. The jimport function is used to load files from the Joomla! framework that are required for our component. This particular statement will load the file /libraries/joomla/application/component/model.php. The &#8216;.&#8217;s are used as directory separators and the last part is the name of the file to load. All files are loaded relative to the libraries directory. This particular file contains the class definition for the JModel class, which is necessary because our model extends this class.</p>
<p>Now that we have created our model, we must modify our view so that it uses it to obtain the greeting.</p>
<p><a id="Using_the_Model" name="Using_the_Model"></a></p>
<h2><span class="mw-headline">Using the Model</span></h2>
<p>The Joomla! framework is setup in such a way that the controller will automatically load the model that has the same name as the view and will push it into the view. Since our view is called &#8216;Hello&#8217;, our &#8216;Hello&#8217; model will automatically be loaded and pushed into the view. Therefore, we can easily retrieve a reference to our model using the JView::getModel() method. (If the model had not followed this convention, we could have passed the model name to <a class="external text" title="http://api.joomla.org/Joomla-Framework/Application/JView.html#getModel" rel="nofollow" href="http://api.joomla.org/Joomla-Framework/Application/JView.html#getModel">JView::getModel()</a>)</p>
<p>Our previous view code contained the lines:</p>
<div dir="ltr">
<pre class="source-php"><span class="re0">$greeting</span> = <span class="st0">"Hello World!"</span>;</pre>
</div>
<p>To take advantage of our model, we change this line to:</p>
<div dir="ltr">
<pre class="source-php"><span class="re0">$model</span> =&amp; <span class="re0">$this</span>-&gt;<span class="me1">getModel</span><span class="br0">(</span><span class="br0">)</span>;
<span class="re0">$greeting</span> = <span class="re0">$model</span>-&gt;<span class="me1">getGreeting</span><span class="br0">(</span><span class="br0">)</span>;</pre>
</div>
<p>The complete view now looks like:</p>
<div dir="ltr">
<pre class="source-php"><span class="kw2">&lt;?php</span>
<span class="coMULTI">/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2
 * @license    GNU/GPL
*/</span>

<span class="co1">// No direct access</span>

<span class="kw3">defined</span><span class="br0">(</span> <span class="st0">'_JEXEC'</span> <span class="br0">)</span> or <span class="kw3">die</span><span class="br0">(</span> <span class="st0">'Restricted access'</span> <span class="br0">)</span>;

jimport<span class="br0">(</span> <span class="st0">'joomla.application.component.view'</span><span class="br0">)</span>;

<span class="coMULTI">/**
 * HTML View class for the HelloWorld Component
 *
 * @package    HelloWorld
 */</span>

<span class="kw2">class</span> HelloViewHello <span class="kw2">extends</span> JView
<span class="br0">{</span>
    <span class="kw2">function</span> display<span class="br0">(</span><span class="re0">$tpl</span> = <span class="kw2">null</span><span class="br0">)</span>
    <span class="br0">{</span>
        <span class="re0">$model</span> =&amp; <span class="re0">$this</span>-&gt;<span class="me1">getModel</span><span class="br0">(</span><span class="br0">)</span>;
        <span class="re0">$greeting</span> = <span class="re0">$model</span>-&gt;<span class="me1">getGreeting</span><span class="br0">(</span><span class="br0">)</span>;
        <span class="re0">$this</span>-&gt;<span class="me1">assignRef</span><span class="br0">(</span> <span class="st0">'greeting'</span>,        <span class="re0">$greeting</span> <span class="br0">)</span>;

        parent::<span class="me2">display</span><span class="br0">(</span><span class="re0">$tpl</span><span class="br0">)</span>;
    <span class="br0">}</span>
<span class="br0">}</span></pre>
</div>
<p><a id="Adding_the_File_to_the_Package" name="Adding_the_File_to_the_Package"></a></p>
<h3><span class="mw-headline">Adding the File to the Package</span></h3>
<p>All that remains is to add an entry to the XML file so that our new model will be copied. The Joomla! framework will look for our model in the models directory, so the entry for this file will look like (it should be added to the site section):</p>
<div dir="ltr">
<pre class="source-php">&lt;filename&gt;models/hello.php&lt;/filename&gt;</pre>
</div>
<p>Our new hello.xml file will look like:</p>
<div dir="ltr">
<pre class="source-xml"><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">"1.0"</span> <span class="re0">encoding</span>=<span class="st0">"utf-8"</span><span class="re2">?&gt;</span></span>
<span class="sc3"><span class="re1">&lt;install</span> <span class="re0">type</span>=<span class="st0">"component"</span> <span class="re0">version</span>=<span class="st0">"1.5.0"</span><span class="re2">&gt;</span></span>
 <span class="sc3"><span class="re1">&lt;name<span class="re2">&gt;</span></span></span>Hello<span class="sc3"><span class="re1">&lt;/name<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="coMULTI">&lt;!-- The following elements are optional and free of formatting conttraints --&gt;</span></span>
 <span class="sc3"><span class="re1">&lt;creationDate<span class="re2">&gt;</span></span></span>2007-02-22<span class="sc3"><span class="re1">&lt;/creationDate<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;author<span class="re2">&gt;</span></span></span>John Doe<span class="sc3"><span class="re1">&lt;/author<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;authorEmail<span class="re2">&gt;</span></span></span>john.doe@example.org<span class="sc3"><span class="re1">&lt;/authorEmail<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;authorUrl<span class="re2">&gt;</span></span></span>http://www.example.org<span class="sc3"><span class="re1">&lt;/authorUrl<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;copyright<span class="re2">&gt;</span></span></span>Copyright Info<span class="sc3"><span class="re1">&lt;/copyright<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;license<span class="re2">&gt;</span></span></span>License Info<span class="sc3"><span class="re1">&lt;/license<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="coMULTI">&lt;!--  The version string is recorded in the components table --&gt;</span></span>
 <span class="sc3"><span class="re1">&lt;version<span class="re2">&gt;</span></span></span>1.01<span class="sc3"><span class="re1">&lt;/version<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="coMULTI">&lt;!-- The description is optional and defaults to the name --&gt;</span></span>
 <span class="sc3"><span class="re1">&lt;description<span class="re2">&gt;</span></span></span>Description of the component ...<span class="sc3"><span class="re1">&lt;/description<span class="re2">&gt;</span></span></span>

 <span class="sc3"><span class="coMULTI">&lt;!-- Site Main File Copy Section --&gt;</span></span>
 <span class="sc3"><span class="coMULTI">&lt;!-- Note the folder attribute: This attribute describes the folder
      to copy FROM in the package to install therefore files copied
      in this section are copied from /site/ in the package --&gt;</span></span>
 <span class="sc3"><span class="re1">&lt;files</span> <span class="re0">folder</span>=<span class="st0">"site"</span><span class="re2">&gt;</span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>controller.php<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>hello.php<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>index.html<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>models/hello.php<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>models/index.html<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>views/index.html<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>views/hello/index.html<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>views/hello/view.html.php<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>views/hello/tmpl/default.php<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>views/hello/tmpl/index.html<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
 <span class="sc3"><span class="re1">&lt;/files<span class="re2">&gt;</span></span></span>

 <span class="sc3"><span class="re1">&lt;administration<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="coMULTI">&lt;!-- Administration Menu Section --&gt;</span></span>
  <span class="sc3"><span class="re1">&lt;menu<span class="re2">&gt;</span></span></span>Hello World!<span class="sc3"><span class="re1">&lt;/menu<span class="re2">&gt;</span></span></span>

  <span class="sc3"><span class="coMULTI">&lt;!-- Administration Main File Copy Section --&gt;</span></span>
  <span class="sc3"><span class="re1">&lt;files</span> <span class="re0">folder</span>=<span class="st0">"admin"</span><span class="re2">&gt;</span></span>
   <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>hello.php<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
   <span class="sc3"><span class="re1">&lt;filename<span class="re2">&gt;</span></span></span>index.html<span class="sc3"><span class="re1">&lt;/filename<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;/files<span class="re2">&gt;</span></span></span>

 <span class="sc3"><span class="re1">&lt;/administration<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/install<span class="re2">&gt;</span></span></span></pre>
</div>
<p><a id="Conclusion" name="Conclusion"></a></p>
<h2><span class="mw-headline">Conclusion</span></h2>
<p>We now have a simple MVC component. Each element is very simple at this point, but provides a great deal of flexibility and power.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.result-search.com/sty/2009/07/22/developing-a-model-view-controller-component-part-2-adding-a-model.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
