<?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>Acts Media &#187; mvc</title>
	<atom:link href="http://blog.actsmedia.com/tag/mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.actsmedia.com</link>
	<description>Acts Media specializes in Wed Design, Web Development, and Flash/Flex Authoring</description>
	<lastBuildDate>Thu, 27 Jan 2011 16:15:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Joomla Component Global Models</title>
		<link>http://blog.actsmedia.com/2009/08/joomla-component-global-models/</link>
		<comments>http://blog.actsmedia.com/2009/08/joomla-component-global-models/#comments</comments>
		<pubDate>Sat, 29 Aug 2009 03:29:35 +0000</pubDate>
		<dc:creator>Bryn Knatterud</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[joomla]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.actsmedia.com/?p=95</guid>
		<description><![CDATA[One thing that I often find necessary when creating a custom component in Joomla is the need to have a &#8220;Global&#8221; model that I can use in some or all of my views. It took me awhile to find documentation on this and what I did find was outdated documentation, so I think it is [...]]]></description>
			<content:encoded><![CDATA[<p>One thing that I often find necessary when creating a custom component in Joomla is the need to have a &#8220;Global&#8221; model that I can use in some or all of my views. It took me awhile to find documentation on this and what I did find was <a href="http://forum.joomla.org/viewtopic.php?f=231&#038;t=173921" target="_blank">outdated documentation</a>, so I think it is worth providing some here&#8230;</p>
<p>I will use some simple code examples so you can get an idea of how it works.</p>
<h3>Prerequisite knowledge</h3>
<ul>
<li>Basic understanding of <a href="http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1" target="_blank">Joomla component MVC</a></li>
</ul>
<h3>1. Create your global model</h3>
<p>Create a file called &#8220;global.php&#8221; and place it in the &#8220;models&#8221; folder of you component.</p>
<p>Insert the following code&#8230;[global.php]</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'_JEXEC'</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Resctricted access'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jimport<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'joomla.application.component.model'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
JModel<span style="color: #339933;">::</span><span style="color: #004000;">addIncludePath</span><span style="color: #009900;">&#40;</span>JPATH_COMPONENT<span style="color: #339933;">.</span>DS<span style="color: #339933;">.</span><span style="color: #0000ff;">'models'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">class</span> CustomComponentModelGlobal <span style="color: #000000; font-weight: bold;">extends</span> JModel
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">function</span> getData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;My Global Data&quot;</span><span style="color: #339933;">;</span>		
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>2. Modify Controller Code</h3>
<p>We will modify the code of the display method in the controller to make our global model available in all of the views.</p>
<p><em>controller.php</em></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> display<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$view</span> <span style="color: #339933;">=</span> JRequest<span style="color: #339933;">::</span><span style="color: #004000;">getVar</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'view'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$view</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		JRequest<span style="color: #339933;">::</span><span style="color: #004000;">setVar</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'view'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'default'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000088;">$model</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getModel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Global'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$document</span> <span style="color: #339933;">=&amp;</span> JFactory<span style="color: #339933;">::</span><span style="color: #004000;">getDocument</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$viewType</span>	<span style="color: #339933;">=</span> <span style="color: #000088;">$document</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$viewName</span>	<span style="color: #339933;">=</span> <span style="color: #000088;">$view</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$view</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getView</span><span style="color: #009900;">&#40;</span> 
                             <span style="color: #000088;">$viewName</span><span style="color: #339933;">,</span>
                             <span style="color: #000088;">$viewType</span><span style="color: #339933;">,</span> 
                             <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
                             <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'base_path'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_basePath<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setModel</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$model</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	parent<span style="color: #339933;">::</span><span style="color: #004000;">display</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>3. Access the Global model in the view</h3>
<p><em>view.html.php</em></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> display<span style="color: #009900;">&#40;</span><span style="color: #000088;">$tpl</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">/*
	 * The model can now be accessed in the view
	 * in either way below
	 */</span>
&nbsp;
	<span style="color: #000088;">$data</span> <span style="color: #339933;">=&amp;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'data'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Global'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$globalModel</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getModel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Global'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$data</span> <span style="color: #339933;">=&amp;</span> <span style="color: #000088;">$globalModel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//assign the data to a ref for output in the html</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assignRef</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'myGobalData'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.actsmedia.com/2009/08/joomla-component-global-models/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

